From: Julien Andrieux Date: Fri, 6 Oct 2017 14:59:41 +0000 (+0200) Subject: Add REST server in a goroutine X-Git-Url: https://git.halfball.org/?a=commitdiff_plain;h=bb1fb36330d1ca83dd09f919bdcdb8966cd22ede;p=demo-grpc.git Add REST server in a goroutine --- diff --git a/api/api.proto b/api/api.proto index 148d134..2c6c20d 100644 --- a/api/api.proto +++ b/api/api.proto @@ -13,6 +13,5 @@ service Ping { post: "/1/ping" body: "*" }; - } } \ No newline at end of file diff --git a/server/main.go b/server/main.go index 834f04b..e879387 100644 --- a/server/main.go +++ b/server/main.go @@ -4,8 +4,11 @@ import ( "fmt" "log" "net" + "net/http" "strings" + "github.com/grpc-ecosystem/grpc-gateway/runtime" + "golang.org/x/net/context" "gitlab.com/pantomath-io/demo-grpc/api" @@ -21,6 +24,13 @@ const ( clientIDKey contextKey = iota ) +func credMatcher(headerName string) (mdName string, ok bool) { + if headerName == "Login" || headerName == "Password" { + return headerName, true + } + return "", false +} + // authenticateAgent check the client credentials func authenticateClient(ctx context.Context, s *api.Server) (string, error) { if md, ok := metadata.FromIncomingContext(ctx); ok { @@ -92,9 +102,37 @@ func startGRPCServer(address, certFile, keyFile string) error { return nil } +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 +} + // main start a gRPC server and waits for connection func main() { grpcAddress := fmt.Sprintf("%s:%d", "localhost", 7777) + restAddress := fmt.Sprintf("%s:%d", "localhost", 7778) certFile := "cert/server.crt" keyFile := "cert/server.key" @@ -106,6 +144,14 @@ func main() { } }() + // fire the REST server in a goroutine + go func() { + err := startRESTServer(restAddress, grpcAddress, certFile) + if err != nil { + log.Fatalf("failed to start gRPC server: %s", err) + } + }() + // infinite loop log.Printf("Entering infinite loop") select {}