feat: add new features such as database watchdog

This commit is contained in:
DaanSelen
2026-04-21 16:27:04 +02:00
parent 610fdffdb8
commit c4a4fafb52
16 changed files with 428 additions and 158 deletions
+28 -5
View File
@@ -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)
}
}
+34
View File
@@ -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()))
}
}
}
}
-87
View File
@@ -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)
})
}
+62
View File
@@ -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,
})
})
}
+79
View File
@@ -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",
})
})
}
+27
View File
@@ -0,0 +1,27 @@
package crypto
import (
"crypto/sha512"
"encoding/hex"
"io"
"os"
)
func CalculateHash(p string) (string, error) {
f, err := os.Open(p)
if err != nil {
return "", err
}
defer f.Close()
h := sha512.New()
if _, err := io.Copy(h, f); err != nil {
return "", err
}
sum := h.Sum(nil)
// return the sha checksum in hex
return hex.EncodeToString(sum), nil
// alternatively return in base64
//return base64.StdEncoding.EncodeToString(sum), nil
}
+42 -13
View File
@@ -1,36 +1,65 @@
package database
import (
"eden-server/internal/runtime"
"path/filepath"
"time"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"gorm.io/gorm/schema"
)
func KickoffDatabase() (*gorm.DB, error) {
db, err := gorm.Open(sqlite.Open("garden.db"), &gorm.Config{
NamingStrategy: schema.NamingStrategy{
SingularTable: true,
},
})
//var watchdogStop = make(chan struct{})
func KickoffDatabase(workDir string) (*gorm.DB, error) {
dbLoc := filepath.Join(workDir, "data", "garden.db")
db, err := gorm.Open(sqlite.Open(dbLoc), &gorm.Config{})
if err != nil {
return nil, err
}
if err := db.AutoMigrate(&AppState{}); err != nil {
if err := db.AutoMigrate(&State{}); err != nil {
return nil, err
}
if err := db.AutoMigrate(&Files{}); err != nil {
if err := db.AutoMigrate(&File{}); err != nil {
return nil, err
}
// create the first row if it does not exist yet
if err := db.FirstOrCreate(&AppState{}, AppState{
ID: 1,
Mode: "unspecified",
Running: false,
if err := db.FirstOrCreate(&State{}, State{
ID: 0,
Mode: "unspecified",
}).Error; err != nil {
return nil, err
}
return db, nil
}
func KickoffDatabaseWatchdog(env runtime.Environment, db *gorm.DB) {
timeInterval := time.Second * time.Duration(env.WatchInterval)
ticker := time.NewTicker(timeInterval)
go func() {
defer ticker.Stop()
/*
// Possible future mechanism to stop the watchdog
// must be inside a non-conditional for loop
select {
case <-ticker.C: // ticker event
watchdog(env.DataDirectory, db)
case <-watchdogStop:
return
}
*/
// run the watchdog function once to see if all is well.
watchdog(env.DataDirectory, db)
// then defer to a decoupled/disowned golang goroutine
for range ticker.C {
watchdog(env.DataDirectory, db)
}
}()
}
+43 -13
View File
@@ -1,19 +1,49 @@
package database
type AppState struct {
import (
"time"
"gorm.io/datatypes"
)
type Mode string
const (
ModeUnspecified Mode = "unspecified"
ModeVideo Mode = "video"
ModePresentation Mode = "presentation"
ModeInternet Mode = "internet"
)
type State struct {
ID int `gorm:"primaryKey"`
// Mode =
// 0: unspecified
// 1: video
// 2: presentation
// 3: internet URL
Mode string
Running bool
// unspecified
// video
// presentation
// internet URL
Mode Mode `gorm:"type:varchar(20);not null"` // Must specify what kind of file it is
Targets datatypes.JSON
Location string // Must be the location where the file is downloadable on the API
UpdatedAt time.Time
}
type Files struct {
ID int `gorm:"primaryKey"`
Mode string
Filename string
Filepath string
type Device struct {
ID int `gorm:"primaryKey"`
DeviceType string
Hostname string
RemoteAddress string
Alive bool
Compliant bool
CreatedAt time.Time
UpdatedAt time.Time
}
type File struct {
ID int `gorm:"primaryKey"`
Mode Mode `gorm:"type:varchar(20);not null"`
GivenName string
Filepath string
Checksum string // base64 encoded sha512 checksum
CreatedAt time.Time
UpdatedAt time.Time
}
+6 -14
View File
@@ -1,29 +1,21 @@
package database
import (
"path/filepath"
"gorm.io/gorm"
)
func GetAppState(db *gorm.DB) (AppState, error) {
var state AppState
func GetState(db *gorm.DB) (State, error) {
var state State
return state, db.First(&state).Error
}
func GetFiles(db *gorm.DB) ([]Files, error) {
var files []Files
func GetFiles(db *gorm.DB) ([]File, error) {
var files []File
return files, db.Find(&files).Error
}
func RegisterFile(db *gorm.DB, category, fullPath string) error {
file := Files{
Mode: category,
Filename: filepath.Base(fullPath),
Filepath: fullPath,
}
return db.Create(&file).Error
func RegisterFile(db *gorm.DB, f File) error {
return db.Create(&f).Error
}
+20
View File
@@ -0,0 +1,20 @@
package database
import (
"log"
"log/slog"
"gorm.io/gorm"
)
func watchdog(w string, db *gorm.DB) {
slog.Info("performing the watchdog cycle")
files, err := GetFiles(db)
if err != nil {
slog.Error("failed to retrieve the files indexed from the database", "error", err)
return
}
log.Println(files)
}
+38 -17
View File
@@ -1,44 +1,65 @@
package runtime
import (
"log/slog"
"os"
"path/filepath"
"strconv"
)
type Environment struct {
Version string
Codename string
Hostname string
Port string
WorkDir string
Version string
Codename string
DataDirectory string
ContentDirectory string
Hostname string
Port int
WatchInterval int
}
func safeGrab(key, fallback string) string {
// part of environment checking
func safeStringGrab(key, fallback string) string {
if v, ok := os.LookupEnv(key); ok {
return v
}
return fallback
}
func safeIntGrab(key string, fallback int) int {
if v, ok := os.LookupEnv(key); ok {
if i, err := strconv.Atoi(v); err == nil {
return i
}
}
return fallback
}
func GrabEnvironment() Environment {
cwd, err := os.Getwd()
if err != nil {
cwd = "."
}
fbBase := filepath.Join(cwd, "data")
fbContent := filepath.Join(fbBase, "content")
return Environment{
Version: safeGrab("VERSION", "0.0.1"),
Codename: safeGrab("CODENAME", "Magical Anomaly"),
Hostname: safeGrab("HOSTNAME", "0.0.0.0"),
Port: safeGrab("PORT", "8080"),
WorkDir: safeGrab("OPERATIONDIR", "."),
Version: safeStringGrab("VERSION", "0.0.1"),
Codename: safeStringGrab("CODENAME", "Magical Anomaly"),
DataDirectory: safeStringGrab("DATA_DIR", fbBase),
ContentDirectory: safeStringGrab("CONTENT_DIR", fbContent),
Hostname: safeStringGrab("HOSTNAME", "0.0.0.0"),
Port: safeIntGrab("PORT", 8080),
WatchInterval: safeIntGrab("WATCHDOG_INTERVAL", 60),
}
}
// part of filesystem checking
func EnsureOperation(workDir string) error {
slog.Info("starting audit on: " + workDir)
nDirs := []string{
workDir,
filepath.Join(workDir, "content"),
filepath.Join(workDir, "content", "fresh"),
filepath.Join(workDir, "content", "archive"),
filepath.Join(workDir, "data"),
filepath.Join(workDir, "data", "content"),
filepath.Join(workDir, "web"),
}