feat: add locally syncing and watchdog
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
package utility
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
@@ -13,14 +15,24 @@ type Environment struct {
|
||||
ContentDirectory string
|
||||
Hostname string
|
||||
Port int
|
||||
WatchInterval int
|
||||
WatchdogInterval int
|
||||
WatchdogSyncMode string
|
||||
}
|
||||
|
||||
var (
|
||||
validSyncModes = []string{
|
||||
"sync",
|
||||
"strict",
|
||||
"dry",
|
||||
}
|
||||
)
|
||||
|
||||
// part of environment checking
|
||||
func safeStringGrab(key, fallback string) string {
|
||||
if v, ok := os.LookupEnv(key); ok {
|
||||
return v
|
||||
}
|
||||
slog.Debug("using fallback", "key", key, "fallback", fallback)
|
||||
return fallback
|
||||
}
|
||||
|
||||
@@ -30,6 +42,17 @@ func safeIntGrab(key string, fallback int) int {
|
||||
return i
|
||||
}
|
||||
}
|
||||
slog.Debug("using fallback", "key", key, "fallback", fallback)
|
||||
return fallback
|
||||
}
|
||||
|
||||
func safeSyncModeGrab(key, fallback string) string {
|
||||
if v, ok := os.LookupEnv(key); ok {
|
||||
if slices.Contains(validSyncModes, v) {
|
||||
return v
|
||||
}
|
||||
}
|
||||
slog.Debug("using fallback", "key", key, "fallback", fallback)
|
||||
return fallback
|
||||
}
|
||||
|
||||
@@ -48,8 +71,11 @@ func GrabEnvironment() Environment {
|
||||
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),
|
||||
Port: safeIntGrab("PORT", 8080),
|
||||
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
|
||||
// dry: do nothing
|
||||
WatchdogSyncMode: safeStringGrab("WATCHDOG_SYNC_MODE", "strict"),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user