github.com/prysmaticlabs/prysm@v1.4.4/beacon-chain/server/main.go (about) 1 // Package main allows for creation of an HTTP-JSON to gRPC 2 // gateway as a binary go process. 3 package main 4 5 import ( 6 "context" 7 "flag" 8 "fmt" 9 "net/http" 10 "strings" 11 12 joonix "github.com/joonix/log" 13 beaconGateway "github.com/prysmaticlabs/prysm/beacon-chain/gateway" 14 "github.com/prysmaticlabs/prysm/beacon-chain/rpc/apimiddleware" 15 "github.com/prysmaticlabs/prysm/shared/gateway" 16 _ "github.com/prysmaticlabs/prysm/shared/maxprocs" 17 "github.com/sirupsen/logrus" 18 ) 19 20 var ( 21 beaconRPC = flag.String("beacon-rpc", "localhost:4000", "Beacon chain gRPC endpoint") 22 port = flag.Int("port", 8000, "Port to serve on") 23 ethApiPort = flag.Int("port", 8001, "Port to serve Ethereum API on") 24 host = flag.String("host", "127.0.0.1", "Host to serve on") 25 debug = flag.Bool("debug", false, "Enable debug logging") 26 allowedOrigins = flag.String("corsdomain", "localhost:4242", "A comma separated list of CORS domains to allow") 27 enableDebugRPCEndpoints = flag.Bool("enable-debug-rpc-endpoints", false, "Enable debug rpc endpoints such as /eth/v1alpha1/beacon/state") 28 grpcMaxMsgSize = flag.Int("grpc-max-msg-size", 1<<22, "Integer to define max recieve message call size") 29 ) 30 31 func init() { 32 logrus.SetFormatter(joonix.NewFormatter()) 33 } 34 35 func main() { 36 flag.Parse() 37 if *debug { 38 log.SetLevel(logrus.DebugLevel) 39 } 40 41 gatewayConfig := beaconGateway.DefaultConfig(*enableDebugRPCEndpoints) 42 43 gw := gateway.New( 44 context.Background(), 45 []gateway.PbMux{gatewayConfig.V1Alpha1PbMux, gatewayConfig.V1PbMux}, 46 gatewayConfig.Handler, 47 *beaconRPC, 48 fmt.Sprintf("%s:%d", *host, *port), 49 ).WithAllowedOrigins(strings.Split(*allowedOrigins, ",")). 50 WithMaxCallRecvMsgSize(uint64(*grpcMaxMsgSize)). 51 WithApiMiddleware(fmt.Sprintf("%s:%d", *host, *ethApiPort), &apimiddleware.BeaconEndpointFactory{}) 52 53 mux := http.NewServeMux() 54 mux.HandleFunc("/swagger/", gateway.SwaggerServer()) 55 mux.HandleFunc("/healthz", healthzServer(gw)) 56 gw = gw.WithMux(mux) 57 58 gw.Start() 59 60 select {} 61 } 62 63 // healthzServer returns a simple health handler which returns ok. 64 func healthzServer(gw *gateway.Gateway) http.HandlerFunc { 65 return func(w http.ResponseWriter, r *http.Request) { 66 w.Header().Set("Content-Type", "text/plain") 67 if err := gw.Status(); err != nil { 68 http.Error(w, err.Error(), http.StatusBadGateway) 69 return 70 } 71 if _, err := fmt.Fprintln(w, "ok"); err != nil { 72 log.WithError(err).Error("failed to respond to healthz") 73 } 74 } 75 }