infra/op-ufm/pkg/service/healthz_server.go

38 lines
713 B
Go
Raw Normal View History

package service
import (
"context"
"net/http"
"github.com/gorilla/mux"
"github.com/rs/cors"
)
2023-07-15 00:08:02 +03:00
type HealthzServer struct {
ctx context.Context
server *http.Server
}
2023-07-18 20:51:22 +03:00
func (h *HealthzServer) Start(ctx context.Context, addr string) error {
hdlr := mux.NewRouter()
hdlr.HandleFunc("/healthz", h.Handle).Methods("GET")
c := cors.New(cors.Options{
AllowedOrigins: []string{"*"},
})
server := &http.Server{
Handler: c.Handler(hdlr),
Addr: addr,
}
h.server = server
h.ctx = ctx
return h.server.ListenAndServe()
}
2023-07-15 00:08:02 +03:00
func (h *HealthzServer) Shutdown() error {
return h.server.Shutdown(h.ctx)
}
2023-07-15 00:08:02 +03:00
func (h *HealthzServer) Handle(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("OK"))
}