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

43 lines
836 B
Go
Raw Normal View History

package service
import (
"context"
"net/http"
"github.com/gorilla/mux"
"github.com/rs/cors"
"github.com/ethereum/go-ethereum/log"
)
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) {
_, err := w.Write([]byte("OK"))
if err != nil {
log.Error("error handling HealthzServer response")
}
}