feat: add new features such as database watchdog
This commit is contained in:
+28
-5
@@ -9,19 +9,42 @@ import (
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func KickoffApi(env runtime.Environment, db *gorm.DB) {
|
||||
r := gin.Default()
|
||||
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 runtime.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")
|
||||
spawnRoutes(api, env, db)
|
||||
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:%s", env.Hostname, env.Port))
|
||||
err := r.Run(fmt.Sprintf("%s:%d", env.Hostname, env.Port))
|
||||
if err != nil {
|
||||
slog.Error("failed to start the Gin server due to: " + err.Error())
|
||||
slog.Error("failed to start the Gin server", "error", err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func slogMiddleware(logger *slog.Logger) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
start := time.Now()
|
||||
path := c.Request.URL.Path
|
||||
query := c.Request.URL.RawQuery
|
||||
|
||||
c.Next()
|
||||
|
||||
logger.Info("request",
|
||||
slog.String("method", c.Request.Method),
|
||||
slog.String("path", path),
|
||||
slog.String("query", query),
|
||||
slog.Int("status", c.Writer.Status()),
|
||||
slog.Duration("latency", time.Since(start)),
|
||||
slog.String("client_ip", c.ClientIP()),
|
||||
slog.Int("body_size", c.Writer.Size()),
|
||||
)
|
||||
|
||||
if len(c.Errors) > 0 {
|
||||
for _, err := range c.Errors {
|
||||
logger.Error("request error", slog.String("error", err.Error()))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,87 +0,0 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"eden-server/internal/database"
|
||||
"eden-server/internal/runtime"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// 0: unspecified
|
||||
// 1: video
|
||||
// 2: presentation
|
||||
// 3: internet URL
|
||||
func categorizeFile(ext string) string {
|
||||
switch ext {
|
||||
case ".mp4", ".mov", ".avi", ".mkv", ".webm", ".m4a":
|
||||
return "video"
|
||||
case ".pptx", ".ppt", ".key", ".odp":
|
||||
return "presentation"
|
||||
default:
|
||||
return "unspecified"
|
||||
}
|
||||
}
|
||||
|
||||
func spawnRoutes(api *gin.RouterGroup, env runtime.Environment, db *gorm.DB) {
|
||||
// The Root endpoint '/' displays a simple HTML template.
|
||||
// from root: ./templates/index.html
|
||||
api.GET("/", func(c *gin.Context) {
|
||||
c.HTML(http.StatusOK, "index.html",
|
||||
gin.H{
|
||||
"title": env.Codename,
|
||||
"version": env.Version,
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
// prefix: api
|
||||
// Display the information on what is going on at the moment
|
||||
api.GET("/api/status", func(c *gin.Context) {
|
||||
state, err := database.GetAppState(db)
|
||||
if err != nil {
|
||||
slog.Warn("unable to determine state")
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, state)
|
||||
})
|
||||
|
||||
// define the upload route
|
||||
api.POST("/api/upload", func(c *gin.Context) {
|
||||
file, err := c.FormFile("file")
|
||||
if err != nil {
|
||||
slog.Error("missing file upload", "error", err)
|
||||
c.JSON(http.StatusBadRequest, gin.H{"msg": "file is required"})
|
||||
return
|
||||
}
|
||||
|
||||
ext := filepath.Ext(file.Filename)
|
||||
catName := categorizeFile(ext)
|
||||
safeName := uuid.New().String()[:8] + "_" + catName + ext
|
||||
destPath := filepath.Join(env.WorkDir, "content", "fresh", safeName)
|
||||
|
||||
if err := c.SaveUploadedFile(file, destPath); err != nil {
|
||||
slog.Error("failed to receive the file over http:", "error", err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"msg": "internal error"})
|
||||
} else {
|
||||
database.RegisterFile(db, catName, destPath)
|
||||
c.JSON(http.StatusCreated, gin.H{"msg": "file has succesfully been uploaded"})
|
||||
}
|
||||
})
|
||||
|
||||
// define a route to check what is registered
|
||||
api.GET("/api/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, gin.H{"msg": "internal error"})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, files)
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"eden-server/internal/database"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// 0: unspecified
|
||||
// 1: video
|
||||
// 2: presentation
|
||||
// 3: internet URL
|
||||
func categorizeFilemode(ext string) database.Mode {
|
||||
switch ext {
|
||||
case ".mp4", ".mov", ".avi", ".mkv", ".webm", ".m4a":
|
||||
return database.ModeVideo
|
||||
case ".pptx", ".ppt", ".key", ".odp":
|
||||
return database.ModePresentation
|
||||
default:
|
||||
return database.ModeUnspecified
|
||||
}
|
||||
}
|
||||
|
||||
func spawnApiRoutes(api *gin.RouterGroup /* env runtime.Environment,*/, db *gorm.DB) {
|
||||
// prefix: api
|
||||
// Display the information on what is going on at the moment
|
||||
api.GET("/status", func(c *gin.Context) {
|
||||
state, err := database.GetState(db)
|
||||
if err != nil {
|
||||
slog.Error("unable to determine state", "error", err)
|
||||
c.JSON(http.StatusInternalServerError, RespObj{
|
||||
Msg: ieMes,
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, RespObj{
|
||||
Msg: okMes,
|
||||
Data: state,
|
||||
})
|
||||
})
|
||||
|
||||
// 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, RespObj{
|
||||
Msg: ieMes,
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, RespObj{
|
||||
Msg: okMes,
|
||||
Data: files,
|
||||
})
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"eden-server/internal/crypto"
|
||||
"eden-server/internal/database"
|
||||
"eden-server/internal/runtime"
|
||||
"log"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func spawnFileRoutes(file *gin.RouterGroup, env runtime.Environment, db *gorm.DB) {
|
||||
// /file/<file-name>
|
||||
file.GET("/:filename", func(c *gin.Context) {
|
||||
f := c.Param("filename")
|
||||
p := filepath.Join(env.DataDirectory, "content", 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.Error("failed to get file details from request", "error", err)
|
||||
c.JSON(http.StatusBadRequest, RespObj{
|
||||
Msg: "a file is required",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
e := filepath.Ext(f.Filename)
|
||||
m := categorizeFilemode(e)
|
||||
if m == database.ModeUnspecified {
|
||||
slog.Warn("discarding file since its filetype is unsupported")
|
||||
c.JSON(http.StatusUnsupportedMediaType, RespObj{
|
||||
Msg: "unsupported filetype",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
safeName := uuid.New().String()[:8] + "_" + string(m) + e
|
||||
destPath := filepath.Join(env.DataDirectory, "content", safeName)
|
||||
|
||||
if err := c.SaveUploadedFile(f, destPath); err != nil {
|
||||
slog.Error("failed to receive the file over http:", "error", err)
|
||||
c.JSON(http.StatusInternalServerError, RespObj{
|
||||
Msg: ieMes,
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
cSum, err := crypto.CalculateHash(destPath)
|
||||
if err != nil {
|
||||
slog.Error("failed to calculate hash of file at given path", "error", err)
|
||||
c.JSON(http.StatusInternalServerError, RespObj{
|
||||
Msg: ieMes,
|
||||
})
|
||||
}
|
||||
log.Println(cSum)
|
||||
|
||||
fData := database.File{
|
||||
Mode: m,
|
||||
GivenName: f.Filename,
|
||||
Filepath: destPath,
|
||||
Checksum: cSum,
|
||||
}
|
||||
database.RegisterFile(db, fData)
|
||||
c.JSON(http.StatusCreated, RespObj{
|
||||
Msg: "file has succesfully been uploaded",
|
||||
})
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user