Add REST server in a goroutine
authorJulien Andrieux <julien@pantomath.io>
Fri, 6 Oct 2017 14:59:41 +0000 (16:59 +0200)
committerJulien Andrieux <julien@pantomath.io>
Sat, 7 Oct 2017 13:13:06 +0000 (15:13 +0200)
api/api.proto
server/main.go

index 148d134..2c6c20d 100644 (file)
@@ -13,6 +13,5 @@ service Ping {
         post: "/1/ping"
         body: "*"
     };
-
   }
 }
\ No newline at end of file
index 834f04b..e879387 100644 (file)
@@ -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 {}