// SPDX-License-Identifier: AGPL-3.0-or-later // Package config loads docwell-server settings from environment variables. package config import ( "errors" "os" "strconv" "strings" "time" ) type Config struct { // Addr is the listen address of the public API, e.g. ":8080". Addr string // PublicURL is the externally reachable base URL of the server. PublicURL string // WebURL is the base URL of docwell-web; links in emails open it. WebURL string // CORSOrigins lists origins allowed to call the API from a browser. CORSOrigins []string // SignupEnabled allows self-service signup (the first account always can). SignupEnabled bool // SignupDomains limits signup to these email domains when not empty. SignupDomains []string PocketBaseURL string PocketBaseEmail string PocketBasePassword string // DataDir holds docwell-server's own files (the search index). DataDir string // MaxUploadMB caps one attachment. MaxUploadMB int ShutdownTimeout time.Duration } // Load reads the configuration from the environment and validates it. func Load() (Config, error) { return fromLookup(os.LookupEnv) } func fromLookup(lookup func(string) (string, bool)) (Config, error) { get := func(key, def string) string { if v, ok := lookup(key); ok && v != "" { return v } return def } cfg := Config{ Addr: get("DOCWELL_ADDR", ":8080"), PublicURL: strings.TrimRight(get("DOCWELL_PUBLIC_URL", "http://localhost:8080"), "/"), WebURL: strings.TrimRight(get("DOCWELL_WEB_URL", "http://localhost:3000"), "/"), CORSOrigins: splitList(get("DOCWELL_CORS_ORIGINS", "http://localhost:5173,http://localhost:3000")), PocketBaseURL: strings.TrimRight(get("DOCWELL_PB_URL", "http://127.0.0.1:8090"), "/"), PocketBaseEmail: get("DOCWELL_PB_EMAIL", ""), PocketBasePassword: get("DOCWELL_PB_PASSWORD", ""), SignupEnabled: get("DOCWELL_SIGNUP_ENABLED", "true") == "true", SignupDomains: splitList(get("DOCWELL_SIGNUP_DOMAINS", "")), DataDir: get("DOCWELL_DATA_DIR", "./data"), ShutdownTimeout: 15 * time.Second, } if v := get("DOCWELL_SHUTDOWN_TIMEOUT", ""); v != "" { d, err := time.ParseDuration(v) if err != nil { return Config{}, errors.New("DOCWELL_SHUTDOWN_TIMEOUT: " + err.Error()) } cfg.ShutdownTimeout = d } cfg.MaxUploadMB = 50 if v := get("DOCWELL_MAX_UPLOAD_MB", ""); v != "" { n, err := strconv.Atoi(v) if err != nil || n < 1 || n > 100 { return Config{}, errors.New("DOCWELL_MAX_UPLOAD_MB must be between 1 and 100") } cfg.MaxUploadMB = n } if cfg.PocketBaseEmail == "" || cfg.PocketBasePassword == "" { return Config{}, errors.New("DOCWELL_PB_EMAIL and DOCWELL_PB_PASSWORD are required") } return cfg, nil } func splitList(s string) []string { var out []string for _, part := range strings.Split(s, ",") { if part = strings.TrimSpace(part); part != "" { out = append(out, part) } } return out }