Files
upandown/cmd/server/main.go
T
claude BotandClaude Opus 5.5 f8801f00f9 Lot 1 : catalogue PostgreSQL partagé pour les métadonnées et les partages
- Nouveau paquet internal/catalog : pool pgx, migrations SQL embarquées
  (sérialisées entre instances par verrou consultatif), transactions
  rejouées après conflit, interblocage ou bascule du primaire (pool vidé
  sur erreur de lecture seule ou de connexion). DSN multi-hôtes accepté
  (target_session_attrs=read-write) pour une future HA.
- Store.UseCatalog : tags, icônes, auteurs (item_meta) et liens de partage
  (shares) lus et écrits en base ; chaque modification se fait sous verrou
  consultatif par racine (« main », « home/<id> »), donc sans perte
  d'écriture entre instances. Sans DATABASE_URL, rien ne change (JSON).
- Reprise unique de .meta.json et .shares.json au premier branchement
  (table imports), fichiers renommés en *.imported.
- Serveur : DATABASE_URL / DATABASE_URL_FILE, DATABASE_WAIT (60 s).
- Compose : service postgres:16 sur un réseau interne dédié ; Traefik
  inchangé. Dockerfile : go.sum et téléchargement des modules.
- Tests : UPANDOWN_TEST_DATABASE_URL fait passer toute la suite storage par
  la base ; tests de partage entre deux instances et de reprise JSON.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LNfwm4pfYR9C914C2EeiuB
2026-11-20 17:20:16 +01:00

483 lines
15 KiB
Go

