feat: make watchdog itself configurable

This commit is contained in:
DaanSelen
2026-04-22 16:29:07 +02:00
parent ec3a996d6a
commit fa832bd1e0
12 changed files with 59 additions and 53 deletions
+1 -1
View File
@@ -1,9 +1,9 @@
package api
import (
"eden-server/internal/utility"
"fmt"
"log/slog"
"orbits-server/internal/utility"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
+6 -2
View File
@@ -1,9 +1,9 @@
package api
import (
"eden-server/internal/database"
"log/slog"
"net/http"
"orbits-server/internal/database"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
@@ -12,7 +12,7 @@ import (
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) {
api.GET("/command", func(c *gin.Context) {
state, err := database.GetState(db)
if err != nil {
slog.Error("unable to determine state", "error", err)
@@ -28,6 +28,10 @@ func spawnApiRoutes(api *gin.RouterGroup /* env runtime.Environment,*/, db *gorm
})
})
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)
+3 -3
View File
@@ -1,11 +1,11 @@
package api
import (
"eden-server/internal/database"
"eden-server/internal/utility"
"errors"
"log/slog"
"net/http"
"orbits-server/internal/database"
"orbits-server/internal/utility"
"path/filepath"
"github.com/gin-gonic/gin"
@@ -35,7 +35,7 @@ func spawnFileRoutes(file *gin.RouterGroup, env utility.Environment, db *gorm.DB
readerStream, err := f.Open()
if err != nil {
slog.Error("failed to open uploaded file in memory")
slog.Error("failed to a reader stream")
}
defer readerStream.Close()
+5 -4
View File
@@ -1,7 +1,7 @@
package database
import (
"eden-server/internal/utility"
"orbits-server/internal/utility"
"path/filepath"
"time"
@@ -13,7 +13,7 @@ import (
//var watchdogStop = make(chan struct{})
func KickoffDatabase(workDir string) (*gorm.DB, error) {
dbLoc := filepath.Join(workDir, "garden.db")
dbLoc := filepath.Join(workDir, "station.db")
db, err := gorm.Open(sqlite.Open(dbLoc), &gorm.Config{
Logger: logger.Discard, // disable gorm logging since its not slog (yet)
TranslateError: true,
@@ -24,7 +24,7 @@ func KickoffDatabase(workDir string) (*gorm.DB, error) {
// try to use GORM automigrate if the schema changes
if err := db.AutoMigrate(
&State{},
&Command{},
&Device{},
&File{},
); err != nil {
@@ -32,8 +32,9 @@ func KickoffDatabase(workDir string) (*gorm.DB, error) {
}
// create the first row if it does not exist yet
if err := db.FirstOrCreate(&State{}, State{
if err := db.FirstOrCreate(&Command{}, Command{
ID: 0,
State: "idle",
MediaType: Unspecified,
}).Error; err != nil {
return nil, err
+3 -2
View File
@@ -19,8 +19,9 @@ type Timestamps struct {
ExpiresAt time.Time
}
type State struct {
ID int `gorm:"primaryKey;not null;"`
type Command struct {
ID int `gorm:"primaryKey;not null;"`
State string
// unspecified
// video
// presentation
+4 -4
View File
@@ -1,10 +1,10 @@
package database
import (
"eden-server/internal/utility"
"fmt"
"io"
"log/slog"
"orbits-server/internal/utility"
"path/filepath"
"github.com/google/uuid"
@@ -61,11 +61,11 @@ func BuildFileRecord(r io.Reader, origName string, contentDirectory string) (Fil
return fData, nil
}
func GetState(db *gorm.DB) (State, error) {
var state State
func GetState(db *gorm.DB) (Command, error) {
var state Command
if err := db.First(&state).Error; err != nil {
return State{}, err
return Command{}, err
}
return state, nil
+4 -5
View File
@@ -1,10 +1,9 @@
package database
import (
"eden-server/internal/utility"
"errors"
"fmt"
"log/slog"
"orbits-server/internal/utility"
"os"
"path/filepath"
@@ -77,7 +76,7 @@ func watchdog(env utility.Environment, db *gorm.DB) {
case "sync":
readerStream, err := os.Open(fp)
if err != nil {
slog.Error("failed to a reader stream")
}
defer readerStream.Close()
@@ -86,8 +85,6 @@ func watchdog(env utility.Environment, db *gorm.DB) {
slog.Error("failed to enroll local file into the database", "error", err)
}
fmt.Println(fileData)
if err := RegisterFile(db, fileData); err != nil {
if errors.Is(err, gorm.ErrDuplicatedKey) {
slog.Debug("discarding file since its a duplicate", "error", err)
@@ -96,6 +93,8 @@ func watchdog(env utility.Environment, db *gorm.DB) {
}
}
// to fully finalize the enrollment process, we rename the locally inserted file to a unique filename
// this is to make all files comply, wether uploaded via the api or locally inserted with the filesystem
if err := os.Rename(fp, fileData.FilePath); err != nil {
slog.Error("failed to move the locally inserted file", "error", err)
}
+19 -2
View File
@@ -15,6 +15,7 @@ type Environment struct {
ContentDirectory string
Hostname string
Port int
WatchdogEnabled bool
WatchdogInterval int
WatchdogSyncMode string
}
@@ -46,6 +47,15 @@ func safeIntGrab(key string, fallback int) int {
return fallback
}
func safeBoolGrab(key string, fallback bool) bool {
if v, ok := os.LookupEnv(key); ok {
if b, err := strconv.ParseBool(v); err == nil {
return b
}
}
return fallback
}
func safeSyncModeGrab(key, fallback string) string {
if v, ok := os.LookupEnv(key); ok {
if slices.Contains(validSyncModes, v) {
@@ -66,12 +76,19 @@ func GrabEnvironment() Environment {
fbContent := filepath.Join(fbBase, "content")
return Environment{
Version: safeStringGrab("VERSION", "0.0.1"),
Codename: safeStringGrab("CODENAME", "Magical Anomaly"),
// Basic server configuration
Version: safeStringGrab("VERSION", "0.0.1"),
Codename: safeStringGrab("CODENAME", "Magical Anomaly"),
// GIN API configuration
DataDirectory: safeStringGrab("DATA_DIR", fbBase),
ContentDirectory: safeStringGrab("CONTENT_DIR", fbContent),
Hostname: safeStringGrab("HOSTNAME", "0.0.0.0"),
Port: safeIntGrab("PORT", 8080),
// watchdog configuration
// watchdog is the integrity checker/steward
WatchdogEnabled: safeBoolGrab("WATCHDOG_ENABLED", true),
WatchdogInterval: safeIntGrab("WATCHDOG_INTERVAL", 60),
// sync: sync local files to the database, for example when you want to allow local inserting files which then get added to the database
// strict: make the database leading, this is when you only allow API uploads to be registered, and remove orphaned filesystem files
-22
View File
@@ -4,8 +4,6 @@ import (
"crypto/sha512"
"encoding/hex"
"io"
"mime/multipart"
"os"
)
func HashReader(r io.Reader) (string, error) {
@@ -19,23 +17,3 @@ func HashReader(r io.Reader) (string, error) {
// alternatively return in base64
//return base64.StdEncoding.EncodeToString(h.Sum(nil)), nil
}
func HashUpload(fileHeader *multipart.FileHeader) (string, error) {
stream, err := fileHeader.Open()
if err != nil {
return "", err
}
defer stream.Close()
return HashReader(stream)
}
func HashFile(path string) (string, error) {
file, err := os.Open(path)
if err != nil {
return "", err
}
defer file.Close()
return HashReader(file)
}