github.com/prebid/prebid-server@v0.275.0/endpoints/version.go (about)

     1  package endpoints
     2  
     3  import (
     4  	"encoding/json"
     5  	"net/http"
     6  
     7  	"github.com/golang/glog"
     8  )
     9  
    10  const versionEndpointValueNotSet = "not-set"
    11  
    12  // NewVersionEndpoint returns the latest git tag as the version and commit hash as the revision from which the binary was built
    13  func NewVersionEndpoint(version, revision string) http.HandlerFunc {
    14  	response, err := prepareVersionEndpointResponse(version, revision)
    15  	if err != nil {
    16  		glog.Fatalf("error creating /version endpoint response: %v", err)
    17  	}
    18  
    19  	return func(w http.ResponseWriter, _ *http.Request) {
    20  		w.Write(response)
    21  	}
    22  }
    23  
    24  func prepareVersionEndpointResponse(version, revision string) (json.RawMessage, error) {
    25  	if version == "" {
    26  		version = versionEndpointValueNotSet
    27  	}
    28  	if revision == "" {
    29  		revision = versionEndpointValueNotSet
    30  	}
    31  
    32  	return json.Marshal(struct {
    33  		Revision string `json:"revision"`
    34  		Version  string `json:"version"`
    35  	}{
    36  		Revision: revision,
    37  		Version:  version,
    38  	})
    39  }