From ca242a55f8251c7c3efd55dee1cd24b95de6adc1 Mon Sep 17 00:00:00 2001 From: Marek Goc Date: Tue, 11 Apr 2023 23:33:33 +0200 Subject: [PATCH] enable or disable swagger --- fibers.go | 66 +++++++++++++++++++++++++--------------------- swagger/swagger.go | 2 ++ 2 files changed, 38 insertions(+), 30 deletions(-) diff --git a/fibers.go b/fibers.go index 426cf2d..eefaf87 100644 --- a/fibers.go +++ b/fibers.go @@ -103,39 +103,45 @@ func (g *App) init() { if g.Swagger == nil { return } - g.App.Get(g.fullPath(g.Swagger.OpenAPIUrl), func(c *fiber.Ctx) error { - return c.JSON(g.Swagger) - }) - g.App.Get(g.fullPath(g.Swagger.DocsUrl), func(c *fiber.Ctx) error { - options := `{}` - if g.Swagger.SwaggerOptions != nil { - data, err := json.Marshal(g.Swagger.SwaggerOptions) - if err != nil { - log.Panic(err) - } - options = string(data) - } - return c.Render("templates/swagger", fiber.Map{ - "openapi_url": g.fullPath(g.Swagger.OpenAPIUrl), - "title": g.Swagger.Title, - "swagger_options": options, + if g.Swagger.SwaggerEnable || g.Swagger.RedocEnable { + g.App.Get(g.fullPath(g.Swagger.OpenAPIUrl), func(c *fiber.Ctx) error { + return c.JSON(g.Swagger) }) - }) - g.App.Get(g.fullPath(g.Swagger.RedocUrl), func(c *fiber.Ctx) error { - options := `{}` - if g.Swagger.RedocOptions != nil { - data, err := json.Marshal(g.Swagger.RedocOptions) - if err != nil { - log.Panic(err) + } + if g.Swagger.SwaggerEnable { + g.App.Get(g.fullPath(g.Swagger.DocsUrl), func(c *fiber.Ctx) error { + options := `{}` + if g.Swagger.SwaggerOptions != nil { + data, err := json.Marshal(g.Swagger.SwaggerOptions) + if err != nil { + log.Panic(err) + } + options = string(data) } - options = string(data) - } - return c.Render("templates/redoc", fiber.Map{ - "openapi_url": g.fullPath(g.Swagger.OpenAPIUrl), - "title": g.Swagger.Title, - "redoc_options": options, + return c.Render("templates/swagger", fiber.Map{ + "openapi_url": g.fullPath(g.Swagger.OpenAPIUrl), + "title": g.Swagger.Title, + "swagger_options": options, + }) }) - }) + } + if g.Swagger.RedocEnable { + g.App.Get(g.fullPath(g.Swagger.RedocUrl), func(c *fiber.Ctx) error { + options := `{}` + if g.Swagger.RedocOptions != nil { + data, err := json.Marshal(g.Swagger.RedocOptions) + if err != nil { + log.Panic(err) + } + options = string(data) + } + return c.Render("templates/redoc", fiber.Map{ + "openapi_url": g.fullPath(g.Swagger.OpenAPIUrl), + "title": g.Swagger.Title, + "redoc_options": options, + }) + }) + } g.initRouters() g.Swagger.BuildOpenAPI() } diff --git a/swagger/swagger.go b/swagger/swagger.go index ea5c7f9..5aa157b 100644 --- a/swagger/swagger.go +++ b/swagger/swagger.go @@ -33,6 +33,8 @@ type Swagger struct { Contact *openapi3.Contact License *openapi3.License OpenAPI *openapi3.T + SwaggerEnable bool + RedocEnable bool SwaggerOptions map[string]interface{} RedocOptions map[string]interface{} }