From 0b609156b184d00ab1a6e742b9b998be4457345e Mon Sep 17 00:00:00 2001 From: Carlos Sosa Date: Mon, 22 Jun 2020 11:33:39 -0700 Subject: First Commit --- server/rest.go | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 server/rest.go (limited to 'server/rest.go') diff --git a/server/rest.go b/server/rest.go new file mode 100644 index 0000000..3dd0f0c --- /dev/null +++ b/server/rest.go @@ -0,0 +1,40 @@ +package server + +import ( + "fmt" + "git.rodere.systems/stream-service/api" + "github.com/grpc-ecosystem/grpc-gateway/runtime" + "golang.org/x/net/context" + "google.golang.org/grpc" + "google.golang.org/grpc/credentials" + "log" + "net/http" +) + +// Start the REST HTTP API server +func startRESTServer(address, grpcAddress, certFile string) error { + ctx := context.Background() + ctx, cancel := context.WithCancel(ctx) + defer cancel() + + mux := runtime.NewServeMux(runtime.WithIncomingHeaderMatcher(credMatcher)) + + creds, err := credentials.NewClientTLSFromFile(certFile, "") + if err != nil { + return fmt.Errorf("could not load TLS certificate: %s", err) + } + + // Setup the client gRPC options + opts := []grpc.DialOption{grpc.WithTransportCredentials(creds)} + + // Register ping + err = api.RegisterPingHandlerFromEndpoint(ctx, mux, grpcAddress, opts) + if err != nil { + return fmt.Errorf("could not register service Ping: %s", err) + } + + log.Printf("starting HTTP/1.1 REST server on %s", address) + http.ListenAndServe(address, mux) + + return nil +} -- cgit v1.3-2-g0d8e