Skip to content

Commit d4eef32

Browse files
committed
build: expose openapi on http server
1 parent dd605a2 commit d4eef32

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed

example.config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ server:
88
http:
99
enabled: true
1010
port: 3476
11+
expose_openapi: false
1112
tls:
1213
enabled: false
1314
cert: /etc/letsencrypt/live/yourdomain.com/fullchain.pem

internal/config/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ type (
3737
HTTP struct {
3838
Enabled bool `mapstructure:"enabled"` // Whether the HTTP server is enabled
3939
Port string `mapstructure:"port"` // Port for the HTTP server
40+
ExposeOpenAPI bool `mapstructure:"expose_openapi"` // expose OpenAPI configuration
4041
TLSConfig TLSConfig `mapstructure:"tls"` // TLS configuration for the HTTP server
4142
CORSAllowedOrigins []string `mapstructure:"cors_allowed_origins"` // List of allowed origins for CORS
4243
CORSAllowedHeaders []string `mapstructure:"cors_allowed_headers"` // List of allowed headers for CORS
@@ -276,6 +277,7 @@ func DefaultConfig() *Config {
276277
HTTP: HTTP{
277278
Enabled: true,
278279
Port: "3476",
280+
ExposeOpenAPI: false,
279281
TLSConfig: TLSConfig{
280282
Enabled: false,
281283
},

internal/servers/server.go

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -305,24 +305,37 @@ func (s *Container) Run(
305305
}),
306306
}
307307

308-
mux := runtime.NewServeMux(muxOpts...)
308+
// Create the gRPC gateway mux
309+
grpcMux := runtime.NewServeMux(muxOpts...)
309310

310-
if err = grpcV1.RegisterPermissionHandler(ctx, mux, conn); err != nil {
311+
if err = grpcV1.RegisterPermissionHandler(ctx, grpcMux, conn); err != nil {
311312
return err
312313
}
313-
if err = grpcV1.RegisterSchemaHandler(ctx, mux, conn); err != nil {
314+
if err = grpcV1.RegisterSchemaHandler(ctx, grpcMux, conn); err != nil {
314315
return err
315316
}
316-
if err = grpcV1.RegisterDataHandler(ctx, mux, conn); err != nil {
317+
if err = grpcV1.RegisterDataHandler(ctx, grpcMux, conn); err != nil {
317318
return err
318319
}
319-
if err = grpcV1.RegisterBundleHandler(ctx, mux, conn); err != nil {
320+
if err = grpcV1.RegisterBundleHandler(ctx, grpcMux, conn); err != nil {
320321
return err
321322
}
322-
if err = grpcV1.RegisterTenancyHandler(ctx, mux, conn); err != nil {
323+
if err = grpcV1.RegisterTenancyHandler(ctx, grpcMux, conn); err != nil {
323324
return err
324325
}
325326

327+
// Create a new http.ServeMux for serving your OpenAPI file and gRPC gateway
328+
httpMux := http.NewServeMux()
329+
330+
if srv.HTTP.ExposeOpenAPI {
331+
httpMux.HandleFunc("/openapi.json", func(w http.ResponseWriter, r *http.Request) {
332+
http.ServeFile(w, r, "./docs/api-reference/openapi.json")
333+
})
334+
}
335+
336+
// Handle all gRPC gateway routes
337+
httpMux.Handle("/", grpcMux)
338+
326339
httpServer = &http.Server{
327340
Addr: ":" + srv.HTTP.Port,
328341
Handler: cors.New(cors.Options{
@@ -333,7 +346,7 @@ func (s *Container) Run(
333346
http.MethodGet, http.MethodPost,
334347
http.MethodHead, http.MethodPatch, http.MethodDelete, http.MethodPut,
335348
},
336-
}).Handler(mux),
349+
}).Handler(httpMux),
337350
ReadHeaderTimeout: 5 * time.Second,
338351
}
339352

0 commit comments

Comments
 (0)