Skip to content

Commit

Permalink
v0.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Andreas Lindfalk committed May 24, 2022
1 parent 8e048c2 commit b85f89e
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 2 deletions.
63 changes: 62 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,65 @@ Since we only have one port on Cloud Run, but want to serve both the "native" gR

Opentelemetry instrumentation is configured for the gRPC server via the [otelgrpc](https://pkg.go.dev/go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc) lib to intercept both unary and stream communication.

// TODO: continue...
Which handler to use is configurable, provide your implementation of choice by setting "HttpAndGrpcHandlerFunc" in the config. If this is not provided then the "DefaultHttpAndGrpcHandlerFunc" is used, and we also provide a "CorsHttpAndGrpcHandlerFunc" that handles preflight requests so have a closer look at: [handlers.go](https://github.com/dentech-floss/server/blob/master/pkg/server/handlers.go).

## Install

```
go get github.com/dentech-floss/[email protected]
```

## Usage

```go
package example

import (
"github.com/dentech-floss/server/pkg/server"
)

func main() {

ctx := context.Background()

...

appointmentServiceV1 := service.NewAppointmentServiceV1(repo, publisher, logger)
patientGatewayServiceV1 := service.NewPatientGatewayServiceV1(repo, publisher, logger)

...

_server := server.NewServer(
&server.ServerConfig{
Port: config.Port,
// The "DefaultHttpAndGrpcHandlerFunc" will be used if you don't set this
// HttpAndGrpcHandlerFunc: server.CorsHttpAndGrpcHandlerFunc
},
)

appointmentServiceV1.Register(_server.GrpcServer)
appointmentServiceV1.RegisterMux(ctx, _server.GrpcMux) // if you use the grpc gateway

patientGatewayServiceV1.Register(_server.GrpcServer)
patientGatewayServiceV1.RegisterMux(ctx, _server.GrpcMux)

go _server.Serve()
handleShutdown(_server, logger)
}

func handleShutdown(
_server *service.Server,
logger *logging.Logger,
) {
done := make(chan os.Signal, 1)
signal.Notify(done, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
sig := <-done // Block until we receive our signal
logger.Info("Got signal: " + sig.String())

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := _server.Shutdown(ctx); err != nil {
logger.Warn("Server shutdown failed: " + err.Error())
}
}
```
4 changes: 3 additions & 1 deletion pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ func NewServer(config *ServerConfig) *Server {

// Serve both the gRPC server and the http/json proxy on the same port
httpServer := &http.Server{
Handler: h2c.NewHandler(config.HttpAndGrpcHandlerFunc(grpcMux, grpcServer), &http2.Server{}),
Handler: h2c.NewHandler(
config.HttpAndGrpcHandlerFunc(grpcMux, grpcServer), &http2.Server{},
),
}

return &Server{
Expand Down

0 comments on commit b85f89e

Please sign in to comment.