mirror of
https://github.com/scroll-tech/scroll.git
synced 2026-01-14 08:28:02 -05:00
Co-authored-by: chuhanjin <419436363@qq.com> Co-authored-by: maskpp <maskpp266@gmail.com> Co-authored-by: Lawliet-Chan <1576710154@qq.com> Co-authored-by: HAOYUatHZ <37070449+HAOYUatHZ@users.noreply.github.com> Co-authored-by: colinlyguo <colinlyguo@gmail.com> Co-authored-by: Péter Garamvölgyi <peter@scroll.io> Co-authored-by: colinlyguo <102356659+colinlyguo@users.noreply.github.com>
49 lines
1.3 KiB
Go
49 lines
1.3 KiB
Go
package utils
|
|
|
|
import (
|
|
"net"
|
|
"net/http"
|
|
|
|
"github.com/scroll-tech/go-ethereum/log"
|
|
"github.com/scroll-tech/go-ethereum/rpc"
|
|
)
|
|
|
|
// StartHTTPEndpoint starts the HTTP RPC endpoint.
|
|
func StartHTTPEndpoint(endpoint string, apis []rpc.API) (*http.Server, net.Addr, error) {
|
|
srv := rpc.NewServer()
|
|
for _, api := range apis {
|
|
if err := srv.RegisterName(api.Namespace, api.Service); err != nil {
|
|
log.Crit("register namespace failed", "namespace", api.Namespace, "error", err)
|
|
}
|
|
}
|
|
// start the HTTP listener
|
|
var (
|
|
listener net.Listener
|
|
err error
|
|
)
|
|
if listener, err = net.Listen("tcp", endpoint); err != nil {
|
|
return nil, nil, err
|
|
}
|
|
// Bundle and start the HTTP server
|
|
httpSrv := &http.Server{
|
|
Handler: srv,
|
|
ReadTimeout: rpc.DefaultHTTPTimeouts.ReadTimeout,
|
|
WriteTimeout: rpc.DefaultHTTPTimeouts.WriteTimeout,
|
|
IdleTimeout: rpc.DefaultHTTPTimeouts.IdleTimeout,
|
|
}
|
|
go func() {
|
|
_ = httpSrv.Serve(listener)
|
|
}()
|
|
return httpSrv, listener.Addr(), err
|
|
}
|
|
|
|
// StartWSEndpoint starts the WS RPC endpoint.
|
|
func StartWSEndpoint(endpoint string, apis []rpc.API) (*http.Server, net.Addr, error) {
|
|
handler, addr, err := StartHTTPEndpoint(endpoint, apis)
|
|
if err == nil {
|
|
srv := (handler.Handler).(*rpc.Server)
|
|
handler.Handler = srv.WebsocketHandler(nil)
|
|
}
|
|
return handler, addr, err
|
|
}
|