Created
August 1, 2018 10:44
-
-
Save akovardin/f8d8184f36ee7cfe6ac5bc12a908a335 to your computer and use it in GitHub Desktop.
errgroup
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 gateway | |
| import ( | |
| "context" | |
| "net/http" | |
| "github.com/grpc-ecosystem/grpc-gateway/runtime" | |
| "google.golang.org/grpc" | |
| "golang.org/x/sync/errgroup" | |
| "github.com/propellerads/regressor/api" | |
| "github.com/propellerads/regressor/app/log" | |
| ) | |
| func Start(ctx context.Context, addr, endpoint string) error { | |
| mux := runtime.NewServeMux() | |
| opts := []grpc.DialOption{grpc.WithInsecure()} | |
| if err := api.RegisterRegressorServiceHandlerFromEndpoint(ctx, mux, endpoint, opts); err != nil { | |
| log.Warnw("failed to listen grpc addr", "value", err) | |
| return err | |
| } | |
| srv := &http.Server{ | |
| Handler: mux, | |
| Addr: addr, | |
| } | |
| log.Infow("starting grpc gateway server", "address", addr) | |
| e, _ := errgroup.WithContext(ctx) | |
| e.Go(func() error { | |
| return srv.ListenAndServe() | |
| }) | |
| e.Go(func() error { | |
| <-ctx.Done() | |
| return srv.Shutdown(ctx) | |
| }) | |
| return e.Wait() | |
| }errgroup |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment