Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[2.0] add healthcheck #15

Merged
merged 1 commit into from
Aug 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,6 @@ WORKDIR /go/src/github.com/tokopedia/gripmock

EXPOSE 4770 4771

HEALTHCHECK CMD curl --fail http://127.0.0.1:4771/health

ENTRYPOINT ["gripmock"]
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ require (

require (
github.com/fatih/color v1.10.0 // indirect
github.com/felixge/httpsnoop v1.0.1 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/gorilla/handlers v1.5.1 // indirect
github.com/mattn/go-colorable v0.1.8 // indirect
github.com/mattn/go-isatty v0.0.12 // indirect
golang.org/x/net v0.14.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/fatih/color v1.10.0 h1:s36xzo75JdqLaaWoiEHk767eHiwo0598uUxyfiPkDsg=
github.com/fatih/color v1.10.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGEBuJM=
github.com/felixge/httpsnoop v1.0.1 h1:lvB5Jl89CsZtGIWuTcDM1E/vkVs49/Ml7JJe07l8SPQ=
github.com/felixge/httpsnoop v1.0.1/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
github.com/go-playground/locales v0.13.0 h1:HyWk6mgj5qFqCT5fjGBuRArbVDfE4hi8+e8ceBS/t7Q=
github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8=
github.com/go-playground/universal-translator v0.17.0 h1:icxd5fm+REJzpZx7ZfpaD876Lmtgy7VtROAbHHXk8no=
Expand All @@ -16,6 +18,8 @@ github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/handlers v1.5.1 h1:9lRY6j8DEeeBT10CvO9hGW0gmky0BprnvDI5vfhUHH4=
github.com/gorilla/handlers v1.5.1/go.mod h1:t8XrUpc4KVXb7HGyJ4/cEnwQiaxrX/hz1Zv/4g96P1Q=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y=
Expand Down
28 changes: 19 additions & 9 deletions stub/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,17 @@ import (
"golang.org/x/text/language"
)

type Handler struct {
type HealthcheckHandler struct{}

func (*HealthcheckHandler) ServeHTTP(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(200)
}

func NewHealthcheckHandler() *HealthcheckHandler {
return &HealthcheckHandler{}
}

type ApiHandler struct {
stubs *storage.StubStorage
convertor *yaml2json.Convertor
}
Expand All @@ -25,11 +35,11 @@ type findStubPayload struct {
Data map[string]interface{} `json:"data"`
}

func NewHandler() *Handler {
return &Handler{stubs: storage.New(), convertor: yaml2json.New()}
func NewApiHandler() *ApiHandler {
return &ApiHandler{stubs: storage.New(), convertor: yaml2json.New()}
}

func (h *Handler) searchHandle(w http.ResponseWriter, r *http.Request) {
func (h *ApiHandler) searchHandle(w http.ResponseWriter, r *http.Request) {
stub := new(findStubPayload)
decoder := json.NewDecoder(r.Body)
decoder.UseNumber()
Expand Down Expand Up @@ -57,12 +67,12 @@ func (h *Handler) searchHandle(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(output)
}

func (h *Handler) purgeHandle(w http.ResponseWriter, _ *http.Request) {
func (h *ApiHandler) purgeHandle(w http.ResponseWriter, _ *http.Request) {
h.stubs.Purge()
w.WriteHeader(204)
}

func (h *Handler) listHandle(w http.ResponseWriter, _ *http.Request) {
func (h *ApiHandler) listHandle(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "application/json")
err := json.NewEncoder(w).Encode(h.stubs.Stubs())
if err != nil {
Expand All @@ -71,7 +81,7 @@ func (h *Handler) listHandle(w http.ResponseWriter, _ *http.Request) {
}
}

func (h *Handler) addHandle(w http.ResponseWriter, r *http.Request) {
func (h *ApiHandler) addHandle(w http.ResponseWriter, r *http.Request) {
// todo: add supported input array
stub := new(storage.Stub)
decoder := json.NewDecoder(r.Body)
Expand All @@ -97,13 +107,13 @@ func (h *Handler) addHandle(w http.ResponseWriter, r *http.Request) {
}
}

func (h *Handler) responseError(err error, w http.ResponseWriter) {
func (h *ApiHandler) responseError(err error, w http.ResponseWriter) {
w.WriteHeader(500)

_, _ = w.Write([]byte(err.Error()))
}

func (h *Handler) readStubs(path string) {
func (h *ApiHandler) readStubs(path string) {
files, err := os.ReadDir(path)
if err != nil {
log.Printf("Can't read stub from %s. %v\n", path, err)
Expand Down
11 changes: 8 additions & 3 deletions stub/stub.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package stub

import (
"fmt"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
"github.com/tokopedia/gripmock/pkg/storage"
"golang.org/x/text/cases"
"golang.org/x/text/language"
"log"
"net/http"
"os"
)

type Options struct {
Expand All @@ -24,12 +26,15 @@ func RunStubServer(opt Options) {
}
addr := opt.BindAddr + ":" + opt.Port

api := NewHandler()
api := NewApiHandler()
if opt.StubPath != "" {
api.readStubs(opt.StubPath)
}

healthcheck := NewHealthcheckHandler()

router := mux.NewRouter()
router.Handle("/health", healthcheck).Methods("GET")

apiRouter := router.PathPrefix("/api").Subrouter()
apiRouter.HandleFunc("/stubs/search", api.searchHandle).Methods("POST")
Expand All @@ -39,8 +44,8 @@ func RunStubServer(opt Options) {

fmt.Println("Serving stub admin on http://" + addr)
go func() {
http.Handle("/", router)
err := http.ListenAndServe(addr, nil)
handler := handlers.CompressHandler(handlers.LoggingHandler(os.Stdout, router))
err := http.ListenAndServe(addr, handler)
log.Fatal(err)
}()
}
Expand Down