2020-04-29 17:40:33 +00:00
|
|
|
// Package main allows for creation of an HTTP-JSON to gRPC
|
|
|
|
// gateway as a binary go process.
|
2019-06-02 15:33:44 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2020-03-23 18:17:17 +00:00
|
|
|
"strings"
|
2019-06-02 15:33:44 +00:00
|
|
|
|
|
|
|
joonix "github.com/joonix/log"
|
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/gateway"
|
|
|
|
"github.com/sirupsen/logrus"
|
2019-06-13 14:53:42 +00:00
|
|
|
_ "go.uber.org/automaxprocs"
|
2019-06-02 15:33:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2020-03-23 18:17:17 +00:00
|
|
|
beaconRPC = flag.String("beacon-rpc", "localhost:4000", "Beacon chain gRPC endpoint")
|
|
|
|
port = flag.Int("port", 8000, "Port to serve on")
|
|
|
|
debug = flag.Bool("debug", false, "Enable debug logging")
|
2020-04-29 17:40:33 +00:00
|
|
|
allowedOrigins = flag.String("corsdomain", "", "A comma separated list of CORS domains to allow")
|
2019-06-02 15:33:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
logrus.SetFormatter(joonix.NewFormatter())
|
|
|
|
}
|
|
|
|
|
|
|
|
var log = logrus.New()
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
flag.Parse()
|
|
|
|
if *debug {
|
|
|
|
log.SetLevel(logrus.DebugLevel)
|
|
|
|
}
|
|
|
|
|
|
|
|
mux := http.NewServeMux()
|
2020-03-23 18:17:17 +00:00
|
|
|
gw := gateway.New(context.Background(), *beaconRPC, fmt.Sprintf("0.0.0.0:%d", *port), mux, strings.Split(*allowedOrigins, ","))
|
2019-06-02 15:33:44 +00:00
|
|
|
mux.HandleFunc("/swagger/", gateway.SwaggerServer())
|
|
|
|
mux.HandleFunc("/healthz", healthzServer(gw))
|
|
|
|
gw.Start()
|
|
|
|
|
|
|
|
select {}
|
|
|
|
}
|
|
|
|
|
|
|
|
// healthzServer returns a simple health handler which returns ok.
|
|
|
|
func healthzServer(gw *gateway.Gateway) http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Content-Type", "text/plain")
|
|
|
|
if err := gw.Status(); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusBadGateway)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if _, err := fmt.Fprintln(w, "ok"); err != nil {
|
|
|
|
log.WithError(err).Error("failed to respond to healthz")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|