Turn the raw pages stored by Phase 2 into rows in the SQLite items table (AGENTS.md §11 Phase 3, FR-3/FR-4/FR-5/FR-12). - internal/parse: dependency-free HTML reader plus extractors for the ExtJS learning-task grid (subject, title, category, kind, due date, status), the home news feed (author, posted time, title, body) and the calendar week grid (day column + grid offset give the time; label gives title/room/teacher). - internal/parse: fail loudly. A challenge page, login page, changed layout or unknown due-date format is an explicit error, never an empty day. - FR-4: structure is parsed deterministically; importance is classified by an OmniRoute model (default openrouter/google/gemini-3.5-flash-lite) with a deterministic fallback. The classifier used is printed and recorded. - internal/llm: small OpenAI-compatible OmniRoute client (handles SSE replies). - internal/store: migration 002 adds source_page, importance and external_ref with a unique (child_id, source, type, external_ref) index; UpsertItems, CountItemsByType, SampleItems and ChildByName. - cmd/school parse: reads var/raw/<child>/<run>, upserts items, records the scan row (ok=0 plus the error on failure) and prints counts and samples. - scripts/parse-compass.sh and make parse run it in a one-off container against the service's own volume without restarting the service. - docs/phase-3-parse-notes.md records the approach, the FR-4 decision and the known gaps. Evidence on the live database: 56 rows for Harry (20 task, 1 message, 35 event). Re-running the same raw pages inserts 0 and updates 56. Co-Authored-By: Paperclip <noreply@paperclip.ing>
150 lines
4.3 KiB
Go
150 lines
4.3 KiB
Go
// Package config loads the Compass/NHS service configuration from the process
|
|
// environment. Every secret comes from the environment (environment.d); nothing
|
|
// is compiled into the binary and nothing secret is read from a committed file.
|
|
package config
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"log/slog"
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
// Embed the IANA time zone database so SCHOOL_TIMEZONE resolves even in a
|
|
// container without tzdata.
|
|
_ "time/tzdata"
|
|
)
|
|
|
|
// Config is the fully resolved runtime configuration.
|
|
type Config struct {
|
|
// Addr is the listen address, for example ":8792".
|
|
Addr string
|
|
// DBPath is the SQLite database file, for example "/data/school.db".
|
|
DBPath string
|
|
// SessionSecret signs the session cookie. Required, >= 16 bytes.
|
|
SessionSecret []byte
|
|
// AdminUsername is the single dashboard login name (Sam).
|
|
AdminUsername string
|
|
// AdminPassword is the plaintext password. Optional if AdminPasswordHash is set.
|
|
AdminPassword string
|
|
// AdminPasswordHash is a bcrypt hash. Optional if AdminPassword is set.
|
|
AdminPasswordHash string
|
|
// CookieSecure sets the Secure attribute on cookies. Keep true behind HTTPS.
|
|
CookieSecure bool
|
|
// SessionTTL is how long a session cookie stays valid.
|
|
SessionTTL time.Duration
|
|
// LogLevel is the slog level.
|
|
LogLevel slog.Level
|
|
// AppEnv is a label such as "production" or "development".
|
|
AppEnv string
|
|
// Version is injected at build time.
|
|
Version string
|
|
// Location is the family timezone. Due dates and briefs are wall-clock, so
|
|
// this must be the family's zone, not the container default of UTC.
|
|
Location *time.Location
|
|
}
|
|
|
|
// DefaultTimezone is the family timezone used when SCHOOL_TIMEZONE is unset.
|
|
const DefaultTimezone = "Australia/Sydney"
|
|
|
|
// Load reads and validates the configuration.
|
|
func Load() (*Config, error) {
|
|
cfg := &Config{
|
|
Addr: env("SCHOOL_ADDR", ":8792"),
|
|
DBPath: env("SCHOOL_DB_PATH", "/data/school.db"),
|
|
AdminUsername: env("SCHOOL_ADMIN_USERNAME", "sam"),
|
|
AdminPassword: os.Getenv("SCHOOL_ADMIN_PASSWORD"),
|
|
AdminPasswordHash: os.Getenv("SCHOOL_ADMIN_PASSWORD_HASH"),
|
|
CookieSecure: envBool("SCHOOL_COOKIE_SECURE", true),
|
|
AppEnv: env("SCHOOL_APP_ENV", "production"),
|
|
}
|
|
|
|
secret := os.Getenv("SCHOOL_SESSION_SECRET")
|
|
if len(secret) < 16 {
|
|
return nil, errors.New("SCHOOL_SESSION_SECRET must be set and at least 16 characters")
|
|
}
|
|
cfg.SessionSecret = []byte(secret)
|
|
|
|
if cfg.AdminPassword == "" && cfg.AdminPasswordHash == "" {
|
|
return nil, errors.New("set SCHOOL_ADMIN_PASSWORD or SCHOOL_ADMIN_PASSWORD_HASH")
|
|
}
|
|
if cfg.AdminUsername == "" {
|
|
return nil, errors.New("SCHOOL_ADMIN_USERNAME must not be empty")
|
|
}
|
|
|
|
ttl, err := time.ParseDuration(env("SCHOOL_SESSION_TTL", "12h"))
|
|
if err != nil {
|
|
return nil, fmt.Errorf("SCHOOL_SESSION_TTL: %w", err)
|
|
}
|
|
cfg.SessionTTL = ttl
|
|
|
|
lvl, err := parseLevel(env("SCHOOL_LOG_LEVEL", "info"))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
cfg.LogLevel = lvl
|
|
|
|
loc, err := time.LoadLocation(env("SCHOOL_TIMEZONE", DefaultTimezone))
|
|
if err != nil {
|
|
return nil, fmt.Errorf("SCHOOL_TIMEZONE: %w", err)
|
|
}
|
|
cfg.Location = loc
|
|
|
|
return cfg, nil
|
|
}
|
|
|
|
// DBConfig is the minimum a database-only command needs. The parse subcommand
|
|
// opens the same SQLite file as the service without requiring the service's
|
|
// session secret or login.
|
|
type DBConfig struct {
|
|
DBPath string
|
|
Location *time.Location
|
|
}
|
|
|
|
// LoadDB reads the database path and the family timezone only.
|
|
func LoadDB() (*DBConfig, error) {
|
|
loc, err := time.LoadLocation(env("SCHOOL_TIMEZONE", DefaultTimezone))
|
|
if err != nil {
|
|
return nil, fmt.Errorf("SCHOOL_TIMEZONE: %w", err)
|
|
}
|
|
return &DBConfig{
|
|
DBPath: env("SCHOOL_DB_PATH", "/data/school.db"),
|
|
Location: loc,
|
|
}, nil
|
|
}
|
|
|
|
func env(key, fallback string) string {
|
|
if v := strings.TrimSpace(os.Getenv(key)); v != "" {
|
|
return v
|
|
}
|
|
return fallback
|
|
}
|
|
|
|
func envBool(key string, fallback bool) bool {
|
|
v := strings.TrimSpace(os.Getenv(key))
|
|
if v == "" {
|
|
return fallback
|
|
}
|
|
b, err := strconv.ParseBool(v)
|
|
if err != nil {
|
|
return fallback
|
|
}
|
|
return b
|
|
}
|
|
|
|
func parseLevel(s string) (slog.Level, error) {
|
|
switch strings.ToLower(strings.TrimSpace(s)) {
|
|
case "debug":
|
|
return slog.LevelDebug, nil
|
|
case "info", "":
|
|
return slog.LevelInfo, nil
|
|
case "warn", "warning":
|
|
return slog.LevelWarn, nil
|
|
case "error":
|
|
return slog.LevelError, nil
|
|
default:
|
|
return 0, fmt.Errorf("SCHOOL_LOG_LEVEL: unknown level %q", s)
|
|
}
|
|
}
|