Files
Docwell/Server/internal/config/config.go
T
tajniak81andClaude Opus 5.5 6984c7d162 Connect Google Calendar, import OneNote notebooks, sync Readwise, and package the clipper
Phase F follow-ups (ADR-16):

- Calendars: events are now stored in calendar_events by a
  calendars.sync job (every 15 minutes per account, 90 days back and
  400 ahead), so journals open at once and keep showing events while a
  source is down. Google accounts connect with OAuth (calendar.readonly)
  through DOCWELL_GOOGLE_CLIENT_ID/SECRET; reconnecting renews access.
- OneNote: whole notebooks import through Microsoft Graph (Notes.Read,
  DOCWELL_MICROSOFT_CLIENT_ID/SECRET/TENANT) in the background, with
  section groups, sections, subpages, pictures and attached files. The
  Microsoft sign-in lives in memory for an hour and is never stored.
- Readwise: an hourly sync with the person's sealed access token; new
  books get a page under "Readwise", new highlights are appended once.
  New readwise_syncs collection.
- internal/oauth: signed states and one callback route per provider,
  routed by purpose. directory gains ImportConverted and CallerOf.
- Phone app: "Today's journal" on the home screen and calendar events
  on journal pages.
- Web clipper: `npm run package` builds Chrome/Edge, Firefox and source
  zips; store/ holds listing texts, permission notes and a privacy
  policy.
- OpenAPI, generated clients, translations, compose and .env examples,
  and user docs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 14:59:34 +01:00

225 lines
7.3 KiB
Go

// SPDX-License-Identifier: AGPL-3.0-or-later
// Package config loads docwell-server settings from environment variables.
package config
import (
"errors"
"net/url"
"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, AI vectors).
DataDir string
// SecretKey encrypts stored AI provider keys; empty derives one from the
// PocketBase password.
SecretKey string
// MaxUploadMB caps one attachment.
MaxUploadMB int
// MediaURL is docwell-media, which cuts long recordings with ffmpeg; ""
// cuts them on the server. MediaToken is its shared secret, if any.
MediaURL string
MediaToken string
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
// Google and Microsoft are OAuth apps people sign in to for Google
// Calendar and OneNote imports; empty client ids turn those off.
Google OAuthApp
Microsoft OAuthApp
// 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"),
SecretKey: get("DOCWELL_SECRET_KEY", ""),
MediaURL: strings.TrimRight(get("DOCWELL_MEDIA_URL", ""), "/"),
MediaToken: get("DOCWELL_MEDIA_TOKEN", ""),
ShutdownTimeout: 15 * time.Second,
WebhooksAllowLocal: get("DOCWELL_WEBHOOKS_ALLOW_LOCAL", "false") == "true",
}
cfg.Google = OAuthApp{ClientID: get("DOCWELL_GOOGLE_CLIENT_ID", ""), ClientSecret: get("DOCWELL_GOOGLE_CLIENT_SECRET", "")}
cfg.Microsoft = OAuthApp{
ClientID: get("DOCWELL_MICROSOFT_CLIENT_ID", ""), ClientSecret: get("DOCWELL_MICROSOFT_CLIENT_SECRET", ""),
Tenant: get("DOCWELL_MICROSOFT_TENANT", "common"),
}
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.MediaURL != "" {
u, err := url.Parse(cfg.MediaURL)
if err != nil || (u.Scheme != "http" && u.Scheme != "https") || u.Host == "" {
return Config{}, errors.New("DOCWELL_MEDIA_URL must be an http(s) URL, e.g. http://media:8090")
}
}
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
}
// OAuthApp is an app registered with Google or Microsoft (Tenant: Microsoft
// only; "common" lets any account sign in).
type OAuthApp struct {
ClientID string
ClientSecret string
Tenant string
}