Files
orbits/internal/api/api.go
T
2026-04-23 14:57:02 +02:00

52 lines
1.2 KiB
Go

package api
import (
"fmt"
"log/slog"
"orbits-server/internal/api/middleware"
"orbits-server/internal/utility"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
)
const (
okMes string = "OK"
ieMes string = "An internal error occured, contact your administrator"
)
type RespObj struct {
Msg string `json:"msg"`
Data any `json:"data"`
}
// All error messages from slog must have an error field with the golang error
// See bottom the the kickoff function for details
func KickoffApi(logger *slog.Logger, env utility.Environment, db *gorm.DB) {
gin.SetMode(gin.ReleaseMode)
// For a nice looking logger:
// r := gin.Default()
// JSON logger: https://gin-gonic.com/en/docs/logging/structured-logging/
r := gin.New()
r.Use(middleware.SlogMiddleware(logger))
r.Use(gin.Recovery())
api := r.Group("/api")
spawnApiRoutes(api /*env,*/, db)
file := r.Group("/file")
spawnFileRoutes(file, env, db)
r.Static("/assets", "./web/frontend/dist/assets")
r.NoRoute(func(c *gin.Context) {
c.File("./web/frontend/dist/index.html")
})
err := r.Run(fmt.Sprintf("%s:%d", env.Hostname, env.Port))
if err != nil {
slog.Error("failed to start the Gin server", "error", err)
}
}