chore: reorder api package structure
This commit is contained in:
+4
-12
@@ -3,23 +3,15 @@ package api
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
|
|
||||||
"orbits-server/internal/api/middleware"
|
"orbits-server/internal/api/middleware"
|
||||||
|
"orbits-server/internal/api/routes"
|
||||||
"orbits-server/internal/utility"
|
"orbits-server/internal/utility"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"gorm.io/gorm"
|
"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
|
// All error messages from slog must have an error field with the golang error
|
||||||
// See bottom the the kickoff function for details
|
// See bottom the the kickoff function for details
|
||||||
|
|
||||||
@@ -34,10 +26,10 @@ func KickoffApi(logger *slog.Logger, env utility.Environment, db *gorm.DB) {
|
|||||||
r.Use(gin.Recovery())
|
r.Use(gin.Recovery())
|
||||||
|
|
||||||
api := r.Group("/api")
|
api := r.Group("/api")
|
||||||
spawnApiRoutes(api /*env,*/, db)
|
routes.RegisterApiRoutes(api /*env,*/, db)
|
||||||
|
|
||||||
file := r.Group("/file")
|
file := r.Group("/file")
|
||||||
spawnFileRoutes(file, env, db)
|
routes.RegisterFileRoutes(file, env, db)
|
||||||
|
|
||||||
r.Static("/assets", "./web/frontend/dist/assets")
|
r.Static("/assets", "./web/frontend/dist/assets")
|
||||||
r.NoRoute(func(c *gin.Context) {
|
r.NoRoute(func(c *gin.Context) {
|
||||||
|
|||||||
@@ -3,6 +3,8 @@ package middleware
|
|||||||
import (
|
import (
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"orbits-server/internal/api/response"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
@@ -36,11 +38,23 @@ func SlogMiddleware(logger *slog.Logger) gin.HandlerFunc {
|
|||||||
|
|
||||||
func AuthMiddleware() gin.HandlerFunc {
|
func AuthMiddleware() gin.HandlerFunc {
|
||||||
return func(c *gin.Context) {
|
return func(c *gin.Context) {
|
||||||
orbitsKey := c.GetHeader("orbits-key")
|
authorizationHeader := c.GetHeader("Authorization")
|
||||||
if len(orbitsKey) == 0 {
|
if len(authorizationHeader) == 0 {
|
||||||
c.AbortWithStatus(http.StatusUnauthorized)
|
c.AbortWithStatusJSON(http.StatusUnauthorized, response.BasicResponse{
|
||||||
|
Msg: "Authorization header is required",
|
||||||
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
headerParts := strings.Split(authorizationHeader, " ")
|
||||||
|
if len(headerParts) != 2 || headerParts[0] != "Bearer" {
|
||||||
|
c.AbortWithStatusJSON(http.StatusUnauthorized, response.BasicResponse{
|
||||||
|
Msg: "Authorization header is invalid",
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
givenKey := headerParts[1]
|
||||||
|
slog.Info(givenKey)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,11 @@
|
|||||||
|
package response
|
||||||
|
|
||||||
|
const (
|
||||||
|
OkMes string = "OK"
|
||||||
|
IntErrMes string = "An internal error occured, contact your administrator"
|
||||||
|
)
|
||||||
|
|
||||||
|
type BasicResponse struct {
|
||||||
|
Msg string `json:"msg"`
|
||||||
|
Data any `json:"data"`
|
||||||
|
}
|
||||||
@@ -1,29 +1,30 @@
|
|||||||
package api
|
package routes
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"orbits-server/internal/api/response"
|
||||||
"orbits-server/internal/database"
|
"orbits-server/internal/database"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
func spawnApiRoutes(api *gin.RouterGroup /* env runtime.Environment,*/, db *gorm.DB) {
|
func RegisterApiRoutes(api *gin.RouterGroup /* env runtime.Environment,*/, db *gorm.DB) {
|
||||||
// prefix: api
|
// prefix: api
|
||||||
// Display the information on what is going on at the moment
|
// Display the information on what is going on at the moment
|
||||||
api.GET("/command", func(c *gin.Context) {
|
api.GET("/command", func(c *gin.Context) {
|
||||||
state, err := database.GetState(db)
|
state, err := database.GetState(db)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Error("unable to determine state", "error", err)
|
slog.Error("unable to determine state", "error", err)
|
||||||
c.JSON(http.StatusInternalServerError, RespObj{
|
c.JSON(http.StatusInternalServerError, response.BasicResponse{
|
||||||
Msg: ieMes,
|
Msg: response.IntErrMes,
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
c.JSON(http.StatusOK, RespObj{
|
c.JSON(http.StatusOK, response.BasicResponse{
|
||||||
Msg: okMes,
|
Msg: response.OkMes,
|
||||||
Data: state,
|
Data: state,
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
@@ -37,14 +38,14 @@ func spawnApiRoutes(api *gin.RouterGroup /* env runtime.Environment,*/, db *gorm
|
|||||||
files, err := database.GetFiles(db)
|
files, err := database.GetFiles(db)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Error("failed to retrieve available files", "error", err)
|
slog.Error("failed to retrieve available files", "error", err)
|
||||||
c.JSON(http.StatusInternalServerError, RespObj{
|
c.JSON(http.StatusInternalServerError, response.BasicResponse{
|
||||||
Msg: ieMes,
|
Msg: response.IntErrMes,
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
c.JSON(http.StatusOK, RespObj{
|
c.JSON(http.StatusOK, response.BasicResponse{
|
||||||
Msg: okMes,
|
Msg: response.OkMes,
|
||||||
Data: files,
|
Data: files,
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
@@ -1,9 +1,10 @@
|
|||||||
package api
|
package routes
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"orbits-server/internal/api/response"
|
||||||
"orbits-server/internal/database"
|
"orbits-server/internal/database"
|
||||||
"orbits-server/internal/utility"
|
"orbits-server/internal/utility"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
@@ -12,7 +13,7 @@ import (
|
|||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
func spawnFileRoutes(file *gin.RouterGroup, env utility.Environment, db *gorm.DB) {
|
func RegisterFileRoutes(file *gin.RouterGroup, env utility.Environment, db *gorm.DB) {
|
||||||
// /file/<file-name>
|
// /file/<file-name>
|
||||||
file.GET("/:filename", func(c *gin.Context) {
|
file.GET("/:filename", func(c *gin.Context) {
|
||||||
f := c.Param("filename")
|
f := c.Param("filename")
|
||||||
@@ -27,7 +28,7 @@ func spawnFileRoutes(file *gin.RouterGroup, env utility.Environment, db *gorm.DB
|
|||||||
f, err := c.FormFile("file")
|
f, err := c.FormFile("file")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Debug("no file or file headers provided on the request", "error", err)
|
slog.Debug("no file or file headers provided on the request", "error", err)
|
||||||
c.JSON(http.StatusBadRequest, RespObj{
|
c.JSON(http.StatusBadRequest, response.BasicResponse{
|
||||||
Msg: "a file is required",
|
Msg: "a file is required",
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
@@ -42,8 +43,8 @@ func spawnFileRoutes(file *gin.RouterGroup, env utility.Environment, db *gorm.DB
|
|||||||
fileData, err := database.BuildFileRecord(readerStream, f.Filename, env.ContentDirectory)
|
fileData, err := database.BuildFileRecord(readerStream, f.Filename, env.ContentDirectory)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Error("failed to enroll file to the database", "error", err)
|
slog.Error("failed to enroll file to the database", "error", err)
|
||||||
c.JSON(http.StatusInternalServerError, RespObj{
|
c.JSON(http.StatusInternalServerError, response.BasicResponse{
|
||||||
Msg: ieMes,
|
Msg: response.IntErrMes,
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -51,13 +52,13 @@ func spawnFileRoutes(file *gin.RouterGroup, env utility.Environment, db *gorm.DB
|
|||||||
if err := database.RegisterFile(db, fileData); err != nil {
|
if err := database.RegisterFile(db, fileData); err != nil {
|
||||||
if errors.Is(err, gorm.ErrDuplicatedKey) {
|
if errors.Is(err, gorm.ErrDuplicatedKey) {
|
||||||
slog.Debug("discarding file since its checksum is a duplicate", "error", err)
|
slog.Debug("discarding file since its checksum is a duplicate", "error", err)
|
||||||
c.JSON(http.StatusConflict, RespObj{
|
c.JSON(http.StatusConflict, response.BasicResponse{
|
||||||
Msg: "file checksum already exists",
|
Msg: "file checksum already exists",
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
slog.Error("failed to insert filedata to the database", "error", err)
|
slog.Error("failed to insert filedata to the database", "error", err)
|
||||||
c.JSON(http.StatusInternalServerError, RespObj{
|
c.JSON(http.StatusInternalServerError, response.BasicResponse{
|
||||||
Msg: ieMes,
|
Msg: response.IntErrMes,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
@@ -66,14 +67,14 @@ func spawnFileRoutes(file *gin.RouterGroup, env utility.Environment, db *gorm.DB
|
|||||||
// save to filesystem after everything has given a green light
|
// save to filesystem after everything has given a green light
|
||||||
if err := c.SaveUploadedFile(f, fileData.FilePath); err != nil {
|
if err := c.SaveUploadedFile(f, fileData.FilePath); err != nil {
|
||||||
slog.Error("failed to receive the file over http:", "error", err)
|
slog.Error("failed to receive the file over http:", "error", err)
|
||||||
c.JSON(http.StatusInternalServerError, RespObj{
|
c.JSON(http.StatusInternalServerError, response.BasicResponse{
|
||||||
Msg: ieMes,
|
Msg: response.IntErrMes,
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
slog.Info("saved file to local filesystem and database")
|
slog.Info("saved file to local filesystem and database")
|
||||||
c.JSON(http.StatusCreated, RespObj{
|
c.JSON(http.StatusCreated, response.BasicResponse{
|
||||||
Msg: "file has succesfully been uploaded",
|
Msg: "file has succesfully been uploaded",
|
||||||
Data: fileData,
|
Data: fileData,
|
||||||
})
|
})
|
||||||
@@ -36,15 +36,13 @@ type Command struct {
|
|||||||
Timestamps
|
Timestamps
|
||||||
}
|
}
|
||||||
|
|
||||||
type Key struct {
|
type AccessKey struct {
|
||||||
ID int `gorm:"primaryKey;not null;"`
|
ID int `gorm:"primaryKey;not null;"`
|
||||||
MetaName string
|
MetaName string
|
||||||
KeyName string `gorm:"not null;"`
|
KeyName string `gorm:"not null;"`
|
||||||
// We don't store the key itself, we hash the key
|
// We don't store the key itself, we hash the key
|
||||||
KeyHash string `gorm:"not null;"`
|
KeyHash string `gorm:"not null;"`
|
||||||
// we're cooking without pepper
|
// we're cooking without pepper
|
||||||
KeySalt string `gorm:"not null;"`
|
|
||||||
CreatedAt time.Time `gorm:"not null;"`
|
|
||||||
Timestamps
|
Timestamps
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user