Added swagger support

This commit is contained in:
2023-07-22 15:50:06 +02:00
parent d49ca4fa68
commit b31c00ed70
5 changed files with 44 additions and 8 deletions

View File

@ -3,7 +3,7 @@ package api
import (
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/helmet"
"{{cookiecutter.module_path}}/internal/app/api/v1"
v1 "{{cookiecutter.module_path}}/internal/app/api/v1"
)
func ConfigureRoutes(app *fiber.App) error {

View File

@ -5,14 +5,21 @@ import (
"github.com/gofiber/fiber/v2/middleware/requestid"
)
// @Summary Endpoint for testing latency
// @Description Endpoint for testing latency
// @Tags Health
// @Success 200 {string} string
// @Router /api/v1/ping [get]
func Ping(c *fiber.Ctx) error {
return c.SendString("pong")
}
func ConfigureRoutes(router fiber.Router) error {
v1 := router.Group("/v1")
v1.Use(requestid.New())
v1.Get("/ping", func(c *fiber.Ctx) error {
return c.SendString("pong")
})
v1.Get("/ping", Ping)
return nil
}

View File

@ -5,9 +5,19 @@ import (
"github.com/gofiber/fiber/v2/middleware/compress"
"github.com/gofiber/fiber/v2/middleware/helmet"
"github.com/gofiber/fiber/v2/middleware/logger"
"github.com/gofiber/swagger"
_ "{{cookiecutter.module_path}}/docs"
"{{cookiecutter.module_path}}/internal/app/api"
)
// @title {{cookiecutter.project_name}} API
// @version 1.0
// @description Documentation for the {{cookiecutter.project_name}} API.
// @securityDefinitions.apikey ApiKeyAuth
// @in header
// @name Authorization
// @description Token used to authenticate against the API.
func Run() error {
app := fiber.New()
@ -15,6 +25,11 @@ func Run() error {
app.Use(compress.New())
app.Use(helmet.New())
// Prefork is only enabled in prod-mode.
if !app.Config().Prefork {
app.Get("/swagger/*", swagger.HandlerDefault)
}
api.ConfigureRoutes(app)
return app.Listen(":3000")

View File

@ -34,4 +34,7 @@ stop: ## Stop the container
start: ## Start the container
podman start {{project_name}}
swagger: ## Generate swagger documentation
swag init -d internal/app
{%- endraw %}