// Commande server : API de gestion de fichiers + frontend embarqué.
package main
import (
"context"
"errors"
"fmt"
"io/fs"
"log/slog"
"net"
"net/http"
"net/url"
"os"
"os/signal"
"path"
"strconv"
"strings"
"syscall"
"time"
"github.com/vblogio/upandown/internal/api"
"github.com/vblogio/upandown/internal/auth"
"github.com/vblogio/upandown/internal/catalog"
"github.com/vblogio/upandown/internal/storage"
"github.com/vblogio/upandown/internal/tokens"
"github.com/vblogio/upandown/web"
)
// version est renseignée au build (-ldflags "-X main.version=...").
var version = "dev"
func env(key, def string) string {
if v := os.Getenv(key); v != "" {
return v
}
return def
}
// envBool lit un booléen ; une valeur illisible est une erreur de configuration.
func envBool(key string, def bool) bool {
v := os.Getenv(key)
if v == "" {
return def
}
b, err := strconv.ParseBool(v)
if err != nil {
fatal(key + " invalide (true ou false)")
}
return b
}
func envInt(key string, def int64) int64 {
v := os.Getenv(key)
if v == "" {
return def
}
n, err := strconv.ParseInt(v, 10, 64)
if err != nil || n < 0 {
fatal(key + " invalide (entier >= 0)")
}
return n
}
func fatal(msg string, args ...any) {
slog.Error(msg, args...)
os.Exit(1)
}
// envDuration lit une durée (« 30s », « 2m »).
func envDuration(key string, def time.Duration) time.Duration {
v := os.Getenv(key)
if v == "" {
return def
}
d, err := time.ParseDuration(v)
if err != nil || d < 0 {
fatal(key + " invalide (durée attendue, ex. 60s)")
}
return d
}
// secretEnv lit un secret dans NAME, ou dans le fichier NAME_FILE (secrets Docker).
func secretEnv(name string) string {
if v := os.Getenv(name); v != "" {
return v
}
if f := os.Getenv(name + "_FILE"); f != "" {
b, err := os.ReadFile(f)
if err != nil {
fatal(name+"_FILE illisible", "err", err)
}
return strings.TrimSpace(string(b))
}
return ""
}
func main() {
slog.SetDefault(slog.New(slog.NewJSONHandler(os.Stdout, nil)))
addr := env("LISTEN_ADDR", ":8080")
// `server healthcheck` : sonde pour l'image distroless (pas de curl/wget).
if len(os.Args) > 1 && os.Args[1] == "healthcheck" {
os.Exit(healthcheck(addr))
}
dataDir := env("DATA_DIR", "./data")
store, err := storage.New(dataDir)
if err != nil {
fatal("initialisation du stockage", "dir", dataDir, "err", err)
}
if store.History, err = historyPolicy(); err != nil {
fatal("historique", "err", err)
}
store.Defaults = storage.SettingsDefaults{
Registration: strings.ToLower(env("REGISTRATION", storage.RegClosed)),
QuotaMB: envInt("USER_QUOTA_MB", 0),
CommonReadonly: envBool("COMMON_READONLY", false),
TrashDays: int(envInt("TRASH_DAYS", 0)),
}
if !storage.ValidRegistration(store.Defaults.Registration) {
fatal("REGISTRATION invalide (closed, approval ou open)")
}
// Clé de signature des liens de partage : SHARE_SECRET, sinon DATA_DIR/.share-secret.
if secret := os.Getenv("SHARE_SECRET"); secret != "" {
if len(secret) < 16 {
fatal("SHARE_SECRET trop court (16 caractères minimum)")
}
store.SetShareSecret(secret)
}
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
spaces := storage.NewSpaces(store)
// Catalogue PostgreSQL partagé (mode cluster) : DATABASE_URL ou DATABASE_URL_FILE.
// Sans lui, les métadonnées restent dans les fichiers JSON de DATA_DIR.
if dsn := secretEnv("DATABASE_URL"); dsn != "" {
db, err := catalog.Open(ctx, dsn, envDuration("DATABASE_WAIT", 60*time.Second))
if err != nil {
fatal("catalogue PostgreSQL", "err", err)
}
defer db.Close()
if err := store.UseCatalog(db, "main"); err != nil {
fatal("catalogue PostgreSQL", "err", err)
}
spaces.Catalog = db
slog.Info("catalogue PostgreSQL branché")
}
spaces.MaxUpload = envInt("MAX_UPLOAD_MB", 0) << 20 // 0 : illimité
mode := strings.ToLower(env("AUTH_MODE", "local"))
handler := (&api.Handler{Store: store, Spaces: spaces, Mode: mode, Version: version}).Init()
mux := http.NewServeMux()
var root http.Handler = mux
tokenStore, err := tokens.Open(dataDir)
if err != nil {
fatal("jetons d'accès", "err", err)
}
ts := &tokens.Server{Store: tokenStore, UserID: api.UserID}
switch mode {
case "none":
// Aucune authentification intégrée (reverse proxy avec SSO, usage local).
slog.Warn("aucune authentification intégrée (AUTH_MODE=none) : à réserver à un reverse proxy qui authentifie")
ts.Resolve = func(string) (*auth.User, bool) { return nil, true }
case "local", "oidc", "both":
accounts, err := auth.OpenAccounts(dataDir)
if err != nil {
fatal("comptes", "err", err)
}
handler.Accounts = accounts
a, err := authentication(ctx, mode, dataDir, accounts, store, handler)
if err != nil {
fatal("authentification", "err", err)
}
a.Register(mux)
handler.Auth = a
root = a.Middleware(mux, public)
ts.Resolve = func(id string) (*auth.User, bool) {
acc, ok := accounts.Get(id)
if !ok || acc.Status != auth.StatusActive {
return nil, false
}
u := acc.User()
return &u, true
}
handler.OnUserRemoved = tokenStore.RevokeUser
default:
fatal(fmt.Sprintf("AUTH_MODE %q inconnu (local, oidc, both ou none)", mode))
}
handler.Register(mux)
ts.Register(mux)
mux.HandleFunc("GET /healthz", func(w http.ResponseWriter, _ *http.Request) { _, _ = w.Write([]byte("ok")) })
mux.Handle("/", spa())
h := logRequests(securityHeaders(sameOrigin(ts.Middleware(root, mux))))
srv := &http.Server{
Addr: addr,
Handler: h,
ReadHeaderTimeout: 10 * time.Second,
IdleTimeout: 120 * time.Second,
// Pas de ReadTimeout ni de WriteTimeout : les dépôts et téléchargements de
// gros fichiers peuvent durer longtemps ; seule l'inactivité est bornée.
}
go func() {
slog.Info("démarrage", "addr", addr, "data", dataDir, "auth", mode, "version", version,
"history_keep", store.History.Keep, "max_upload_mb", spaces.MaxUpload>>20)
if err := srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
fatal("serveur", "err", err)
}
}()
<-ctx.Done()
shutdownCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
_ = srv.Shutdown(shutdownCtx)
slog.Info("arrêt")
}
// authentication prépare l'authentification locale (comptes, inscription) et/ou OIDC.
func authentication(ctx context.Context, mode, dataDir string, accounts *auth.Accounts, store *storage.Store, h *api.Handler) (*auth.Auth, error) {
secret, err := auth.LoadSecret(os.Getenv("SESSION_SECRET"), dataDir)
if err != nil {
return nil, err
}
ttl, err := time.ParseDuration(env("SESSION_TTL", "24h"))
if err != nil || ttl < 5*time.Minute {
return nil, errors.New("SESSION_TTL invalide (ex. 24h, minimum 5m)")
}
pub := publicURL()
secureCookies := strings.HasPrefix(pub, "https://") || strings.HasPrefix(os.Getenv("OIDC_REDIRECT_URL"), "https://")
sessions := auth.NewSessions(secret, ttl, secureCookies)
h.SecureCookies = secureCookies
a := auth.New(&auth.Auth{
Accounts: accounts,
Sessions: sessions,
Local: mode == "local" || mode == "both",
Registration: func() string {
if st, err := store.ReadSettings(); err == nil {
return st.Registration
}
return storage.RegClosed
},
OIDCName: env("OIDC_NAME", "SSO"),
})
h.OIDCName = a.OIDCName
if a.Local {
if user := os.Getenv("ADMIN_USER"); user != "" {
pass := secretEnv("ADMIN_PASSWORD")
if pass == "" {
return nil, errors.New("ADMIN_USER défini sans ADMIN_PASSWORD")
}
if err := accounts.EnsureAdmin(user, pass); err != nil {
return nil, fmt.Errorf("administrateur initial : %w", err)
}
}
if accounts.NeedsSetup() {
slog.Warn("aucun administrateur : le premier compte créé (inscription) le deviendra ; définir ADMIN_USER et ADMIN_PASSWORD pour l'imposer")
}
}
if mode == "local" {
return a, nil
}
// OIDC
cfg, err := oidcConfig(secret, ttl)
if err != nil {
return nil, err
}
cfg.Sessions = sessions
policy := auth.OIDCPolicy{AdminGroups: fields(os.Getenv("OIDC_ADMIN_GROUPS")), AdminEmails: fields(os.Getenv("OIDC_ADMIN_EMAILS"))}
cfg.Provision = func(u auth.User, groups []string) (auth.User, error) {
return accounts.UpsertOIDC(u, groups, policy)
}
o, err := auth.NewOIDC(ctx, cfg)
if err != nil {
return nil, err
}
a.OIDC = o
slog.Info("authentification OIDC activée", "issuer", cfg.Issuer, "client_id", cfg.ClientID,
"groups", cfg.AllowedGroups, "admin_groups", policy.AdminGroups, "session_ttl", ttl.String())
return a, nil
}
// publicURL est l'origine publique du service : PUBLIC_URL, sinon celle de
// OIDC_REDIRECT_URL ; vide, elle est déduite de chaque requête.
func publicURL() string {
if v := os.Getenv("PUBLIC_URL"); v != "" {
return strings.TrimRight(v, "/")
}
if u, err := url.Parse(os.Getenv("OIDC_REDIRECT_URL")); err == nil && u.Scheme != "" && u.Host != "" {
return u.Scheme + "://" + u.Host
}
return ""
}
// oidcConfig lit les variables OIDC_*.
func oidcConfig(secret []byte, ttl time.Duration) (auth.Config, error) {
cfg := auth.Config{
Issuer: os.Getenv("OIDC_ISSUER"),
ClientID: os.Getenv("OIDC_CLIENT_ID"),
ClientSecret: secretEnv("OIDC_CLIENT_SECRET"),
RedirectURL: os.Getenv("OIDC_REDIRECT_URL"),
Scopes: fields(env("OIDC_SCOPES", "openid profile email groups")),
GroupsClaim: env("OIDC_GROUPS_CLAIM", "groups"),
SessionTTL: ttl,
Secret: secret,
}
cfg.AllowedGroups = fields(os.Getenv("OIDC_ALLOWED_GROUPS"))
var err error
if cfg.EndSession, err = strconv.ParseBool(env("OIDC_LOGOUT", "true")); err != nil {
return cfg, errors.New("OIDC_LOGOUT invalide (true ou false)")
}
return cfg, nil
}
// fields découpe une liste séparée par des espaces ou des virgules.
func fields(s string) []string {
return strings.FieldsFunc(s, func(r rune) bool { return r == ' ' || r == ',' })
}
func historyPolicy() (storage.HistoryPolicy, error) {
p := storage.DefaultHistory
keep, err := strconv.Atoi(env("HISTORY_KEEP", strconv.Itoa(p.Keep)))
if err != nil || keep < 0 {
return p, errors.New("HISTORY_KEEP invalide (entier >= 0)")
}
p.Keep = keep
if p.Interval, err = time.ParseDuration(env("HISTORY_INTERVAL", "0")); err != nil || p.Interval < 0 {
return p, errors.New("HISTORY_INTERVAL invalide (ex. 10m, 0 : à chaque remplacement)")
}
if p.MaxAge, err = time.ParseDuration(env("HISTORY_MAX_AGE", "0")); err != nil || p.MaxAge < 0 {
return p, errors.New("HISTORY_MAX_AGE invalide (ex. 2160h, 0 : illimité)")
}
return p, nil
}
func healthcheck(addr string) int {
host, port, err := net.SplitHostPort(addr)
if err != nil {
return 1
}
if host == "" || host == "0.0.0.0" || host == "::" {
host = "127.0.0.1"
}
c := http.Client{Timeout: 3 * time.Second}
res, err := c.Get("http://" + net.JoinHostPort(host, port) + "/healthz")
if err != nil {
return 1
}
res.Body.Close()
if res.StatusCode != http.StatusOK {
return 1
}
return 0
}
// spa sert le build Vite embarqué ; toute route inconnue renvoie index.html.
func spa() http.Handler {
dist, _ := fs.Sub(web.Dist, "dist")
files := http.FileServerFS(dist)
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
p := path.Clean(r.URL.Path)
if p != "/" {
if fi, err := fs.Stat(dist, p[1:]); err == nil && !fi.IsDir() {
// Les assets Vite sont nommés par empreinte : cache long.
if path.Dir(p) == "/assets" {
w.Header().Set("Cache-Control", "public, max-age=31536000, immutable")
}
files.ServeHTTP(w, r)
return
}
}
index, err := fs.ReadFile(dist, "index.html")
if err != nil {
http.Error(w, "frontend absent : lancer `npm run build` dans frontend/", http.StatusNotFound)
return
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Header().Set("Cache-Control", "no-cache")
_, _ = w.Write(index)
})
}
// public indique les chemins servis sans session : sonde, liens de partage
// (/s/<jeton> et son API), état de la connexion (/api/me, logo : page de
// connexion) et fichiers statiques du build (code de l'application, sans
// aucune donnée).
func public(p string) bool {
switch {
case p == "/healthz", strings.HasPrefix(p, "/s/"), strings.HasPrefix(p, "/api/public/"), p == "/api/me", p == "/api/logo":
return true
case strings.HasPrefix(p, "/api/"), p == "/", p == "/index.html":
return false
}
dist, _ := fs.Sub(web.Dist, "dist")
fi, err := fs.Stat(dist, strings.TrimPrefix(path.Clean(p), "/"))
return err == nil && !fi.IsDir()
}
// sameOrigin refuse les requêtes qui modifient des données et viennent d'un
// autre site (CSRF) : l'en-tête Origin d'une requête de navigateur doit
// désigner l'hôte du service. Les requêtes sans Origin (curl, jetons d'API)
// ne sont pas concernées.
func sameOrigin(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet, http.MethodHead, http.MethodOptions:
default:
if r.Header.Get("Sec-Fetch-Site") == "cross-site" {
http.Error(w, "origine refusée", http.StatusForbidden)
return
}
if o := r.Header.Get("Origin"); o != "" && o != "null" {
host := r.Host
if h := r.Header.Get("X-Forwarded-Host"); h != "" {
host, _, _ = strings.Cut(h, ",")
host = strings.TrimSpace(host)
}
if u, err := url.Parse(o); err != nil || !strings.EqualFold(u.Host, host) {
http.Error(w, "origine refusée", http.StatusForbidden)
return
}
}
}
next.ServeHTTP(w, r)
})
}
func securityHeaders(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
h := w.Header()
h.Set("X-Content-Type-Options", "nosniff")
h.Set("Referrer-Policy", "same-origin")
h.Set("X-Frame-Options", "SAMEORIGIN")
if strings.HasPrefix(r.URL.Path, "/s/") || strings.HasPrefix(r.URL.Path, "/api/public/") {
h.Set("X-Robots-Tag", "noindex, nofollow") // liens de partage
}
next.ServeHTTP(w, r)
})
}
type statusWriter struct {
http.ResponseWriter
status int
}
func (s *statusWriter) WriteHeader(code int) {
s.status = code
s.ResponseWriter.WriteHeader(code)
}
// Unwrap permet à http.ResponseController (Flush, SetWriteDeadline) d'atteindre
// l'écrivain d'origine.
func (s *statusWriter) Unwrap() http.ResponseWriter { return s.ResponseWriter }
func (s *statusWriter) Flush() {
if f, ok := s.ResponseWriter.(http.Flusher); ok {
f.Flush()
}
}
func logRequests(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
sw := &statusWriter{ResponseWriter: w, status: http.StatusOK}
next.ServeHTTP(sw, r)
if r.URL.Path != "/healthz" {
// Le jeton d'un lien de partage fait partie du chemin : il n'est pas journalisé.
p := r.URL.Path
if strings.HasPrefix(p, "/s/") {
p = "/s/…"
} else if strings.HasPrefix(p, "/api/public/") {
p = "/api/public/…"
}
slog.Info("http", "method", r.Method, "path", p, "status", sw.status,
"dur_ms", time.Since(start).Milliseconds())
}
})
}