config

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 11, 2026 License: MIT Imports: 7 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

View Source
const InsecureDefaultJWTSecret = "your_secret_key"

InsecureDefaultJWTSecret is the dev/test fallback used when JWT_SECRET is unset. It is intentionally well-known; Validate rejects it so it can never reach a real deployment. Tests and `go run` against a local DB still work.

Variables

This section is empty.

Functions

func GetAllowedOrigins

func GetAllowedOrigins() []string

GetAllowedOrigins parses LIBRARYZ_ALLOWED_ORIGINS (comma-separated) into an exact-match CORS allowlist. Empty/unset means dev mode (localhost only) — see server.cors.

func GetDatabaseUrl

func GetDatabaseUrl() string

func GetJWTPreviousSecret

func GetJWTPreviousSecret() string

GetJWTPreviousSecret is the rotated-out secret that still verifies tokens (JWT_SECRET_PREVIOUS). Empty when not rotating.

func GetJWTSecret

func GetJWTSecret() string

func GetListenAddr

func GetListenAddr() string

func GetMaxUploadBytes

func GetMaxUploadBytes() int64

func GetRecAlpha

func GetRecAlpha() float64

GetRecAlpha is the implicit-feedback confidence scale (0 → trainer default).

func GetRecFactors

func GetRecFactors() int

GetRecFactors is the latent dimension k (0 → trainer default).

func GetRecRetrainInterval

func GetRecRetrainInterval() time.Duration

GetRecRetrainInterval is how often the in-process trainer re-fits the model.

func GetStorageDir

func GetStorageDir() string

Types

type Config

type Config struct {
	DatabaseURL string
	// MigrateDatabaseURL is the DSN schema migrations run over. It must reach
	// Postgres directly (not pgbouncer in transaction-pooling mode): the
	// migrator holds a session advisory lock. Defaults to DatabaseURL.
	MigrateDatabaseURL string
	// AutoMigrate runs migrations on server startup (default). Set
	// LIBRARYZ_AUTO_MIGRATE=false when migrations run as a separate
	// `libraryz migrate` step instead.
	AutoMigrate bool
	ListenAddr  string
	StorageDir  string
	JWTSecret   string
	// JWTPreviousSecret still verifies (but never signs) tokens, for
	// zero-downtime rotation. Optional.
	JWTPreviousSecret string
	// AccessTokenTTL bounds a leaked access token's usefulness; clients renew
	// with a refresh token that lives RefreshTokenTTL.
	AccessTokenTTL  time.Duration
	RefreshTokenTTL time.Duration
	// SessionMaxAge caps a session's total lifetime however often it's
	// refreshed (LIBRARYZ_SESSION_MAX_AGE, default 365 days).
	SessionMaxAge time.Duration
	// VerifySessionOnAccess checks the session behind every access token,
	// so logout / revocation is immediate rather than at access-token
	// expiry. One extra lookup per authenticated request. Off by default:
	// LIBRARYZ_VERIFY_SESSION_ON_ACCESS=true.
	VerifySessionOnAccess bool
	// LoadSubjectOnAccess reloads the user's roles and disabled flag on
	// every authenticated request, so promotions, demotions and account
	// disabling apply at once. On by default; LIBRARYZ_LOAD_SUBJECT_ON_ACCESS
	// =false saves two lookups per request, and roles then come from the
	// access token (up to its TTL stale).
	LoadSubjectOnAccess bool
	// SessionPurgeInterval is how often expired sessions are deleted.
	SessionPurgeInterval time.Duration
	// RedisAddr (host:port) backs the login throttles shared by every
	// instance (LIBRARYZ_REDIS_ADDR, plus optional LIBRARYZ_REDIS_PASSWORD).
	// Empty means per-process in-memory throttles — fine for one instance.
	RedisAddr     string
	RedisPassword string
	// TrustedProxies (LIBRARYZ_TRUSTED_PROXIES, comma-separated CIDRs) are
	// the proxies whose X-Forwarded-For is believed for the client IP. Set
	// it behind Kong / a load balancer, or every client shares the proxy's
	// IP for per-IP login throttling.
	TrustedProxies []netip.Prefix

	MaxUploadBytes int64
	// Per-account upload quota over a rolling UploadQuotaWindow (see
	// catalog.UploadQuota). "New" accounts are unverified or younger than
	// UploadQuotaNewAccountAge.
	UploadQuotaWindow        time.Duration
	UploadQuotaFiles         int
	UploadQuotaBytes         int64
	UploadQuotaNewFiles      int
	UploadQuotaNewBytes      int64
	UploadQuotaNewAccountAge time.Duration
	// AllowedOrigins is the CORS allowlist (exact-match) for browser clients.
	// Empty means "dev mode": only localhost / 127.0.0.1 origins are allowed.
	// Set LIBRARYZ_ALLOWED_ORIGINS (comma-separated) in production.
	AllowedOrigins []string
	// CORSAllowPrivateLAN, when true, additionally allows RFC-1918 private-IP
	// origins (192.168/16, 10/8, 172.16/12) in dev mode — convenient for
	// hitting the web client over the LAN without listing every IP. Ignored
	// when AllowedOrigins is set (that path stays strict). Opt-in:
	// LIBRARYZ_CORS_ALLOW_PRIVATE_LAN=true.
	CORSAllowPrivateLAN bool
	S3Endpoint          string
	S3AccessKey         string
	S3SecretKey         string
	S3Bucket            string
	S3UseSSL            bool
	// Blob garbage collection: how often to sweep storage, and how long a
	// moderator-removed edition's file is kept (so the removal can still be
	// reverted) before it's purged.
	BlobGCInterval  time.Duration
	BlobGCRetention time.Duration
	// Recommendation training knobs.
	RecRetrainInterval time.Duration
	RecFactors         int
	RecAlpha           float64
	// HTTP server timeouts + graceful-shutdown budget. ReadHeaderTimeout is
	// the primary Slowloris defense; ReadTimeout/WriteTimeout are overridden
	// per-request on the upload/download streaming handlers (which can
	// legitimately run long) via http.ResponseController.
	ReadHeaderTimeout time.Duration
	ReadTimeout       time.Duration
	WriteTimeout      time.Duration
	IdleTimeout       time.Duration
	ShutdownTimeout   time.Duration
	// DB connection pool. MaxOpenConns must be sized so
	// instances × MaxOpenConns < Postgres max_connections (default 100) —
	// see PLAN.md Phase 6.1. MaxIdleConns is clamped to MaxOpenConns in db.
	DBMaxOpenConns    int
	DBMaxIdleConns    int
	DBConnMaxLifetime time.Duration
	// PDF sanitization runs in a child process (internal/sanitize
	// EnableIsolation). Worst-case memory for sanitization is
	// PDFSanitizeConcurrency × PDFSanitizeMemoryMB. The default budget fits
	// the largest allowed upload (LIBRARYZ_MAX_UPLOAD_BYTES, 500 MiB);
	// raise both together.
	PDFSanitizeMemoryMB    int
	PDFSanitizeTimeout     time.Duration
	PDFSanitizeConcurrency int
	// contains filtered or unexported fields
}

func Load

func Load() *Config

func (*Config) Validate

func (c *Config) Validate() error

Validate fails fast on configuration that is safe for tests but dangerous in a real deployment. Call it from main() before serving; the library path (server.New, used by tests) deliberately does not, so the dev fallback secret keeps working there.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL