Implement a reverse proxy using grpc-gateway

This commit is contained in:
Marc Di Luzio 2020-06-13 00:23:21 +01:00
parent 7ababb79f6
commit 8c6230ca20
22 changed files with 3122 additions and 802 deletions

View file

@ -0,0 +1,45 @@
package main
import (
"context"
"flag"
"fmt"
"net/http"
"os"
"github.com/golang/glog"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"google.golang.org/grpc"
"github.com/mdiluz/rove/pkg/rove"
)
var endpoint = os.Getenv("GRPC_ENDPOINT")
var address = os.Getenv("HOST_ADDRESS")
func run() error {
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
// Create a new mux and register it with the gRPC engpoint
fmt.Printf("Hosting reverse-proxy on %s for %s\n", address, endpoint)
mux := runtime.NewServeMux()
opts := []grpc.DialOption{grpc.WithInsecure()}
err := rove.RegisterRoveServerHandlerFromEndpoint(ctx, mux, endpoint, opts)
if err != nil {
return err
}
// Start the HTTP server and proxy calls to gRPC endpoint when needed
return http.ListenAndServe(address, mux)
}
func main() {
flag.Parse()
defer glog.Flush()
if err := run(); err != nil {
glog.Fatal(err)
}
}

View file

@ -129,7 +129,7 @@ func (s *Server) Initialise(fillWorld bool) (err error) {
log.Fatalf("failed to listen: %v", err)
}
s.grpcServ = grpc.NewServer()
rove.RegisterRoverServerServer(s.grpcServ, s)
rove.RegisterRoveServerServer(s.grpcServ, s)
return nil
}

View file

@ -98,7 +98,7 @@ func InnerMain(command string) error {
if err != nil {
return err
}
var client = rove.NewRoverServerClient(clientConn)
var client = rove.NewRoveServerClient(clientConn)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()