Fetch and parse Harry and Finn with a stored Compass session per child and store every item against the right children.id. - cmd/school/session.go + internal/store/session.go: read and write the per-child harness session state in the SQLite `session` table. `show` prints metadata only; the state is never printed. - scripts/compass-fetch-patchright.mjs: reuse the stored session, refresh it after login, resolve the child's userId from the parent page by name, and keep the shared calendar out of the per-child run (it would leak the eldest's timetable into the younger child). - scripts/scan-compass.sh + make scan: scan one child or both; restore the session, fetch, persist the session, then parse. A failing child is recorded in `scans` with ok = 0 and the script exits non-zero (FR-12). - cmd/school/scan.go: `school scan record` writes a fetch failure to the scans table. cmd/school/items.go: `school items purge` removes rows that a shared page stored against the wrong child. - internal/store/store_test.go: prove items and sessions are separated by child. docs/phase-4-both-children-notes.md; README + image tag 0.3.0-phase4. Live evidence: Harry 70 tasks / 1 message / 35 events (6 overdue); Finn 42 tasks / 1 message / 0 events (2 overdue). Finn's subjects are Year 7, Harry's are Year 11. Both sessions stored. Co-Authored-By: Paperclip <noreply@paperclip.ing>
180 lines
4.3 KiB
Go
180 lines
4.3 KiB
Go
// Command school is the Compass / NHS service (school.lab.audasmedia.com.au).
|
|
//
|
|
// Phase 1 skeleton: Go service, SQLite schema (AGENTS.md §7), the service's own
|
|
// session login, HTMX server-rendered shell, and GET /health. It does not scan
|
|
// the school; the Compass login and fetch are Phase 2.
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"crypto/rand"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"log/slog"
|
|
"net/http"
|
|
"os"
|
|
"os/signal"
|
|
"syscall"
|
|
"time"
|
|
|
|
"golang.org/x/crypto/bcrypt"
|
|
|
|
"gitea.lab.audasmedia.com.au/sam/compass_teams_nhs/internal/auth"
|
|
"gitea.lab.audasmedia.com.au/sam/compass_teams_nhs/internal/config"
|
|
"gitea.lab.audasmedia.com.au/sam/compass_teams_nhs/internal/store"
|
|
"gitea.lab.audasmedia.com.au/sam/compass_teams_nhs/internal/web"
|
|
)
|
|
|
|
// version is injected with -ldflags at build time.
|
|
var version = "dev"
|
|
|
|
// seedChildren is the fixed set from AGENTS.md: Harry and Finn.
|
|
var seedChildren = []string{"Harry", "Finn"}
|
|
|
|
func main() {
|
|
if len(os.Args) > 1 {
|
|
switch os.Args[1] {
|
|
case "hash-password":
|
|
hashPassword(os.Args[2:])
|
|
return
|
|
case "gen-secret":
|
|
genSecret()
|
|
return
|
|
case "parse":
|
|
parseCommand(os.Args[2:])
|
|
return
|
|
case "session":
|
|
sessionCommand(os.Args[2:])
|
|
return
|
|
case "scan":
|
|
scanCommand(os.Args[2:])
|
|
return
|
|
case "items":
|
|
itemsCommand(os.Args[2:])
|
|
return
|
|
case "version":
|
|
fmt.Println(version)
|
|
return
|
|
}
|
|
}
|
|
if err := run(); err != nil {
|
|
fmt.Fprintln(os.Stderr, "school:", err)
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
func run() error {
|
|
started := time.Now()
|
|
|
|
cfg, err := config.Load()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
cfg.Version = version
|
|
|
|
log := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: cfg.LogLevel}))
|
|
slog.SetDefault(log)
|
|
|
|
st, err := store.Open(cfg.DBPath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer st.Close()
|
|
|
|
if err := st.Migrate(context.Background()); err != nil {
|
|
return err
|
|
}
|
|
if err := st.SeedChildren(context.Background(), seedChildren...); err != nil {
|
|
return err
|
|
}
|
|
|
|
am, err := auth.New(cfg.SessionSecret, cfg.SessionTTL, cfg.CookieSecure,
|
|
cfg.AdminUsername, cfg.AdminPasswordHash, cfg.AdminPassword)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !am.Hashed() {
|
|
log.Warn("admin password is plaintext; set SCHOOL_ADMIN_PASSWORD_HASH to use bcrypt")
|
|
}
|
|
|
|
srv, err := web.New(cfg, st, am, log)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
httpServer := &http.Server{
|
|
Addr: cfg.Addr,
|
|
Handler: srv.Handler(),
|
|
ReadHeaderTimeout: 10 * time.Second,
|
|
ReadTimeout: 30 * time.Second,
|
|
WriteTimeout: 30 * time.Second,
|
|
IdleTimeout: 60 * time.Second,
|
|
}
|
|
|
|
errCh := make(chan error, 1)
|
|
go func() {
|
|
log.Info("school service listening",
|
|
"addr", cfg.Addr, "db", cfg.DBPath, "env", cfg.AppEnv, "version", version)
|
|
if err := httpServer.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
|
errCh <- err
|
|
}
|
|
}()
|
|
|
|
stop := make(chan os.Signal, 1)
|
|
signal.Notify(stop, syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
select {
|
|
case err := <-errCh:
|
|
return err
|
|
case sig := <-stop:
|
|
log.Info("shutting down", "signal", sig.String())
|
|
}
|
|
|
|
ctxShutdown, cancelShutdown := context.WithTimeout(context.Background(), 10*time.Second)
|
|
defer cancelShutdown()
|
|
if err := httpServer.Shutdown(ctxShutdown); err != nil {
|
|
return fmt.Errorf("shutdown: %w", err)
|
|
}
|
|
log.Info("stopped", "uptime", time.Since(started).Round(time.Second).String())
|
|
return nil
|
|
}
|
|
|
|
// hashPassword prints a bcrypt hash for the password given as an argument or
|
|
// read from stdin. It never prints the password itself.
|
|
func hashPassword(args []string) {
|
|
var password string
|
|
if len(args) > 0 {
|
|
password = args[0]
|
|
} else {
|
|
b, err := io.ReadAll(os.Stdin)
|
|
if err != nil {
|
|
fmt.Fprintln(os.Stderr, "school:", err)
|
|
os.Exit(1)
|
|
}
|
|
password = string(b)
|
|
}
|
|
if password == "" {
|
|
fmt.Fprintln(os.Stderr, "school: empty password")
|
|
os.Exit(2)
|
|
}
|
|
hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
|
if err != nil {
|
|
fmt.Fprintln(os.Stderr, "school:", err)
|
|
os.Exit(1)
|
|
}
|
|
fmt.Println(string(hash))
|
|
}
|
|
|
|
// genSecret prints a fresh 32-byte hex secret. Use it to set
|
|
// SCHOOL_SESSION_SECRET without pasting a secret into a shell history file by
|
|
// hand. It never reads or stores anything.
|
|
func genSecret() {
|
|
b := make([]byte, 32)
|
|
if _, err := rand.Read(b); err != nil {
|
|
fmt.Fprintln(os.Stderr, "school:", err)
|
|
os.Exit(1)
|
|
}
|
|
fmt.Printf("%x\n", b)
|
|
}
|