Skip to content

Commit

Permalink
add metadata endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
eibay committed May 26, 2021
1 parent 55f5e84 commit 3349a8d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
29 changes: 29 additions & 0 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"log"
"net/http"
"os/exec"
)

type ApiHandler struct{}
Expand All @@ -13,6 +14,12 @@ type ResponseHealthCheck struct {
Description string `json:"description"`
}

type ResponseMetadata struct {
Version string `json:"version"`
Description string `json:"description"`
LastCommitSha string `json:"lastcommitsha"`
}

func (ah *ApiHandler) handleHello(w http.ResponseWriter, r *http.Request) {
response := "Hello World"

Expand All @@ -31,3 +38,25 @@ func (ah *ApiHandler) handleHealthCheck(w http.ResponseWriter, r *http.Request)
}
w.Write(response)
}

func (ah *ApiHandler) handleMetadata(w http.ResponseWriter, r *http.Request) {
responseMetaData := ResponseMetadata{
Version: "1.0.777",
Description: "basic go-lang api service",
LastCommitSha: string(getSha()),
}

response, err := json.Marshal(&responseMetaData)
if err != nil {
log.Fatal(err)
}
w.Write(response)
}

func getSha() []byte {
sha, err := exec.Command("bash", "-c", "git rev-parse HEAD").Output()
if err != nil {
log.Fatal(err)
}
return sha
}
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ import (

const (
host = "localhost"
port = "8001"
port = "8002"
)

func main() {
apiHandler := ApiHandler{}

http.HandleFunc("/hello", apiHandler.handleHello)
http.HandleFunc("/health", apiHandler.handleHealthCheck)
http.HandleFunc("/metadata", apiHandler.handleMetadata)

err := http.ListenAndServe(host+":"+port, nil)
if err != nil {
Expand Down

0 comments on commit 3349a8d

Please sign in to comment.