2023-12-03 06:40:01 +00:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
|
|
|
|
// logging "github.com/ipfs/go-log/v2"
|
|
|
|
"github.com/filecoin-project/go-jsonrpc"
|
|
|
|
"github.com/filecoin-project/go-jsonrpc/auth"
|
|
|
|
|
|
|
|
"github.com/filecoin-project/lotus/api"
|
|
|
|
"github.com/filecoin-project/lotus/lib/rpcenc"
|
|
|
|
"github.com/filecoin-project/lotus/metrics/proxy"
|
|
|
|
)
|
|
|
|
|
|
|
|
//var log = logging.Logger("lp/rpc")
|
|
|
|
|
|
|
|
func LotusProviderHandler(
|
|
|
|
authv func(ctx context.Context, token string) ([]auth.Permission, error),
|
|
|
|
remote http.HandlerFunc,
|
|
|
|
a api.LotusProvider,
|
|
|
|
permissioned bool) http.Handler {
|
|
|
|
mux := mux.NewRouter()
|
|
|
|
readerHandler, readerServerOpt := rpcenc.ReaderParamDecoder()
|
|
|
|
rpcServer := jsonrpc.NewServer(jsonrpc.WithServerErrors(api.RPCErrors), readerServerOpt)
|
|
|
|
|
2023-12-04 13:12:02 +00:00
|
|
|
wapi := proxy.MetricedAPI[api.LotusProvider, *api.LotusProviderStruct](a)
|
2023-12-03 06:40:01 +00:00
|
|
|
if permissioned {
|
2023-12-04 13:12:02 +00:00
|
|
|
wapi = api.PermissionedAPI[api.LotusProvider, *api.LotusProviderStruct](wapi)
|
2023-12-03 06:40:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rpcServer.Register("Filecoin", wapi)
|
|
|
|
rpcServer.AliasMethod("rpc.discover", "Filecoin.Discover")
|
|
|
|
|
|
|
|
mux.Handle("/rpc/v0", rpcServer)
|
|
|
|
mux.Handle("/rpc/streams/v0/push/{uuid}", readerHandler)
|
|
|
|
mux.PathPrefix("/remote").HandlerFunc(remote)
|
|
|
|
mux.PathPrefix("/").Handler(http.DefaultServeMux) // pprof
|
|
|
|
|
|
|
|
if !permissioned {
|
|
|
|
return mux
|
|
|
|
}
|
|
|
|
|
|
|
|
ah := &auth.Handler{
|
|
|
|
Verify: authv,
|
|
|
|
Next: mux.ServeHTTP,
|
|
|
|
}
|
|
|
|
return ah
|
|
|
|
}
|