Skip to content

Commit 298593b

Browse files
committed
refactor(server.go): improve code lisibility
factorize repetitive blocks verify http method on openapi request
1 parent d4eef32 commit 298593b

File tree

1 file changed

+16
-13
lines changed

1 file changed

+16
-13
lines changed

internal/servers/server.go

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -308,27 +308,30 @@ func (s *Container) Run(
308308
// Create the gRPC gateway mux
309309
grpcMux := runtime.NewServeMux(muxOpts...)
310310

311-
if err = grpcV1.RegisterPermissionHandler(ctx, grpcMux, conn); err != nil {
312-
return err
313-
}
314-
if err = grpcV1.RegisterSchemaHandler(ctx, grpcMux, conn); err != nil {
315-
return err
316-
}
317-
if err = grpcV1.RegisterDataHandler(ctx, grpcMux, conn); err != nil {
318-
return err
311+
handlers := []func(context.Context, *runtime.ServeMux, *grpc.ClientConn) error{
312+
grpcV1.RegisterPermissionHandler,
313+
grpcV1.RegisterSchemaHandler,
314+
grpcV1.RegisterDataHandler,
315+
grpcV1.RegisterBundleHandler,
316+
grpcV1.RegisterTenancyHandler,
319317
}
320-
if err = grpcV1.RegisterBundleHandler(ctx, grpcMux, conn); err != nil {
321-
return err
322-
}
323-
if err = grpcV1.RegisterTenancyHandler(ctx, grpcMux, conn); err != nil {
324-
return err
318+
319+
for _, handler := range handlers {
320+
if err = handler(ctx, grpcMux, conn); err != nil {
321+
return fmt.Errorf("failed to register handler: %w", err)
322+
}
325323
}
326324

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

330328
if srv.HTTP.ExposeOpenAPI {
331329
httpMux.HandleFunc("/openapi.json", func(w http.ResponseWriter, r *http.Request) {
330+
if r.Method != http.MethodGet {
331+
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
332+
return
333+
}
334+
w.Header().Set("Content-Type", "application/json")
332335
http.ServeFile(w, r, "./docs/api-reference/openapi.json")
333336
})
334337
}

0 commit comments

Comments
 (0)