Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add OpenAPIServable interface #341

Open
dylanhitt opened this issue Jan 9, 2025 · 0 comments
Open

Add OpenAPIServable interface #341

dylanhitt opened this issue Jan 9, 2025 · 0 comments
Assignees

Comments

@dylanhitt
Copy link
Collaborator

per comment #337 (comment)

This interface is intended to unify the serving/configuration of OpenAPI endpoints between the fuego.Server and any adaptors. The current fuego.Server will need to be refactored to adhere to this interface. The current interface will look something like

type OpenAPIServable interface {
    Serve(openapi *fuego.OpenAPI)
}

The fuego.Server already has it's engine/server net/http Server wrapped. In order accomplish this in the case of adaptor. We will need to wrap our engine and say the gin.Engine into the same struct that adheres to this interface. The current code doing this for our server looks like this

// Registers the routes to serve the OpenAPI spec and Swagger UI.
func (s *Server) registerOpenAPIRoutes(jsonSpec []byte) {
	GetStd(s, s.OpenAPIServerConfig.SpecURL, func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("Content-Type", "application/json")
		_, _ = w.Write(jsonSpec)
	})
	s.printOpenAPIMessage(fmt.Sprintf("JSON spec: %s%s", s.url(), s.OpenAPIServerConfig.SpecURL))

	if s.OpenAPIServerConfig.DisableSwaggerUI {
		return
	}
	Registers(s.Engine, netHttpRouteRegisterer[any, any]{
		s: s,
		route: Route[any, any]{
			BaseRoute: BaseRoute{
				Method: http.MethodGet,
				Path:   s.OpenAPIServerConfig.SwaggerURL + "/",
			},
		},
		controller: s.OpenAPIServerConfig.UIHandler(s.OpenAPIServerConfig.SpecURL),
	})
	s.printOpenAPIMessage(fmt.Sprintf("OpenAPI UI: %s%s/index.html", s.url(), s.OpenAPIServerConfig.SwaggerURL))
}

Methods used to print messages should try to adhere to the adaptors logging structure.

A side goal/side effect that may be possible is the removal of the OpenAPIServerConfig as I believe most of this would be able to get into the Engine.OpenAPIConfig

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant