feat: make watchdog itself configurable
This commit is contained in:
+10
-4
@@ -1,10 +1,10 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"eden-server/internal/api"
|
|
||||||
"eden-server/internal/database"
|
|
||||||
"eden-server/internal/utility"
|
|
||||||
"log/slog"
|
"log/slog"
|
||||||
|
"orbits-server/internal/api"
|
||||||
|
"orbits-server/internal/database"
|
||||||
|
"orbits-server/internal/utility"
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -34,8 +34,14 @@ func main() {
|
|||||||
slog.Error("failed to initiate a database connection", "error", err)
|
slog.Error("failed to initiate a database connection", "error", err)
|
||||||
os.Exit(1)
|
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)
|
database.KickoffDatabaseWatchdog(env, db)
|
||||||
|
} else {
|
||||||
|
slog.Info("skipping database watchdog", "watchdog_enabled", env.WatchdogEnabled)
|
||||||
|
}
|
||||||
|
|
||||||
// TO DO make gin log as json
|
// TO DO make gin log as json
|
||||||
// get ready to kick off the http api with Vue frontend
|
// 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
|
Version: 0.0.1
|
||||||
Section: base
|
Section: base
|
||||||
Priority: optional
|
Priority: optional
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
module eden-server
|
module orbits-server
|
||||||
|
|
||||||
go 1.25.0
|
go 1.26.0
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/gin-gonic/gin v1.12.0
|
github.com/gin-gonic/gin v1.12.0
|
||||||
|
|||||||
+1
-1
@@ -1,9 +1,9 @@
|
|||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"eden-server/internal/utility"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
|
"orbits-server/internal/utility"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"eden-server/internal/database"
|
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"orbits-server/internal/database"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
@@ -12,7 +12,7 @@ import (
|
|||||||
func spawnApiRoutes(api *gin.RouterGroup /* env runtime.Environment,*/, db *gorm.DB) {
|
func spawnApiRoutes(api *gin.RouterGroup /* env runtime.Environment,*/, db *gorm.DB) {
|
||||||
// prefix: api
|
// prefix: api
|
||||||
// Display the information on what is going on at the moment
|
// 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)
|
state, err := database.GetState(db)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Error("unable to determine state", "error", err)
|
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
|
// define a route to check what is registered
|
||||||
api.GET("/available", func(c *gin.Context) {
|
api.GET("/available", func(c *gin.Context) {
|
||||||
files, err := database.GetFiles(db)
|
files, err := database.GetFiles(db)
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"eden-server/internal/database"
|
|
||||||
"eden-server/internal/utility"
|
|
||||||
"errors"
|
"errors"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"orbits-server/internal/database"
|
||||||
|
"orbits-server/internal/utility"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
@@ -35,7 +35,7 @@ func spawnFileRoutes(file *gin.RouterGroup, env utility.Environment, db *gorm.DB
|
|||||||
|
|
||||||
readerStream, err := f.Open()
|
readerStream, err := f.Open()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Error("failed to open uploaded file in memory")
|
slog.Error("failed to a reader stream")
|
||||||
}
|
}
|
||||||
defer readerStream.Close()
|
defer readerStream.Close()
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package database
|
package database
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"eden-server/internal/utility"
|
"orbits-server/internal/utility"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -13,7 +13,7 @@ import (
|
|||||||
//var watchdogStop = make(chan struct{})
|
//var watchdogStop = make(chan struct{})
|
||||||
|
|
||||||
func KickoffDatabase(workDir string) (*gorm.DB, error) {
|
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{
|
db, err := gorm.Open(sqlite.Open(dbLoc), &gorm.Config{
|
||||||
Logger: logger.Discard, // disable gorm logging since its not slog (yet)
|
Logger: logger.Discard, // disable gorm logging since its not slog (yet)
|
||||||
TranslateError: true,
|
TranslateError: true,
|
||||||
@@ -24,7 +24,7 @@ func KickoffDatabase(workDir string) (*gorm.DB, error) {
|
|||||||
|
|
||||||
// try to use GORM automigrate if the schema changes
|
// try to use GORM automigrate if the schema changes
|
||||||
if err := db.AutoMigrate(
|
if err := db.AutoMigrate(
|
||||||
&State{},
|
&Command{},
|
||||||
&Device{},
|
&Device{},
|
||||||
&File{},
|
&File{},
|
||||||
); err != nil {
|
); err != nil {
|
||||||
@@ -32,8 +32,9 @@ func KickoffDatabase(workDir string) (*gorm.DB, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// create the first row if it does not exist yet
|
// create the first row if it does not exist yet
|
||||||
if err := db.FirstOrCreate(&State{}, State{
|
if err := db.FirstOrCreate(&Command{}, Command{
|
||||||
ID: 0,
|
ID: 0,
|
||||||
|
State: "idle",
|
||||||
MediaType: Unspecified,
|
MediaType: Unspecified,
|
||||||
}).Error; err != nil {
|
}).Error; err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|||||||
@@ -19,8 +19,9 @@ type Timestamps struct {
|
|||||||
ExpiresAt time.Time
|
ExpiresAt time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
type State struct {
|
type Command struct {
|
||||||
ID int `gorm:"primaryKey;not null;"`
|
ID int `gorm:"primaryKey;not null;"`
|
||||||
|
State string
|
||||||
// unspecified
|
// unspecified
|
||||||
// video
|
// video
|
||||||
// presentation
|
// presentation
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
package database
|
package database
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"eden-server/internal/utility"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
|
"orbits-server/internal/utility"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
@@ -61,11 +61,11 @@ func BuildFileRecord(r io.Reader, origName string, contentDirectory string) (Fil
|
|||||||
return fData, nil
|
return fData, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetState(db *gorm.DB) (State, error) {
|
func GetState(db *gorm.DB) (Command, error) {
|
||||||
var state State
|
var state Command
|
||||||
|
|
||||||
if err := db.First(&state).Error; err != nil {
|
if err := db.First(&state).Error; err != nil {
|
||||||
return State{}, err
|
return Command{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return state, nil
|
return state, nil
|
||||||
|
|||||||
@@ -1,10 +1,9 @@
|
|||||||
package database
|
package database
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"eden-server/internal/utility"
|
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
|
||||||
"log/slog"
|
"log/slog"
|
||||||
|
"orbits-server/internal/utility"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
@@ -77,7 +76,7 @@ func watchdog(env utility.Environment, db *gorm.DB) {
|
|||||||
case "sync":
|
case "sync":
|
||||||
readerStream, err := os.Open(fp)
|
readerStream, err := os.Open(fp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
slog.Error("failed to a reader stream")
|
||||||
}
|
}
|
||||||
defer readerStream.Close()
|
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)
|
slog.Error("failed to enroll local file into the database", "error", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println(fileData)
|
|
||||||
|
|
||||||
if err := RegisterFile(db, fileData); err != nil {
|
if err := RegisterFile(db, fileData); err != nil {
|
||||||
if errors.Is(err, gorm.ErrDuplicatedKey) {
|
if errors.Is(err, gorm.ErrDuplicatedKey) {
|
||||||
slog.Debug("discarding file since its a duplicate", "error", err)
|
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 {
|
if err := os.Rename(fp, fileData.FilePath); err != nil {
|
||||||
slog.Error("failed to move the locally inserted file", "error", err)
|
slog.Error("failed to move the locally inserted file", "error", err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ type Environment struct {
|
|||||||
ContentDirectory string
|
ContentDirectory string
|
||||||
Hostname string
|
Hostname string
|
||||||
Port int
|
Port int
|
||||||
|
WatchdogEnabled bool
|
||||||
WatchdogInterval int
|
WatchdogInterval int
|
||||||
WatchdogSyncMode string
|
WatchdogSyncMode string
|
||||||
}
|
}
|
||||||
@@ -46,6 +47,15 @@ func safeIntGrab(key string, fallback int) int {
|
|||||||
return fallback
|
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 {
|
func safeSyncModeGrab(key, fallback string) string {
|
||||||
if v, ok := os.LookupEnv(key); ok {
|
if v, ok := os.LookupEnv(key); ok {
|
||||||
if slices.Contains(validSyncModes, v) {
|
if slices.Contains(validSyncModes, v) {
|
||||||
@@ -66,12 +76,19 @@ func GrabEnvironment() Environment {
|
|||||||
fbContent := filepath.Join(fbBase, "content")
|
fbContent := filepath.Join(fbBase, "content")
|
||||||
|
|
||||||
return Environment{
|
return Environment{
|
||||||
|
// Basic server configuration
|
||||||
Version: safeStringGrab("VERSION", "0.0.1"),
|
Version: safeStringGrab("VERSION", "0.0.1"),
|
||||||
Codename: safeStringGrab("CODENAME", "Magical Anomaly"),
|
Codename: safeStringGrab("CODENAME", "Magical Anomaly"),
|
||||||
|
|
||||||
|
// GIN API configuration
|
||||||
DataDirectory: safeStringGrab("DATA_DIR", fbBase),
|
DataDirectory: safeStringGrab("DATA_DIR", fbBase),
|
||||||
ContentDirectory: safeStringGrab("CONTENT_DIR", fbContent),
|
ContentDirectory: safeStringGrab("CONTENT_DIR", fbContent),
|
||||||
Hostname: safeStringGrab("HOSTNAME", "0.0.0.0"),
|
Hostname: safeStringGrab("HOSTNAME", "0.0.0.0"),
|
||||||
Port: safeIntGrab("PORT", 8080),
|
Port: safeIntGrab("PORT", 8080),
|
||||||
|
|
||||||
|
// watchdog configuration
|
||||||
|
// watchdog is the integrity checker/steward
|
||||||
|
WatchdogEnabled: safeBoolGrab("WATCHDOG_ENABLED", true),
|
||||||
WatchdogInterval: safeIntGrab("WATCHDOG_INTERVAL", 60),
|
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
|
// 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
|
// 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"
|
"crypto/sha512"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"io"
|
"io"
|
||||||
"mime/multipart"
|
|
||||||
"os"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func HashReader(r io.Reader) (string, error) {
|
func HashReader(r io.Reader) (string, error) {
|
||||||
@@ -19,23 +17,3 @@ func HashReader(r io.Reader) (string, error) {
|
|||||||
// alternatively return in base64
|
// alternatively return in base64
|
||||||
//return base64.StdEncoding.EncodeToString(h.Sum(nil)), nil
|
//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