Created
September 16, 2018 07:41
-
-
Save amsokol/bfcce2cdc0dcfc59b3fac0358556e924 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package rest | |
import ( | |
"context" | |
"net/http" | |
"os" | |
"os/signal" | |
"time" | |
"github.com/grpc-ecosystem/grpc-gateway/runtime" | |
"go.uber.org/zap" | |
"google.golang.org/grpc" | |
"github.com/amsokol/go-grpc-http-rest-microservice-tutorial/pkg/api/v1" | |
"github.com/amsokol/go-grpc-http-rest-microservice-tutorial/pkg/logger" | |
"github.com/amsokol/go-grpc-http-rest-microservice-tutorial/pkg/protocol/rest/middleware" | |
) | |
// RunServer runs HTTP/REST gateway | |
func RunServer(ctx context.Context, grpcPort, httpPort string) error { | |
ctx, cancel := context.WithCancel(ctx) | |
defer cancel() | |
mux := runtime.NewServeMux() | |
opts := []grpc.DialOption{grpc.WithInsecure()} | |
if err := v1.RegisterToDoServiceHandlerFromEndpoint(ctx, mux, "localhost:"+grpcPort, opts); err != nil { | |
logger.Log.Fatal("failed to start HTTP gateway", zap.String("reason", err.Error())) | |
} | |
srv := &http.Server{ | |
Addr: ":" + httpPort, | |
// add handler with middleware | |
Handler: middleware.AddRequestID( | |
middleware.AddLogger(logger.Log, mux)), | |
} | |
// graceful shutdown | |
c := make(chan os.Signal, 1) | |
signal.Notify(c, os.Interrupt) | |
go func() { | |
for range c { | |
// sig is a ^C, handle it | |
} | |
_, cancel := context.WithTimeout(ctx, 5*time.Second) | |
defer cancel() | |
_ = srv.Shutdown(ctx) | |
}() | |
logger.Log.Info("starting HTTP/REST gateway...") | |
return srv.ListenAndServe() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment