feat: add services and separate further

This commit is contained in:
2026-04-29 13:25:38 +02:00
parent 6c28aea655
commit 8f6b1efea0
16 changed files with 318 additions and 168 deletions
+2 -88
View File
@@ -1,102 +1,16 @@
package routes
import (
"log/slog"
"net/http"
"orbits-server/internal/server/api/response"
"orbits-server/internal/server/database"
"orbits-server/internal/shared/security"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
)
const (
accessKeyLen = 32
)
func RegisterApiRoutes(api *gin.RouterGroup, db *gorm.DB) {
// prefix: api
// define subroute with key
// /api/key
key := api.Group("/key")
/*
key.GET("/:key", func(c *gin.Context) {
})
*/
key.POST("/create", func(c *gin.Context) {
var body keyRequestBody
err := c.ShouldBindBodyWithJSON(&body)
if err != nil {
slog.Error("failed to bind body to json", "error", err)
c.JSON(http.StatusBadRequest, response.BasicResponse{
Msg: "invalid JSON",
})
return
}
keyContent := security.GenerateChars(accessKeyLen)
hash, err := security.HashKey(keyContent)
if err != nil {
slog.Error("failed to generate a hash for the key", "error", err)
c.JSON(http.StatusInternalServerError, response.BasicResponse{
Msg: response.IntErrMes,
})
return
}
keyRecord := database.BuildKeyRecord(hash, body.Name, body.ExpiresAt)
if err := database.CreateKey(db, &keyRecord); err != nil {
slog.Error("failed to insert key into the database", "error", err)
c.JSON(http.StatusInternalServerError, response.BasicResponse{
Msg: response.IntErrMes,
})
return
}
slog.Info("saved key to database")
c.JSON(http.StatusCreated, response.BasicResponse{
Msg: "key has succesfully been created and saved",
Data: keyContent,
})
})
key.GET("/verify", func(c *gin.Context) {
api.GET("/version", func(c *gin.Context) {
})
key.DELETE("/:key", func(c *gin.Context) {
})
// define the control route on the api
// /api/control
ctl := api.Group("/control")
// Display the information on what is going on at the moment
ctl.GET("/command", func(c *gin.Context) {
state, err := database.LatestState(db)
if err != nil {
slog.Error("unable to determine state", "error", err)
c.JSON(http.StatusInternalServerError, response.BasicResponse{
Msg: response.IntErrMes,
})
return
}
c.JSON(http.StatusOK, response.BasicResponse{
Msg: response.OkMes,
Data: state,
})
})
ctl.PATCH("/command", func(c *gin.Context) {
api.GET("/codename", func(c *gin.Context) {
})
}