2023-07-12 00:50:31 +03:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/rs/cors"
|
2024-05-30 21:41:03 +03:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2023-07-12 00:50:31 +03:00
|
|
|
)
|
|
|
|
|
2023-07-15 00:08:02 +03:00
|
|
|
type HealthzServer struct {
|
2023-07-12 00:50:31 +03:00
|
|
|
ctx context.Context
|
|
|
|
server *http.Server
|
|
|
|
}
|
|
|
|
|
2023-07-18 20:51:22 +03:00
|
|
|
func (h *HealthzServer) Start(ctx context.Context, addr string) error {
|
2023-07-14 02:09:56 +03:00
|
|
|
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-12 00:50:31 +03:00
|
|
|
}
|
|
|
|
|
2023-07-15 00:08:02 +03:00
|
|
|
func (h *HealthzServer) Shutdown() error {
|
2023-07-12 00:50:31 +03:00
|
|
|
return h.server.Shutdown(h.ctx)
|
|
|
|
}
|
|
|
|
|
2023-07-15 00:08:02 +03:00
|
|
|
func (h *HealthzServer) Handle(w http.ResponseWriter, r *http.Request) {
|
2024-05-30 21:41:03 +03:00
|
|
|
_, err := w.Write([]byte("OK"))
|
|
|
|
if err != nil {
|
|
|
|
log.Error("error handling HealthzServer response")
|
|
|
|
}
|
2023-07-12 00:50:31 +03:00
|
|
|
}
|