From b31c00ed70ec4823b9709b58dd5f8b6c1ee2e3d3 Mon Sep 17 00:00:00 2001 From: niku Date: Sat, 22 Jul 2023 15:50:06 +0200 Subject: [PATCH] Added swagger support --- hooks/post_gen_project.py | 19 +++++++++++++++---- .../internal/app/api/router.go | 2 +- .../internal/app/api/v1/router.go | 13 ++++++++++--- .../internal/app/main.go | 15 +++++++++++++++ {{cookiecutter.project_name}}/justfile | 3 +++ 5 files changed, 44 insertions(+), 8 deletions(-) diff --git a/hooks/post_gen_project.py b/hooks/post_gen_project.py index 979b8d1..cb3b7df 100644 --- a/hooks/post_gen_project.py +++ b/hooks/post_gen_project.py @@ -1,14 +1,25 @@ -import subprocess +from subprocess import run GO_PACKAGES = [ - "github.com/gofiber/fiber/v2" + "github.com/gofiber/fiber/v2", + "github.com/gofiber/swagger", +] + +GO_INSTALL = [ + "github.com/swaggo/swag/cmd/swag@latest" ] def main() -> int: - subprocess.run(["git", "init"]) + run(["git", "init"]) for package in GO_PACKAGES: - subprocess.run(["go", "get", "-u", package]) + run(["go", "get", "-u", package]) + + for package in GO_INSTALL: + run(["go", "install", package]) + + run(["swag", "init", "-d", "internal/app"]) + return 0 if __name__ == "__main__": diff --git a/{{cookiecutter.project_name}}/internal/app/api/router.go b/{{cookiecutter.project_name}}/internal/app/api/router.go index 18384e2..b93ce8f 100644 --- a/{{cookiecutter.project_name}}/internal/app/api/router.go +++ b/{{cookiecutter.project_name}}/internal/app/api/router.go @@ -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 { diff --git a/{{cookiecutter.project_name}}/internal/app/api/v1/router.go b/{{cookiecutter.project_name}}/internal/app/api/v1/router.go index 2c2c860..ea45118 100644 --- a/{{cookiecutter.project_name}}/internal/app/api/v1/router.go +++ b/{{cookiecutter.project_name}}/internal/app/api/v1/router.go @@ -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 } diff --git a/{{cookiecutter.project_name}}/internal/app/main.go b/{{cookiecutter.project_name}}/internal/app/main.go index 4ec4d6e..4825f76 100644 --- a/{{cookiecutter.project_name}}/internal/app/main.go +++ b/{{cookiecutter.project_name}}/internal/app/main.go @@ -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") diff --git a/{{cookiecutter.project_name}}/justfile b/{{cookiecutter.project_name}}/justfile index 9a321f9..48aaf75 100644 --- a/{{cookiecutter.project_name}}/justfile +++ b/{{cookiecutter.project_name}}/justfile @@ -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 %}