Files
Docwell/Server/internal/config/config.go
T
tajniak81andClaude Opus 5.5 3c7403ddf9 Create the first superadmin from DOCWELL_ADMIN_EMAIL on start
When DOCWELL_ADMIN_EMAIL and DOCWELL_ADMIN_PASSWORD are set (and
optionally DOCWELL_ADMIN_NAME), docwell-server creates that account as
superadmin on start while the instance has no accounts. Once any account
exists it changes nothing, so restarts never reset the password or a role
changed in the admin panel. Without them, the first sign-up still wins.

Pass the variables through every compose file, document them in the
.env examples and the configuration, access and quick-start guides.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-13 22:32:29 +01:00

188 lines
5.8 KiB
Go

// 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
// AdminEmail and AdminPassword create the first account as superadmin when
// the instance has none (both set or neither). AdminName is its name.
AdminEmail string
AdminPassword string
AdminName 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
// AuditRetentionDays is how long audit log entries are kept (0 = forever).
AuditRetentionDays int
// WebhooksAllowLocal lets webhooks call loopback and link-local
// addresses (for development; off in production).
WebhooksAllowLocal bool
// PocketBase settings written on every start (settings.ApplyEnv). Empty
// values leave the admin panel in charge.
AppName string
MailFrom string
SMTP SMTP
S3 S3
}
// SMTP is the mail server from DOCWELL_SMTP_*; Host empty means not set.
type SMTP struct {
Host string
Port int
Username string
Password string
TLS bool
}
// S3 is file storage from DOCWELL_S3_*. Enabled is "true", "false" or ""
// (not set).
type S3 struct {
Enabled string
Bucket string
Endpoint string
Region string
AccessKey string
Secret string
ForcePathStyle bool
}
// 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:8070"), "/"),
PocketBaseEmail: get("DOCWELL_PB_EMAIL", ""),
PocketBasePassword: get("DOCWELL_PB_PASSWORD", ""),
SignupEnabled: get("DOCWELL_SIGNUP_ENABLED", "true") == "true",
SignupDomains: splitList(get("DOCWELL_SIGNUP_DOMAINS", "")),
AdminEmail: strings.TrimSpace(get("DOCWELL_ADMIN_EMAIL", "")),
AdminPassword: get("DOCWELL_ADMIN_PASSWORD", ""),
AdminName: get("DOCWELL_ADMIN_NAME", ""),
DataDir: get("DOCWELL_DATA_DIR", "./data"),
ShutdownTimeout: 15 * time.Second,
WebhooksAllowLocal: get("DOCWELL_WEBHOOKS_ALLOW_LOCAL", "false") == "true",
}
cfg.AppName = get("DOCWELL_APP_NAME", "")
cfg.MailFrom = get("DOCWELL_MAIL_FROM", "")
cfg.SMTP = SMTP{
Host: get("DOCWELL_SMTP_HOST", ""),
Username: get("DOCWELL_SMTP_USERNAME", ""),
Password: get("DOCWELL_SMTP_PASSWORD", ""),
TLS: get("DOCWELL_SMTP_TLS", "false") == "true",
}
if v := get("DOCWELL_SMTP_PORT", ""); v != "" {
n, err := strconv.Atoi(v)
if err != nil || n < 1 || n > 65535 {
return Config{}, errors.New("DOCWELL_SMTP_PORT must be between 1 and 65535")
}
cfg.SMTP.Port = n
}
cfg.S3 = S3{
Enabled: get("DOCWELL_S3_ENABLED", ""),
Bucket: get("DOCWELL_S3_BUCKET", ""),
Endpoint: get("DOCWELL_S3_ENDPOINT", ""),
Region: get("DOCWELL_S3_REGION", ""),
AccessKey: get("DOCWELL_S3_ACCESS_KEY", ""),
Secret: get("DOCWELL_S3_SECRET", ""),
ForcePathStyle: get("DOCWELL_S3_FORCE_PATH_STYLE", "true") != "false",
}
if e := cfg.S3.Enabled; e != "" && e != "true" && e != "false" {
return Config{}, errors.New(`DOCWELL_S3_ENABLED must be "true", "false" or empty`)
}
cfg.AuditRetentionDays = 365
if v := get("DOCWELL_AUDIT_RETENTION_DAYS", ""); v != "" {
n, err := strconv.Atoi(v)
if err != nil || n < 0 {
return Config{}, errors.New("DOCWELL_AUDIT_RETENTION_DAYS must be 0 (keep forever) or more")
}
cfg.AuditRetentionDays = n
}
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.AdminEmail == "") != (cfg.AdminPassword == "") {
return Config{}, errors.New("set both DOCWELL_ADMIN_EMAIL and DOCWELL_ADMIN_PASSWORD, or neither")
}
if cfg.AdminPassword != "" && len(cfg.AdminPassword) < 8 {
return Config{}, errors.New("DOCWELL_ADMIN_PASSWORD must be at least 8 characters")
}
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
}