feat: make watchdog itself configurable
This commit is contained in:
+10
-4
@@ -1,10 +1,10 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"eden-server/internal/api"
|
||||
"eden-server/internal/database"
|
||||
"eden-server/internal/utility"
|
||||
"log/slog"
|
||||
"orbits-server/internal/api"
|
||||
"orbits-server/internal/database"
|
||||
"orbits-server/internal/utility"
|
||||
"os"
|
||||
)
|
||||
|
||||
@@ -34,8 +34,14 @@ func main() {
|
||||
slog.Error("failed to initiate a database connection", "error", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
slog.Info("kicking off database watchdog", "watch_interval", env.WatchdogInterval)
|
||||
|
||||
// kick off the watchdog depending on the environment variables
|
||||
if env.WatchdogEnabled {
|
||||
slog.Info("kicking off database watchdog", "watchdog_interval", env.WatchdogInterval)
|
||||
database.KickoffDatabaseWatchdog(env, db)
|
||||
} else {
|
||||
slog.Info("skipping database watchdog", "watchdog_enabled", env.WatchdogEnabled)
|
||||
}
|
||||
|
||||
// TO DO make gin log as json
|
||||
// get ready to kick off the http api with Vue frontend
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
Package: eden-server
|
||||
Package: orbits-server
|
||||
Version: 0.0.1
|
||||
Section: base
|
||||
Priority: optional
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
module eden-server
|
||||
module orbits-server
|
||||
|
||||
go 1.25.0
|
||||
go 1.26.0
|
||||
|
||||
require (
|
||||
github.com/gin-gonic/gin v1.12.0
|
||||
|
||||
+1
-1
@@ -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"
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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()
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -19,8 +19,9 @@ type Timestamps struct {
|
||||
ExpiresAt time.Time
|
||||
}
|
||||
|
||||
type State struct {
|
||||
type Command struct {
|
||||
ID int `gorm:"primaryKey;not null;"`
|
||||
State string
|
||||
// unspecified
|
||||
// video
|
||||
// presentation
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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{
|
||||
// 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
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user