Docker/ gets the same deployment shapes as DriverVault: plain, SeaweedFS in the stack, SeaweedFS split into its roles, and an external S3 endpoint, each as a build-from-source and a registry compose file with an .env example of the same name, plus a README. The API Server now reads PB_S3_* on boot and points PocketBase's file storage at the bucket (only when the settings differ), then asks PocketBase to test it. It never turns S3 off. Plugin state gets an api_data volume, since the image could not write plugins.json. The PocketBase image pinned v0.22.21, which cannot run the v0.23+ migrations or `superuser upsert`; it now pins v0.39.11. Starting from an empty database also exposed two migration bugs: the seed tried to create a passwordless account, and the documents collection saved its self-relation with a blank target. Both are fixed; databases that already ran them are unaffected. Verified on Docker: the SeaweedFS and split stacks come up healthy from an empty database, uploads land in the bucket, and a restart is a no-op. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
188 lines
6.0 KiB
Go
188 lines
6.0 KiB
Go
package config
|
|
|
|
import (
|
|
"os"
|
|
"strings"
|
|
)
|
|
|
|
// Config holds all runtime configuration for the API Server.
|
|
type Config struct {
|
|
Addr string
|
|
PocketBaseURL string
|
|
WebAppURL string
|
|
AllowOrigins []string
|
|
|
|
// PluginsFile is the local JSON store for plugin enable-state + config.
|
|
PluginsFile string
|
|
|
|
// Superuser service account used ONLY for admin user-management
|
|
// (list/create/delete users). Optional: when unset, those endpoints return
|
|
// 503 and the rest of the server is unaffected.
|
|
PocketBaseAdminEmail string
|
|
PocketBaseAdminPassword string
|
|
|
|
// PocketBase file storage. With S3Enabled set and the bucket fully
|
|
// described, the server points PocketBase's "Files storage" at this bucket
|
|
// on boot instead of the pb_data volume; left unset, uploads stay on disk
|
|
// exactly as they always have. Only record files (document blobs) move —
|
|
// PocketBase's own backups are deliberately not touched.
|
|
//
|
|
// S3Endpoint has no default: an in-stack SeaweedFS and one outside it are
|
|
// different addresses, and guessing either would be worse than not trying.
|
|
S3Enabled bool
|
|
S3Bucket string
|
|
S3Region string
|
|
S3Endpoint string
|
|
S3AccessKey string
|
|
S3Secret string
|
|
// S3ForcePathStyle keeps bucket names in the path rather than the hostname.
|
|
// True by default because that is what a self-hosted gateway serves —
|
|
// virtual-host style would need a DNS entry per bucket.
|
|
S3ForcePathStyle bool
|
|
}
|
|
|
|
// StorageConfigured reports whether S3 file storage has been fully specified.
|
|
// Anything less counts as "not asked for": a half-filled .env leaves uploads on
|
|
// the local volume rather than pointing PocketBase at a bucket it cannot reach.
|
|
func (c Config) StorageConfigured() bool {
|
|
return c.S3Enabled && c.S3Bucket != "" && c.S3Endpoint != "" &&
|
|
c.S3AccessKey != "" && c.S3Secret != ""
|
|
}
|
|
|
|
// EnvFile is the .env path (relative to the working directory) that Load reads
|
|
// and that runtime settings changes persist back into.
|
|
const EnvFile = ".env"
|
|
|
|
// Load reads configuration from environment variables, applying sensible
|
|
// defaults. A .env file, if present in the working directory, is loaded first.
|
|
func Load() Config {
|
|
loadDotEnv(EnvFile)
|
|
|
|
cfg := Config{
|
|
Addr: getenv("API_ADDR", ":8080"),
|
|
PocketBaseURL: strings.TrimRight(pocketBaseURL(), "/"),
|
|
WebAppURL: strings.TrimRight(getenv("WEBAPP_URL", "http://localhost:8090"), "/"),
|
|
AllowOrigins: splitCSV(getenv("CORS_ALLOW_ORIGINS", "*")),
|
|
PluginsFile: getenv("PLUGINS_FILE", "plugins.json"),
|
|
PocketBaseAdminEmail: getenv("POCKETBASE_ADMIN_EMAIL", os.Getenv("PB_ADMIN_EMAIL")),
|
|
PocketBaseAdminPassword: getenv("POCKETBASE_ADMIN_PASSWORD", os.Getenv("PB_ADMIN_PASSWORD")),
|
|
S3Enabled: boolEnv("PB_S3_ENABLED", false),
|
|
S3Bucket: getenv("PB_S3_BUCKET", "pilotvault"),
|
|
S3Region: getenv("PB_S3_REGION", "us-east-1"),
|
|
S3Endpoint: strings.TrimRight(getenv("PB_S3_ENDPOINT", ""), "/"),
|
|
S3AccessKey: getenv("PB_S3_ACCESS_KEY", ""),
|
|
S3Secret: getenv("PB_S3_SECRET", ""),
|
|
S3ForcePathStyle: boolEnv("PB_S3_FORCE_PATH_STYLE", true),
|
|
}
|
|
return cfg
|
|
}
|
|
|
|
// pocketBaseURL resolves the PocketBase base URL, honouring the legacy PB_URL
|
|
// variable for backward compatibility with older deployments.
|
|
func pocketBaseURL() string {
|
|
if v := os.Getenv("POCKETBASE_URL"); v != "" {
|
|
return v
|
|
}
|
|
if v := os.Getenv("PB_URL"); v != "" {
|
|
return v
|
|
}
|
|
return "http://10.2.1.10:8026"
|
|
}
|
|
|
|
// UpdateEnvFile persists the given KEY=VALUE pairs into the .env file at path,
|
|
// replacing existing keys in place and appending new ones, while preserving all
|
|
// other lines (comments, ordering, unrelated keys). The file is created if it
|
|
// does not exist. Written with 0600 perms since it holds secrets.
|
|
func UpdateEnvFile(path string, updates map[string]string) error {
|
|
existing, _ := os.ReadFile(path) // missing file → start empty
|
|
|
|
remaining := make(map[string]string, len(updates))
|
|
for k, v := range updates {
|
|
remaining[k] = v
|
|
}
|
|
|
|
var out []string
|
|
for _, line := range strings.Split(string(existing), "\n") {
|
|
trimmed := strings.TrimSpace(line)
|
|
if trimmed == "" || strings.HasPrefix(trimmed, "#") {
|
|
out = append(out, line)
|
|
continue
|
|
}
|
|
key, _, ok := strings.Cut(trimmed, "=")
|
|
key = strings.TrimSpace(key)
|
|
if ok {
|
|
if v, found := remaining[key]; found {
|
|
out = append(out, key+"="+v)
|
|
delete(remaining, key)
|
|
continue
|
|
}
|
|
}
|
|
out = append(out, line)
|
|
}
|
|
// Append any keys that weren't already present.
|
|
for k, v := range remaining {
|
|
out = append(out, k+"="+v)
|
|
}
|
|
|
|
content := strings.Join(out, "\n")
|
|
if !strings.HasSuffix(content, "\n") {
|
|
content += "\n"
|
|
}
|
|
return os.WriteFile(path, []byte(content), 0o600)
|
|
}
|
|
|
|
func getenv(key, def string) string {
|
|
if v := os.Getenv(key); v != "" {
|
|
return v
|
|
}
|
|
return def
|
|
}
|
|
|
|
// boolEnv reads a boolean variable, accepting the usual spellings; anything
|
|
// unrecognised (or unset) falls back to def.
|
|
func boolEnv(key string, def bool) bool {
|
|
switch strings.ToLower(strings.TrimSpace(os.Getenv(key))) {
|
|
case "1", "true", "yes", "on":
|
|
return true
|
|
case "0", "false", "no", "off":
|
|
return false
|
|
}
|
|
return def
|
|
}
|
|
|
|
func splitCSV(s string) []string {
|
|
parts := strings.Split(s, ",")
|
|
out := make([]string, 0, len(parts))
|
|
for _, p := range parts {
|
|
if p = strings.TrimSpace(p); p != "" {
|
|
out = append(out, p)
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
// loadDotEnv loads KEY=VALUE pairs from a .env file into the process env if they
|
|
// are not already set. It is intentionally minimal (no quoting rules beyond
|
|
// trimming surrounding quotes).
|
|
func loadDotEnv(path string) {
|
|
data, err := os.ReadFile(path)
|
|
if err != nil {
|
|
return
|
|
}
|
|
for _, line := range strings.Split(string(data), "\n") {
|
|
line = strings.TrimSpace(line)
|
|
if line == "" || strings.HasPrefix(line, "#") {
|
|
continue
|
|
}
|
|
key, val, ok := strings.Cut(line, "=")
|
|
if !ok {
|
|
continue
|
|
}
|
|
key = strings.TrimSpace(key)
|
|
val = strings.Trim(strings.TrimSpace(val), `"'`)
|
|
if _, exists := os.LookupEnv(key); !exists {
|
|
_ = os.Setenv(key, val)
|
|
}
|
|
}
|
|
}
|