go-ethereum/health/service.go
crypto-services 96968b119e Initial cut
2024-02-15 16:25:35 +08:00

40 lines
1 KiB
Go

package health
import (
"net/http"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/internal/ethapi"
"github.com/ethereum/go-ethereum/node"
)
type handler struct {
ec *ethclient.Client
}
func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
headers := r.Header.Values(healthHeader)
if len(headers) != 0 {
h.processFromHeaders(headers, w, r)
} else {
h.processFromBody(w, r)
}
}
// New constructs a new health service instance.
func New(stack *node.Node, backend ethapi.Backend, cors, vhosts []string) error {
_, err := newHandler(stack, backend, cors, vhosts)
return err
}
// newHandler returns a new `http.Handler` that will answer node health queries.
func newHandler(stack *node.Node, backend ethapi.Backend, cors, vhosts []string) (*handler, error) {
ec := ethclient.NewClient(stack.Attach())
h := handler{ec}
handler := node.NewHTTPHandlerStack(h, cors, vhosts, nil)
stack.RegisterHandler("Health API", "/health", handler)
stack.RegisterHandler("Health API", "/health/", handler)
return &h, nil
}