Skip to content

Commit

Permalink
feat(grpc): add new option that allows cors configuration for the gat…
Browse files Browse the repository at this point in the history
…eway server
  • Loading branch information
bradub committed Mar 10, 2023
1 parent 452a501 commit cc691da
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 137 deletions.
61 changes: 33 additions & 28 deletions grpc/gateway_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"net"
"net/http"
"sync/atomic"
"time"

"contrib.go.opencensus.io/exporter/stackdriver/propagation"
Expand All @@ -19,35 +20,46 @@ import (
"google.golang.org/grpc/credentials/insecure"
)

var _ server = (*gatewayServer)(nil)

type gatewayServer struct {
internalHTTPServer *http.Server
httpServer *http.Server
listener net.Listener
closed atomic.Bool
}

func (s *gatewayServer) addr() string {
return s.listener.Addr().String()
}

func (s *gatewayServer) Serve(listener net.Listener) error {
err := s.internalHTTPServer.Serve(listener)
func (s *gatewayServer) listenAndServe() error {
err := s.httpServer.Serve(s.listener)
if err != nil && !errors.Is(err, http.ErrServerClosed) {
return err
}

return nil
}

func (s *gatewayServer) Close() error {
return s.internalHTTPServer.Shutdown(context.Background())
func (s *gatewayServer) close() error {
if s.closed.Load() {
return nil
}

s.closed.Store(true)

return s.httpServer.Shutdown(context.Background())
}

// nolint: revive // false-positive, it reports tracing as a control flag.
func newGatewayServerWithListener(
func newGatewayServer(
muxOptions []runtime.ServeMuxOption,
tracing bool,
registerGateway registerGatewayFunc,
address string,
middlewares chi.Middlewares,
debugStandardLibraryEndpoints bool,
corsOptions cors.Options,
) (
*serverWithListener,
*gatewayServer,
error,
) {
grpcGatewayMux := runtime.NewServeMux(
Expand Down Expand Up @@ -80,12 +92,7 @@ func newGatewayServerWithListener(
return nil, fmt.Errorf("new listener: %w", err)
}

corsHandler := cors.New(cors.Options{
AllowedMethods: []string{"GET", "POST", "PATCH", "PUT", "DELETE"},
AllowedHeaders: []string{"Accept", "Authorization", "Content-Type"},
ExposedHeaders: []string{"Link", "X-Total-Count"},
AllowCredentials: true,
})
corsHandler := cors.New(corsOptions)

router := chi.NewRouter()

Expand Down Expand Up @@ -115,19 +122,17 @@ func newGatewayServerWithListener(
idleTimeout = 2 * time.Minute
)

return &serverWithListener{
server: &gatewayServer{
internalHTTPServer: &http.Server{
Handler: http.TimeoutHandler(
router,
handlerTimeout,
"",
),
ReadTimeout: readTimeout,
ReadHeaderTimeout: readHeaderTimeout,
WriteTimeout: writeTimeout,
IdleTimeout: idleTimeout,
},
return &gatewayServer{
httpServer: &http.Server{
Handler: http.TimeoutHandler(
router,
handlerTimeout,
"",
),
ReadTimeout: readTimeout,
ReadHeaderTimeout: readHeaderTimeout,
WriteTimeout: writeTimeout,
IdleTimeout: idleTimeout,
},
listener: listener,
},
Expand Down
30 changes: 15 additions & 15 deletions grpc/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,38 @@ require (
github.com/go-chi/chi/v5 v5.0.8
github.com/google/uuid v1.3.0
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0
github.com/grpc-ecosystem/grpc-gateway/v2 v2.15.0
github.com/grpc-ecosystem/grpc-gateway/v2 v2.15.2
github.com/matryer/is v1.4.0
github.com/oklog/run v1.1.0
github.com/rs/cors v1.8.3
go.opencensus.io v0.24.0
go.uber.org/zap v1.24.0
google.golang.org/grpc v1.52.3
google.golang.org/protobuf v1.28.1
google.golang.org/grpc v1.53.0
google.golang.org/protobuf v1.29.0
)

require (
cloud.google.com/go/compute v1.18.0 // indirect
cloud.google.com/go/compute/metadata v0.2.3 // indirect
cloud.google.com/go/monitoring v1.12.0 // indirect
cloud.google.com/go/trace v1.8.0 // indirect
github.com/aws/aws-sdk-go v1.44.189 // indirect
github.com/aws/aws-sdk-go v1.44.218 // indirect
github.com/census-instrumentation/opencensus-proto v0.4.1 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.2.1 // indirect
github.com/googleapis/gax-go/v2 v2.7.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.2.3 // indirect
github.com/googleapis/gax-go/v2 v2.7.1 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/prometheus/prometheus v0.41.0 // indirect
github.com/prometheus/prometheus v0.42.0 // indirect
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/net v0.5.0 // indirect
golang.org/x/oauth2 v0.4.0 // indirect
go.uber.org/multierr v1.10.0 // indirect
golang.org/x/net v0.8.0 // indirect
golang.org/x/oauth2 v0.6.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.4.0 // indirect
golang.org/x/text v0.6.0 // indirect
google.golang.org/api v0.108.0 // indirect
golang.org/x/sys v0.6.0 // indirect
golang.org/x/text v0.8.0 // indirect
google.golang.org/api v0.112.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20230127162408-596548ed4efa // indirect
google.golang.org/genproto v0.0.0-20230306155012-7f2fa6fef1f4 // indirect
)
45 changes: 45 additions & 0 deletions grpc/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit cc691da

Please sign in to comment.