From 13c12b424a34fddd24184be47234ef0102b8703b Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Fri, 8 Mar 2024 10:16:56 +0100 Subject: [PATCH] Mounting handlers and basic swagger docs example --- backend/internal/handlers/global_state.go | 11 +++++++++++ backend/main.go | 17 +++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/backend/internal/handlers/global_state.go b/backend/internal/handlers/global_state.go index ffe2072..4c642ff 100644 --- a/backend/internal/handlers/global_state.go +++ b/backend/internal/handlers/global_state.go @@ -46,6 +46,17 @@ type GState struct { ButtonCount int } +// Register is a simple handler that registers a new user +// +// @Summary Register a new user +// @Description Register a new user +// @Tags User +// @Accept json +// @Produce json +// @Success 200 {string} string "User added" +// @Failure 400 {string} string "Bad request" +// @Failure 500 {string} string "Internal server error" +// @Router /api/register [post] func (gs *GState) Register(c *fiber.Ctx) error { u := new(types.User) if err := c.BodyParser(u); err != nil { diff --git a/backend/main.go b/backend/main.go index bae7a83..d7713c9 100644 --- a/backend/main.go +++ b/backend/main.go @@ -3,16 +3,31 @@ package main import ( "encoding/json" "fmt" + _ "ttime/docs" "ttime/internal/config" "ttime/internal/database" "ttime/internal/handlers" "github.com/gofiber/fiber/v2" + "github.com/gofiber/swagger" _ "github.com/mattn/go-sqlite3" jwtware "github.com/gofiber/contrib/jwt" ) +// @title TTime API +// @version 0.0.1 +// @description This is the API for TTime, a time tracking application. + +// @license.name AGPL +// @license.url https://www.gnu.org/licenses/agpl-3.0.html + +// @host localhost:8080 +// @BasePath /api + +// @externalDocs.description OpenAPI +// @externalDocs.url https://swagger.io/resources/open-api/ + func main() { conf, err := config.ReadConfigFromFile("config.toml") if err != nil { @@ -31,6 +46,8 @@ func main() { // Create the server server := fiber.New() + server.Get("/swagger/*", swagger.HandlerDefault) + // Mount our static files (Beware of the security implications of this!) // This will likely be replaced by an embedded filesystem in the future server.Static("/", "./static")