// Command preview serves the REAL app (real handlers, templates, store // and index) over a throwaway database of synthetic pages, already signed // in, on loopback. It exists so a screen can be looked at before it // ships: no test can see a layout. // // go run ./scripts/preview # then open http://127.0.0.1:8747/ // // PREVIEW_PAGES=10000 adds that many synthetic team pages, to look at and // time search at gate 6's size. Signed in through AppForTest, which says // what it does in its name; this binary binds loopback only. package main import ( "context" "crypto/rand" "encoding/base64" "fmt" "io" "log/slog" "net/http" "os" "path/filepath" "strconv" "amadan.net/rastrillo/rastrillo/blobs" "amadan.net/rastrillo/rastrillo/db" "amadan.net/oficina/mapa/internal/mapa" ) const owner = "paul@example.com" // address is where the preview listens: PREVIEW_ADDR, else loopback // port 8747. It is always loopback; a tailnet proxy may front it. var address = func() string { if given := os.Getenv("PREVIEW_ADDR"); given != "" { return given } return "127.0.0.1:8747" }() func main() { if err := run(); err != nil { fmt.Fprintln(os.Stderr, "preview:", err) os.Exit(1) } } func run() error { dir, err := os.MkdirTemp("", "mapa-preview-") if err != nil { return err } defer os.RemoveAll(dir) config, err := previewConfig() if err != nil { return err } logger := slog.New(slog.NewTextHandler(io.Discard, nil)) database, err := db.Open(filepath.Join(dir, "preview.db"), logger) if err != nil { return err } // Images go to a directory beside the database, as in development // (images decision 4): CARLOS's bucket is not this preview's. if config.Images.Objects, err = blobs.Dir(filepath.Join(dir, "images")); err != nil { return err } built, err := mapa.AppForTest(database, config, logger, owner) if err != nil { return err } extra, _ := strconv.Atoi(os.Getenv("PREVIEW_PAGES")) if err := seed(built.Store, extra); err != nil { return err } fmt.Println("mapa preview on http://" + address + "/ (synthetic, " + "signed in as " + owner + ")") return http.ListenAndServe(address, built.Mux) } func previewConfig() (mapa.Config, error) { dek := make([]byte, 32) if _, err := rand.Read(dek); err != nil { return mapa.Config{}, err } env := map[string]string{ "MAPA_DEK": base64.StdEncoding.EncodeToString(dek), "MAPA_INSTANCE_KEY": "preview", "MAPA_OWNERS": owner, "MAPA_ORIGIN": "http://" + address, "MAPA_ALLOW_LOGGED_MAIL": "1", "MAPA_TIME_ZONE": "Europe/Dublin", } // Behind a tailnet proxy the origin is the proxy's. Logged mail is // refused off loopback, so the preview names an SMTP server that // is never reached: it signs no one in by mail anyway. if origin := os.Getenv("MAPA_ORIGIN"); origin != "" { env["MAPA_ORIGIN"] = origin delete(env, "MAPA_ALLOW_LOGGED_MAIL") env["CARLOS_SMTP_HOST"], env["CARLOS_SMTP_PORT"] = "127.0.0.1", "1" env["CARLOS_SMTP_FROM"] = "noreply@preview.invalid" } return mapa.LoadConfig(func(key string) string { return env[key] }) } // seed writes synthetic pages through the real store, so the preview // exercises the write path production does, events and all. func seed(store *mapa.Store, extra int) error { ctx := context.Background() for _, page := range samples { if _, err := store.CreateTeamPage(ctx, owner, page[0], page[1]); err != nil { return err } } return mapa.SeedSynthetic(ctx, store, owner, extra) } var samples = [][2]string{ {"Brightwater Labs", "# Brightwater Labs\n\nA customer since 2024. " + "Ana is the budget holder; Tom signs.\n\n- Renewal is tied to " + "their grant cycle\n- Pilot scoped to one department ^pilot\n\n" + "See [[Halden Robotics]] for the other pilot, and " + "[[Pricing 2027]], which is not written yet."}, {"Halden Robotics", "Robots for warehouses. Met them through " + "Brightwater Labs at the conference.\n\n> They want the pilot " + "in Q1.\n\nThe pilot block: [[Brightwater Labs#^pilot]]."}, {"Onboarding", "1. Read [[Brightwater Labs]]\n2. Sit in on a call\n" + "3. Write up what you learned"}, }