51 lines
1.1 KiB
Go
51 lines
1.1 KiB
Go
package api
|
|
|
|
import (
|
|
"fmt"
|
|
"log/slog"
|
|
"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(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)
|
|
}
|
|
}
|