extract starting of rpc servers

This commit is contained in:
Anton Evangelatov 2020-06-30 13:50:46 +02:00
parent 954ad1f396
commit 63bce1a426

View File

@ -466,36 +466,11 @@ func prepareMiner(t *TestEnvironment) (*Node, error) {
panic(err) panic(err)
} }
endpoint, err := minerRepo.APIEndpoint() err = startStorMinerAPIServer(minerRepo, n.minerApi)
if err != nil { if err != nil {
return nil, err return nil, err
} }
lst, err := manet.Listen(endpoint)
if err != nil {
return nil, fmt.Errorf("could not listen: %w", err)
}
mux := mux.NewRouter()
rpcServer := jsonrpc.NewServer()
rpcServer.Register("Filecoin", apistruct.PermissionedStorMinerAPI(n.minerApi))
mux.Handle("/rpc/v0", rpcServer)
mux.PathPrefix("/remote").HandlerFunc(n.minerApi.(*impl.StorageMinerAPI).ServeRemote)
mux.PathPrefix("/").Handler(http.DefaultServeMux) // pprof
ah := &auth.Handler{
Verify: n.minerApi.AuthVerify,
Next: mux.ServeHTTP,
}
srv := &http.Server{Handler: ah}
go func() {
_ = srv.Serve(manet.NetListener(lst))
}()
// add local storage for presealed sectors // add local storage for presealed sectors
err = n.minerApi.StorageAddLocal(ctx, presealDir) err = n.minerApi.StorageAddLocal(ctx, presealDir)
if err != nil { if err != nil {
@ -608,32 +583,11 @@ func prepareClient(t *TestEnvironment) (*Node, error) {
return nil, err return nil, err
} }
endpoint, err := nodeRepo.APIEndpoint() err = startClientAPIServer(nodeRepo, n.fullApi)
if err != nil { if err != nil {
return nil, err return nil, err
} }
lst, err := manet.Listen(endpoint)
if err != nil {
return nil, fmt.Errorf("could not listen: %w", err)
}
rpcServer := jsonrpc.NewServer()
rpcServer.Register("Filecoin", apistruct.PermissionedFullAPI(n.fullApi))
ah := &auth.Handler{
Verify: n.fullApi.AuthVerify,
Next: rpcServer.ServeHTTP,
}
http.Handle("/rpc/v0", ah)
srv := &http.Server{Handler: http.DefaultServeMux}
go func() {
_ = srv.Serve(manet.NetListener(lst))
}()
t.RecordMessage("publish our address to the clients addr topic") t.RecordMessage("publish our address to the clients addr topic")
addrinfo, err := n.fullApi.NetAddrsListen(ctx) addrinfo, err := n.fullApi.NetAddrsListen(ctx)
if err != nil { if err != nil {
@ -838,3 +792,67 @@ func getDrandConfig(ctx context.Context, t *TestEnvironment) (node.Option, error
return nil, fmt.Errorf("unknown random_beacon_type: %s", beaconType) return nil, fmt.Errorf("unknown random_beacon_type: %s", beaconType)
} }
} }
func startStorMinerAPIServer(repo *repo.MemRepo, minerApi api.StorageMiner) error {
endpoint, err := repo.APIEndpoint()
if err != nil {
return err
}
lst, err := manet.Listen(endpoint)
if err != nil {
return fmt.Errorf("could not listen: %w", err)
}
mux := mux.NewRouter()
rpcServer := jsonrpc.NewServer()
rpcServer.Register("Filecoin", apistruct.PermissionedStorMinerAPI(minerApi))
mux.Handle("/rpc/v0", rpcServer)
mux.PathPrefix("/remote").HandlerFunc(minerApi.(*impl.StorageMinerAPI).ServeRemote)
mux.PathPrefix("/").Handler(http.DefaultServeMux) // pprof
ah := &auth.Handler{
Verify: minerApi.AuthVerify,
Next: mux.ServeHTTP,
}
srv := &http.Server{Handler: ah}
go func() {
_ = srv.Serve(manet.NetListener(lst))
}()
return nil
}
func startClientAPIServer(repo *repo.MemRepo, api api.FullNode) error {
endpoint, err := repo.APIEndpoint()
if err != nil {
return err
}
lst, err := manet.Listen(endpoint)
if err != nil {
return fmt.Errorf("could not listen: %w", err)
}
rpcServer := jsonrpc.NewServer()
rpcServer.Register("Filecoin", apistruct.PermissionedFullAPI(api))
ah := &auth.Handler{
Verify: api.AuthVerify,
Next: rpcServer.ServeHTTP,
}
http.Handle("/rpc/v0", ah)
srv := &http.Server{Handler: http.DefaultServeMux}
go func() {
_ = srv.Serve(manet.NetListener(lst))
}()
return nil
}