Files
Docwell/Server/internal/settings/env.go
T
tajniak81andClaude Opus 5.5 b9be8be156
CI / go (Server) (push) Failing after 22m50s
CI / go (Web-App) (push) Canceled after 0s
CI / frontend (push) Failing after 8m47s
CI / desktop (ubuntu-latest) (push) Successful in 2h56m50s
CI / mobile (push) Failing after 5m33s
CI / docker (push) Successful in 1h43m8s
Docs / build (push) Successful in 16m2s
Docs / deploy (push) Skipped
CI / desktop (macos-latest) (push) Canceled after 0s
CI / desktop (windows-latest) (push) Canceled after 0s
Run docwell-db as a stock PocketBase and let the server set it up
docwell-db no longer needs a Docwell image: the stack runs
tajniak81/pocketbase-docker-root as it is, and docwell-server does what
the migrations and pb_hooks used to do inside PocketBase.

On start, docwell-server now:
- waits for PocketBase and imports Docwell's collections from an
  embedded snapshot (internal/pbschema). Existing collections and
  fields are matched by name, so their data is kept; the admin-managed
  users options (OAuth2 providers, magic link) are left untouched.
- writes the DOCWELL_APP_NAME, DOCWELL_WEB_URL, DOCWELL_MAIL_FROM,
  DOCWELL_SMTP_* and DOCWELL_S3_* settings into PocketBase, as the
  boot hook did.

It also replaces the hook's two routes: token checks use PocketBase's
auth-refresh (the organization name is read as the superuser), and
Docwell's own emails go out over SMTP from the server. PocketBase never
returns the SMTP password, so the server keeps a copy in the
superuser-only instance_settings collection; a password saved before
this change has to be entered once more in the admin panel.

Database/ keeps pb_migrations as the source of the schema;
export-schema.sh regenerates the snapshot from them. Database/Dockerfile
and pb_hooks are removed, the release no longer builds docwell-db, the
AIO image runs PocketBase without migrations or hooks, and the compose
files default DB_IMAGE to the stock image and pass the settings to the
server instead of the database.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-13 20:09:38 +01:00

116 lines
3.1 KiB
Go

// SPDX-License-Identifier: AGPL-3.0-or-later
package settings
import (
"context"
"fmt"
"net/http"
)
// Env holds the PocketBase settings that come from environment variables.
// ApplyEnv writes them on every start, so changing an env var and restarting
// is enough. Settings without an env var are left alone: the admin panel
// manages them.
type Env struct {
AppName string // DOCWELL_APP_NAME
WebURL string // DOCWELL_WEB_URL: {APP_URL} in PocketBase's auth emails
MailFrom string // DOCWELL_MAIL_FROM
// SMTP is applied only when SMTPHost is set.
SMTPHost string
SMTPPort int
SMTPUsername string
SMTPPassword string
SMTPTLS bool
// S3 is "true" (uploads go to the bucket), "false" (back to local disk)
// or "" (the admin panel's Storage screen is in charge).
S3 string
S3Bucket string
S3Endpoint string
S3Region string
S3AccessKey string
S3Secret string
S3ForcePathStyle bool
}
// PocketBase's placeholders on a fresh database.
const (
pbPlaceholderName = "Acme"
pbPlaceholderSender = "support@example.com"
)
// ApplyEnv writes the environment's settings to PocketBase.
func (s *Service) ApplyEnv(ctx context.Context, env Env) error {
var cur struct {
Meta struct {
AppName string `json:"appName"`
SenderAddress string `json:"senderAddress"`
} `json:"meta"`
}
if err := s.pb.Do(ctx, http.MethodGet, "/api/settings", nil, &cur); err != nil {
return fmt.Errorf("settings: read: %w", err)
}
appName := cur.Meta.AppName
if env.AppName != "" {
appName = env.AppName
} else if appName == pbPlaceholderName || appName == "" {
appName = "Docwell"
}
sender := cur.Meta.SenderAddress
if env.MailFrom != "" {
sender = env.MailFrom
} else if sender == pbPlaceholderSender || sender == "" {
sender = "no-reply@docwell.localhost"
}
webURL := env.WebURL
if webURL == "" {
webURL = "http://localhost:3000"
}
patch := map[string]any{
"meta": map[string]any{
"appName": appName, "appURL": webURL, "senderName": appName, "senderAddress": sender,
},
}
if env.SMTPHost != "" {
port := env.SMTPPort
if port == 0 {
port = 587
}
patch["smtp"] = map[string]any{
"enabled": true, "host": env.SMTPHost, "port": port,
"username": env.SMTPUsername, "password": env.SMTPPassword, "tls": env.SMTPTLS,
}
}
switch env.S3 {
case "true":
bucket, region := env.S3Bucket, env.S3Region
if bucket == "" {
bucket = "docwell"
}
if region == "" {
region = "us-east-1"
}
patch["s3"] = map[string]any{
"enabled": true, "bucket": bucket, "endpoint": env.S3Endpoint, "region": region,
"accessKey": env.S3AccessKey, "secret": env.S3Secret, "forcePathStyle": env.S3ForcePathStyle,
}
case "false":
patch["s3"] = map[string]any{"enabled": false}
}
if err := s.pb.Do(ctx, http.MethodPatch, "/api/settings", patch, nil); err != nil {
return fmt.Errorf("settings: apply environment: %w", settingsError(err))
}
if env.SMTPHost != "" {
if err := s.save(ctx, keySMTPPassword, env.SMTPPassword); err != nil {
return fmt.Errorf("settings: store smtp password: %w", err)
}
}
return nil
}