Skip to content

Commit

Permalink
BREAKING: add WithEngineOptions and move WithOpenAPIConfig to a WithE…
Browse files Browse the repository at this point in the history
…ngineOption
  • Loading branch information
dylanhitt committed Dec 24, 2024
1 parent f92b859 commit 141e302
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 124 deletions.
81 changes: 40 additions & 41 deletions engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,16 @@ import (
"path/filepath"
)

func NewEngine(config ...OpenAPIConfig) *Engine {
if len(config) > 1 {
panic("config should not be more than one")
}
engine := &Engine{
func NewEngine(options ...func(*Engine)) *Engine {
e := &Engine{
OpenAPI: NewOpenAPI(),
OpenAPIConfig: defaultOpenAPIConfig,
ErrorHandler: ErrorHandler,
}
if len(config) > 0 {
engine.setOpenAPIConfig(config[0])
for _, option := range options {
option(e)
}
return engine
return e
}

// The Engine is the main struct of the framework.
Expand All @@ -44,6 +41,41 @@ type EngineOpenAPIConfig struct {
PrettyFormatJson bool
}

func WithOpenAPIConfig(config OpenAPIConfig) func(*Engine) {
return func(e *Engine) {
if config.JsonUrl != "" {
e.OpenAPIConfig.JsonUrl = config.JsonUrl
}

if config.SwaggerUrl != "" {
e.OpenAPIConfig.SwaggerUrl = config.SwaggerUrl
}

if config.JsonFilePath != "" {
e.OpenAPIConfig.JsonFilePath = config.JsonFilePath
}

if config.UIHandler != nil {
e.OpenAPIConfig.UIHandler = config.UIHandler
}

e.OpenAPIConfig.DisableSwagger = config.DisableSwagger
e.OpenAPIConfig.DisableSwaggerUI = config.DisableSwaggerUI
e.OpenAPIConfig.DisableLocalSave = config.DisableLocalSave
e.OpenAPIConfig.PrettyFormatJson = config.PrettyFormatJson

if !validateJsonSpecUrl(e.OpenAPIConfig.JsonUrl) {
slog.Error("Error serving openapi json spec. Value of 's.OpenAPIConfig.JsonSpecUrl' option is not valid", "url", e.OpenAPIConfig.JsonUrl)
return
}

if !validateSwaggerUrl(e.OpenAPIConfig.SwaggerUrl) {
slog.Error("Error serving swagger ui. Value of 's.OpenAPIConfig.SwaggerUrl' option is not valid", "url", e.OpenAPIConfig.SwaggerUrl)
return
}
}
}

// OutputOpenAPISpec takes the OpenAPI spec and outputs it to a JSON file
func (e *Engine) OutputOpenAPISpec() []byte {
e.OpenAPI.computeTags()
Expand Down Expand Up @@ -99,39 +131,6 @@ func (s *Engine) marshalSpec() ([]byte, error) {
return json.Marshal(s.OpenAPI.Description())
}

func (e *Engine) setOpenAPIConfig(config OpenAPIConfig) {
if config.JsonUrl != "" {
e.OpenAPIConfig.JsonUrl = config.JsonUrl
}

if config.SwaggerUrl != "" {
e.OpenAPIConfig.SwaggerUrl = config.SwaggerUrl
}

if config.JsonFilePath != "" {
e.OpenAPIConfig.JsonFilePath = config.JsonFilePath
}

if config.UIHandler != nil {
e.OpenAPIConfig.UIHandler = config.UIHandler
}

e.OpenAPIConfig.DisableSwagger = config.DisableSwagger
e.OpenAPIConfig.DisableSwaggerUI = config.DisableSwaggerUI
e.OpenAPIConfig.DisableLocalSave = config.DisableLocalSave
e.OpenAPIConfig.PrettyFormatJson = config.PrettyFormatJson

if !validateJsonSpecUrl(e.OpenAPIConfig.JsonUrl) {
slog.Error("Error serving openapi json spec. Value of 's.OpenAPIConfig.JsonSpecUrl' option is not valid", "url", e.OpenAPIConfig.JsonUrl)
return
}

if !validateSwaggerUrl(e.OpenAPIConfig.SwaggerUrl) {
slog.Error("Error serving swagger ui. Value of 's.OpenAPIConfig.SwaggerUrl' option is not valid", "url", e.OpenAPIConfig.SwaggerUrl)
return
}
}

func (e *Engine) printOpenAPIMessage(msg string) {
if !e.OpenAPIConfig.DisableMessages {
slog.Info(msg)
Expand Down
12 changes: 7 additions & 5 deletions examples/generate-opengraph-image/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ var optionReturnsPNG = func(br *fuego.BaseRoute) {

func main() {
s := fuego.NewServer(
fuego.WithOpenAPIConfig(fuego.OpenAPIConfig{
EngineOpenAPIConfig: fuego.EngineOpenAPIConfig{
PrettyFormatJson: true,
},
}),
fuego.WithEngineOptions(
fuego.WithOpenAPIConfig(fuego.OpenAPIConfig{
EngineOpenAPIConfig: fuego.EngineOpenAPIConfig{
PrettyFormatJson: true,
},
}),
),
)

fuego.GetStd(s, "/{title}", controller.OpenGraphHandler,
Expand Down
14 changes: 8 additions & 6 deletions examples/petstore/lib/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ import (
func TestPetstoreOpenAPIGeneration(t *testing.T) {
server := NewPetStoreServer(
fuego.WithoutStartupMessages(),
fuego.WithOpenAPIConfig(fuego.OpenAPIConfig{
EngineOpenAPIConfig: fuego.EngineOpenAPIConfig{
JsonFilePath: "testdata/doc/openapi.json",
PrettyFormatJson: true,
},
}),
fuego.WithEngineOptions(
fuego.WithOpenAPIConfig(fuego.OpenAPIConfig{
EngineOpenAPIConfig: fuego.EngineOpenAPIConfig{
JsonFilePath: "testdata/doc/openapi.json",
PrettyFormatJson: true,
},
}),
),
)

server.OutputOpenAPISpec()
Expand Down
20 changes: 12 additions & 8 deletions openapi_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,13 @@ func TestUIHandler(t *testing.T) {

t.Run("wrap DefaultOpenAPIHandler behind a middleware", func(t *testing.T) {
s := NewServer(
WithOpenAPIConfig(OpenAPIConfig{
UIHandler: func(specURL string) http.Handler {
return dummyMiddleware(DefaultOpenAPIHandler(specURL))
},
}),
WithEngineOptions(
WithOpenAPIConfig(OpenAPIConfig{
UIHandler: func(specURL string) http.Handler {
return dummyMiddleware(DefaultOpenAPIHandler(specURL))
},
}),
),
)
s.OutputOpenAPISpec()

Expand All @@ -59,9 +61,11 @@ func TestUIHandler(t *testing.T) {

t.Run("disabling UI", func(t *testing.T) {
s := NewServer(
WithOpenAPIConfig(OpenAPIConfig{
DisableSwaggerUI: true,
}),
WithEngineOptions(
WithOpenAPIConfig(OpenAPIConfig{
DisableSwaggerUI: true,
}),
),
)

s.OutputOpenAPISpec()
Expand Down
70 changes: 40 additions & 30 deletions openapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,12 +245,14 @@ func TestServer_OutputOpenApiSpec(t *testing.T) {
docPath := "doc/openapi.json"
t.Run("base", func(t *testing.T) {
s := NewServer(
WithOpenAPIConfig(
OpenAPIConfig{
EngineOpenAPIConfig: EngineOpenAPIConfig{
JsonFilePath: docPath,
WithEngineOptions(
WithOpenAPIConfig(
OpenAPIConfig{
EngineOpenAPIConfig: EngineOpenAPIConfig{
JsonFilePath: docPath,
},
},
},
),
),
)
Get(s, "/", func(ContextNoBody) (MyStruct, error) {
Expand All @@ -268,13 +270,15 @@ func TestServer_OutputOpenApiSpec(t *testing.T) {
})
t.Run("do not print file", func(t *testing.T) {
s := NewServer(
WithOpenAPIConfig(
OpenAPIConfig{
EngineOpenAPIConfig: EngineOpenAPIConfig{
JsonFilePath: docPath,
DisableLocalSave: true,
WithEngineOptions(
WithOpenAPIConfig(
OpenAPIConfig{
EngineOpenAPIConfig: EngineOpenAPIConfig{
JsonFilePath: docPath,
DisableLocalSave: true,
},
},
},
),
),
)
Get(s, "/", func(ContextNoBody) (MyStruct, error) {
Expand All @@ -290,14 +294,16 @@ func TestServer_OutputOpenApiSpec(t *testing.T) {
})
t.Run("swagger disabled", func(t *testing.T) {
s := NewServer(
WithOpenAPIConfig(
OpenAPIConfig{
EngineOpenAPIConfig: EngineOpenAPIConfig{
JsonFilePath: docPath,
DisableLocalSave: true,
WithEngineOptions(
WithOpenAPIConfig(
OpenAPIConfig{
EngineOpenAPIConfig: EngineOpenAPIConfig{
JsonFilePath: docPath,
DisableLocalSave: true,
},
DisableSwagger: true,
},
DisableSwagger: true,
},
),
),
)
Get(s, "/", func(ContextNoBody) (MyStruct, error) {
Expand All @@ -314,13 +320,15 @@ func TestServer_OutputOpenApiSpec(t *testing.T) {
})
t.Run("pretty format json file", func(t *testing.T) {
s := NewServer(
WithOpenAPIConfig(
OpenAPIConfig{
EngineOpenAPIConfig: EngineOpenAPIConfig{
JsonFilePath: docPath,
PrettyFormatJson: true,
WithEngineOptions(
WithOpenAPIConfig(
OpenAPIConfig{
EngineOpenAPIConfig: EngineOpenAPIConfig{
JsonFilePath: docPath,
PrettyFormatJson: true,
},
},
},
),
),
)
Get(s, "/", func(ContextNoBody) (MyStruct, error) {
Expand Down Expand Up @@ -433,12 +441,14 @@ func TestLocalSave(t *testing.T) {

func TestAutoGroupTags(t *testing.T) {
s := NewServer(
WithOpenAPIConfig(OpenAPIConfig{
EngineOpenAPIConfig: EngineOpenAPIConfig{
DisableLocalSave: true,
},
DisableSwagger: true,
}),
WithEngineOptions(
WithOpenAPIConfig(OpenAPIConfig{
EngineOpenAPIConfig: EngineOpenAPIConfig{
DisableLocalSave: true,
},
DisableSwagger: true,
}),
),
)
Get(s, "/a", func(ContextNoBody) (MyStruct, error) {
return MyStruct{}, nil
Expand Down
6 changes: 4 additions & 2 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,9 +365,11 @@ func WithoutLogger() func(*Server) {
}
}

func WithOpenAPIConfig(openapiConfig OpenAPIConfig) func(*Server) {
func WithEngineOptions(options ...func(*Engine)) func(*Server) {
return func(s *Server) {
s.Engine.setOpenAPIConfig(openapiConfig)
for _, option := range options {
option(s.Engine)
}
}
}

Expand Down
Loading

0 comments on commit 141e302

Please sign in to comment.