Skip to content

Commit

Permalink
refactor(server.go): improve code lisibility
Browse files Browse the repository at this point in the history
factorize repetitive blocks
verify http method on openapi request
  • Loading branch information
leofvo committed Oct 24, 2024
1 parent d4eef32 commit 298593b
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions internal/servers/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,27 +308,30 @@ func (s *Container) Run(
// Create the gRPC gateway mux
grpcMux := runtime.NewServeMux(muxOpts...)

if err = grpcV1.RegisterPermissionHandler(ctx, grpcMux, conn); err != nil {
return err
}
if err = grpcV1.RegisterSchemaHandler(ctx, grpcMux, conn); err != nil {
return err
}
if err = grpcV1.RegisterDataHandler(ctx, grpcMux, conn); err != nil {
return err
handlers := []func(context.Context, *runtime.ServeMux, *grpc.ClientConn) error{
grpcV1.RegisterPermissionHandler,
grpcV1.RegisterSchemaHandler,
grpcV1.RegisterDataHandler,
grpcV1.RegisterBundleHandler,
grpcV1.RegisterTenancyHandler,
}
if err = grpcV1.RegisterBundleHandler(ctx, grpcMux, conn); err != nil {
return err
}
if err = grpcV1.RegisterTenancyHandler(ctx, grpcMux, conn); err != nil {
return err

for _, handler := range handlers {
if err = handler(ctx, grpcMux, conn); err != nil {
return fmt.Errorf("failed to register handler: %w", 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) {
if r.Method != http.MethodGet {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
w.Header().Set("Content-Type", "application/json")
http.ServeFile(w, r, "./docs/api-reference/openapi.json")
})
}
Expand Down

0 comments on commit 298593b

Please sign in to comment.