chore: reorder api package structure
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"orbits-server/internal/api/response"
|
||||
"orbits-server/internal/database"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func RegisterApiRoutes(api *gin.RouterGroup /* env runtime.Environment,*/, db *gorm.DB) {
|
||||
// prefix: api
|
||||
// Display the information on what is going on at the moment
|
||||
api.GET("/command", func(c *gin.Context) {
|
||||
state, err := database.GetState(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,
|
||||
})
|
||||
})
|
||||
|
||||
api.PATCH("/command", func(c *gin.Context) {
|
||||
|
||||
})
|
||||
|
||||
// define a route to check what is registered
|
||||
api.GET("/available", func(c *gin.Context) {
|
||||
files, err := database.GetFiles(db)
|
||||
if err != nil {
|
||||
slog.Error("failed to retrieve available files", "error", err)
|
||||
c.JSON(http.StatusInternalServerError, response.BasicResponse{
|
||||
Msg: response.IntErrMes,
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, response.BasicResponse{
|
||||
Msg: response.OkMes,
|
||||
Data: files,
|
||||
})
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"orbits-server/internal/api/response"
|
||||
"orbits-server/internal/database"
|
||||
"orbits-server/internal/utility"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func RegisterFileRoutes(file *gin.RouterGroup, env utility.Environment, db *gorm.DB) {
|
||||
// /file/<file-name>
|
||||
file.GET("/:filename", func(c *gin.Context) {
|
||||
f := c.Param("filename")
|
||||
p := filepath.Join(env.ContentDirectory, f)
|
||||
|
||||
c.File(p)
|
||||
})
|
||||
|
||||
// define the upload route
|
||||
// /file/upload
|
||||
file.POST("/upload", func(c *gin.Context) {
|
||||
f, err := c.FormFile("file")
|
||||
if err != nil {
|
||||
slog.Debug("no file or file headers provided on the request", "error", err)
|
||||
c.JSON(http.StatusBadRequest, response.BasicResponse{
|
||||
Msg: "a file is required",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
readerStream, err := f.Open()
|
||||
if err != nil {
|
||||
slog.Error("failed to a reader stream")
|
||||
}
|
||||
defer readerStream.Close()
|
||||
|
||||
fileData, err := database.BuildFileRecord(readerStream, f.Filename, env.ContentDirectory)
|
||||
if err != nil {
|
||||
slog.Error("failed to enroll file to the database", "error", err)
|
||||
c.JSON(http.StatusInternalServerError, response.BasicResponse{
|
||||
Msg: response.IntErrMes,
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
if err := database.RegisterFile(db, fileData); err != nil {
|
||||
if errors.Is(err, gorm.ErrDuplicatedKey) {
|
||||
slog.Debug("discarding file since its checksum is a duplicate", "error", err)
|
||||
c.JSON(http.StatusConflict, response.BasicResponse{
|
||||
Msg: "file checksum already exists",
|
||||
})
|
||||
} else {
|
||||
slog.Error("failed to insert filedata to the database", "error", err)
|
||||
c.JSON(http.StatusInternalServerError, response.BasicResponse{
|
||||
Msg: response.IntErrMes,
|
||||
})
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// save to filesystem after everything has given a green light
|
||||
if err := c.SaveUploadedFile(f, fileData.FilePath); err != nil {
|
||||
slog.Error("failed to receive the file over http:", "error", err)
|
||||
c.JSON(http.StatusInternalServerError, response.BasicResponse{
|
||||
Msg: response.IntErrMes,
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
slog.Info("saved file to local filesystem and database")
|
||||
c.JSON(http.StatusCreated, response.BasicResponse{
|
||||
Msg: "file has succesfully been uploaded",
|
||||
Data: fileData,
|
||||
})
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user