infra/op-conductor-mon/pkg/service/healthz_server.go
felipe 28bb7461e9
feat: op-conductor-mon (#1)
* feat: op-conductor-mon

* Update CODEOWNERS
2024-05-24 14:34:07 -07:00

43 lines
831 B
Go

package service
import (
"context"
"net/http"
"github.com/gorilla/mux"
"github.com/rs/cors"
"github.com/ethereum/go-ethereum/log"
)
type HealthzServer struct {
ctx context.Context
server *http.Server
}
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()
}
func (h *HealthzServer) Shutdown() error {
return h.server.Shutdown(h.ctx)
}
func (h *HealthzServer) Handle(w http.ResponseWriter, r *http.Request) {
_, err := w.Write([]byte("OK"))
if err != nil {
log.Warn("failed to write response: %v", err)
}
}