Mounting handlers and basic swagger docs example

This commit is contained in:
Imbus 2024-03-08 10:16:56 +01:00
parent f75cb7e9fc
commit 13c12b424a
2 changed files with 28 additions and 0 deletions

View file

@ -46,6 +46,17 @@ type GState struct {
ButtonCount int 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 { func (gs *GState) Register(c *fiber.Ctx) error {
u := new(types.User) u := new(types.User)
if err := c.BodyParser(u); err != nil { if err := c.BodyParser(u); err != nil {

View file

@ -3,16 +3,31 @@ package main
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
_ "ttime/docs"
"ttime/internal/config" "ttime/internal/config"
"ttime/internal/database" "ttime/internal/database"
"ttime/internal/handlers" "ttime/internal/handlers"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
"github.com/gofiber/swagger"
_ "github.com/mattn/go-sqlite3" _ "github.com/mattn/go-sqlite3"
jwtware "github.com/gofiber/contrib/jwt" 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() { func main() {
conf, err := config.ReadConfigFromFile("config.toml") conf, err := config.ReadConfigFromFile("config.toml")
if err != nil { if err != nil {
@ -31,6 +46,8 @@ func main() {
// Create the server // Create the server
server := fiber.New() server := fiber.New()
server.Get("/swagger/*", swagger.HandlerDefault)
// Mount our static files (Beware of the security implications of this!) // Mount our static files (Beware of the security implications of this!)
// This will likely be replaced by an embedded filesystem in the future // This will likely be replaced by an embedded filesystem in the future
server.Static("/", "./static") server.Static("/", "./static")