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

BREAKING: create generic engine.OutputOpenAPISpec for use in all engines #302

Open
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

dylanhitt
Copy link
Collaborator

@dylanhitt dylanhitt commented Dec 23, 2024

This pulls out as much functionality as possible in regards to generating the assets when calling engine.OutputOpenApiSpec.

This change is breaking in so far as the OpenAPIConfig will be composed of EngineOpenAPIConfig. I personally don't like these names but I want to ask about changing it here first. I would prefer that we change OpenAPIConfig to ServerOpenAPIConfig and EngineOpenAPIConfig becomes the OpenAPIConfig. The current OpenAPIConfig does have some uniqueness in regards to the fuego.Server. We probably could consolidate nearly all of it though if in #260 we created an interface that dealt with route/url registration, but I'm not sure how far we want to take this. Or if someone can think of a better name for EngineOpenAPIConfig.

For instance every framework we bring in would need to be able to handle something like this to make it work

// OutputOpenAPISpec takes the OpenAPI spec and outputs it to a JSON file and/or serves it on a URL.
// Also serves a Swagger UI.
// To modify its behavior, use the [WithOpenAPIConfig] option.
func (s *Server) OutputOpenAPISpec() openapi3.T {
	s.OpenAPI.Description().Servers = append(s.OpenAPI.Description().Servers, &openapi3.Server{
		URL:         s.url(),
		Description: "local server",
	})

	if !s.OpenAPIConfig.DisableSwagger {
		s.registerOpenAPIRoutes(s.Engine.OutputOpenApiSpec())
	}

	return *s.OpenAPI.Description()
}

// Registers the routes to serve the OpenAPI spec and Swagger UI.
func (s *Server) registerOpenAPIRoutes(jsonSpec []byte) {
	GetStd(s, s.OpenAPIConfig.JsonUrl, 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.OpenAPIConfig.JsonUrl))

	if !s.OpenAPIConfig.DisableSwaggerUI {
		Register(s, Route[any, any]{
			BaseRoute: BaseRoute{
				Method: http.MethodGet,
				Path:   s.OpenAPIConfig.SwaggerUrl + "/",
			},
		}, s.OpenAPIConfig.UIHandler(s.OpenAPIConfig.JsonUrl))
		s.printOpenAPIMessage(fmt.Sprintf("OpenAPI UI: %s%s/index.html", s.url(), s.OpenAPIConfig.SwaggerUrl))
	}
}

@dylanhitt dylanhitt force-pushed the refactor/OutputOpenAPISpec branch 3 times, most recently from 72988f0 to 9248dff Compare December 23, 2024 17:47
openapi.go Outdated Show resolved Hide resolved
@dylanhitt
Copy link
Collaborator Author

Assuming this is acceptable. I believe really the decisions regarding how far we take this will be based on how far we want to take #260.

engine.go Outdated Show resolved Hide resolved
engine.go Outdated Show resolved Hide resolved
engine.go Show resolved Hide resolved
engine.go Outdated Show resolved Hide resolved
server.go Outdated Show resolved Hide resolved
engine.go Outdated Show resolved Hide resolved
engine.go Show resolved Hide resolved
engine.go Show resolved Hide resolved
engine.go Show resolved Hide resolved
engine.go Show resolved Hide resolved
engine.go Outdated Show resolved Hide resolved
engine.go Outdated Show resolved Hide resolved
engine.go Show resolved Hide resolved
@dylanhitt dylanhitt force-pushed the refactor/OutputOpenAPISpec branch 3 times, most recently from 3a65a37 to 19055e1 Compare December 24, 2024 02:48
@dylanhitt dylanhitt requested a review from EwenQuim December 24, 2024 03:32
@dylanhitt dylanhitt force-pushed the refactor/OutputOpenAPISpec branch 2 times, most recently from 84ad214 to 49ae791 Compare December 24, 2024 13:52
server.go Outdated Show resolved Hide resolved
engine.go Show resolved Hide resolved
engine.go Show resolved Hide resolved
engine.go Show resolved Hide resolved
server.go Outdated Show resolved Hide resolved
server.go Outdated Show resolved Hide resolved
@dylanhitt dylanhitt force-pushed the refactor/OutputOpenAPISpec branch 2 times, most recently from 172ab14 to 7f199b4 Compare December 25, 2024 05:45
@dylanhitt dylanhitt requested a review from ccoVeille December 25, 2024 05:48
engine.go Show resolved Hide resolved
@dylanhitt dylanhitt force-pushed the refactor/OutputOpenAPISpec branch from 7f199b4 to bd92d9b Compare December 25, 2024 15:10
engine.go Show resolved Hide resolved
engine.go Show resolved Hide resolved
server.go Outdated Show resolved Hide resolved
openapi.go Outdated Show resolved Hide resolved
Copy link
Member

@EwenQuim EwenQuim left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice PR! We just need to de-embed the OpenAPIServerConfig field.

@ccoVeille
Copy link
Collaborator

Nice PR! We just need to de-embed the OpenAPIServerConfig field.

I thought 84ee2ff would come with a change on the struct to de-embed it really. Here you de-embedded the way the embedded struct is called.

But maybe I'm missing something

@dylanhitt
Copy link
Collaborator Author

dylanhitt commented Dec 26, 2024

I thought 84ee2ff would come with a change on the struct to de-embed it really. Here you de-embedded the way the embedded struct is called.

Maybe I'm just misunderstanding the ask. I assumed @EwenQuim just wanted it to be structured the way OpenAPIConfig originally was.

The ask was

By doing useless embedding, users will be able to access via s. instead of s.OpenAPIServerConfig. and I think it's not a good thing if ther isn't a good reason to do so (conciseness isn't a good one IMHO).

@ccoVeille
Copy link
Collaborator

ccoVeille commented Dec 26, 2024

No, you are fine. I'm sorry. I missed the fact you de-embedded at the struct level

Screenshot_20241226_210813_GitHub.jpg

@dylanhitt dylanhitt force-pushed the refactor/OutputOpenAPISpec branch from 178e1e9 to 12d3b26 Compare December 27, 2024 12:58
@dylanhitt dylanhitt force-pushed the refactor/OutputOpenAPISpec branch from dabef5f to 95aff1d Compare December 30, 2024 14:39
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

Successfully merging this pull request may close these issues.

3 participants