lotus/gateway/handler.go

54 lines
1.5 KiB
Go
Raw Normal View History

2021-05-23 17:37:53 +00:00
package gateway
import (
"net/http"
"contrib.go.opencensus.io/exporter/prometheus"
"github.com/filecoin-project/go-jsonrpc"
2022-05-21 01:38:17 +00:00
lapi "github.com/filecoin-project/lotus/api"
2021-05-23 17:37:53 +00:00
"github.com/filecoin-project/lotus/api/v0api"
"github.com/filecoin-project/lotus/api/v1api"
"github.com/filecoin-project/lotus/metrics/proxy"
2022-05-21 01:38:17 +00:00
"github.com/filecoin-project/lotus/node"
2021-05-23 17:37:53 +00:00
"github.com/gorilla/mux"
promclient "github.com/prometheus/client_golang/prometheus"
)
// Handler returns a gateway http.Handler, to be mounted as-is on the server.
2022-05-21 01:38:17 +00:00
func Handler(gwapi lapi.Gateway, api lapi.FullNode, opts ...jsonrpc.ServerOption) (http.Handler, error) {
2021-05-23 17:37:53 +00:00
m := mux.NewRouter()
serveRpc := func(path string, hnd interface{}) {
rpcServer := jsonrpc.NewServer(opts...)
rpcServer.Register("Filecoin", hnd)
2022-05-27 11:23:32 +00:00
rpcServer.AliasMethod("rpc.discover", "Filecoin.Discover")
2021-05-23 17:37:53 +00:00
m.Handle(path, rpcServer)
}
2022-05-21 01:38:17 +00:00
ma := proxy.MetricedGatewayAPI(gwapi)
2021-05-23 17:37:53 +00:00
serveRpc("/rpc/v1", ma)
2022-05-21 01:38:17 +00:00
serveRpc("/rpc/v0", lapi.Wrap(new(v1api.FullNodeStruct), new(v0api.WrapperV1Full), ma))
2021-05-23 17:37:53 +00:00
registry := promclient.DefaultRegisterer.(*promclient.Registry)
exporter, err := prometheus.NewExporter(prometheus.Options{
Registry: registry,
Namespace: "lotus_gw",
})
if err != nil {
return nil, err
}
m.Handle("/debug/metrics", exporter)
2022-05-21 01:38:17 +00:00
m.Handle("/health/livez", node.NewLiveHandler(api))
m.Handle("/health/readyz", node.NewReadyHandler(api))
2021-05-23 17:37:53 +00:00
m.PathPrefix("/").Handler(http.DefaultServeMux)
/*ah := &auth.Handler{
Verify: nodeApi.AuthVerify,
Next: mux.ServeHTTP,
}*/
return m, nil
}