Skip to content

Commit

Permalink
build: expose openapi on http server
Browse files Browse the repository at this point in the history
  • Loading branch information
leofvo committed Oct 24, 2024
1 parent dd605a2 commit d4eef32
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
1 change: 1 addition & 0 deletions example.config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ server:
http:
enabled: true
port: 3476
expose_openapi: false
tls:
enabled: false
cert: /etc/letsencrypt/live/yourdomain.com/fullchain.pem
Expand Down
2 changes: 2 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type (
HTTP struct {
Enabled bool `mapstructure:"enabled"` // Whether the HTTP server is enabled
Port string `mapstructure:"port"` // Port for the HTTP server
ExposeOpenAPI bool `mapstructure:"expose_openapi"` // expose OpenAPI configuration
TLSConfig TLSConfig `mapstructure:"tls"` // TLS configuration for the HTTP server
CORSAllowedOrigins []string `mapstructure:"cors_allowed_origins"` // List of allowed origins for CORS
CORSAllowedHeaders []string `mapstructure:"cors_allowed_headers"` // List of allowed headers for CORS
Expand Down Expand Up @@ -276,6 +277,7 @@ func DefaultConfig() *Config {
HTTP: HTTP{
Enabled: true,
Port: "3476",
ExposeOpenAPI: false,
TLSConfig: TLSConfig{
Enabled: false,
},
Expand Down
27 changes: 20 additions & 7 deletions internal/servers/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,24 +305,37 @@ func (s *Container) Run(
}),
}

mux := runtime.NewServeMux(muxOpts...)
// Create the gRPC gateway mux
grpcMux := runtime.NewServeMux(muxOpts...)

if err = grpcV1.RegisterPermissionHandler(ctx, mux, conn); err != nil {
if err = grpcV1.RegisterPermissionHandler(ctx, grpcMux, conn); err != nil {
return err
}
if err = grpcV1.RegisterSchemaHandler(ctx, mux, conn); err != nil {
if err = grpcV1.RegisterSchemaHandler(ctx, grpcMux, conn); err != nil {
return err
}
if err = grpcV1.RegisterDataHandler(ctx, mux, conn); err != nil {
if err = grpcV1.RegisterDataHandler(ctx, grpcMux, conn); err != nil {
return err
}
if err = grpcV1.RegisterBundleHandler(ctx, mux, conn); err != nil {
if err = grpcV1.RegisterBundleHandler(ctx, grpcMux, conn); err != nil {
return err
}
if err = grpcV1.RegisterTenancyHandler(ctx, mux, conn); err != nil {
if err = grpcV1.RegisterTenancyHandler(ctx, grpcMux, conn); err != nil {
return err
}

// Create a new http.ServeMux for serving your OpenAPI file and gRPC gateway
httpMux := http.NewServeMux()

if srv.HTTP.ExposeOpenAPI {
httpMux.HandleFunc("/openapi.json", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "./docs/api-reference/openapi.json")
})
}

// Handle all gRPC gateway routes
httpMux.Handle("/", grpcMux)

httpServer = &http.Server{
Addr: ":" + srv.HTTP.Port,
Handler: cors.New(cors.Options{
Expand All @@ -333,7 +346,7 @@ func (s *Container) Run(
http.MethodGet, http.MethodPost,
http.MethodHead, http.MethodPatch, http.MethodDelete, http.MethodPut,
},
}).Handler(mux),
}).Handler(httpMux),
ReadHeaderTimeout: 5 * time.Second,
}

Expand Down

0 comments on commit d4eef32

Please sign in to comment.