| 1 | // Command preview serves the REAL app (real handlers, templates, store |
| 2 | // and index) over a throwaway database of synthetic pages, already signed |
| 3 | // in, on loopback. It exists so a screen can be looked at before it |
| 4 | // ships: no test can see a layout. |
| 5 | // |
| 6 | // go run ./scripts/preview # then open http://127.0.0.1:8747/ |
| 7 | // |
| 8 | // PREVIEW_PAGES=10000 adds that many synthetic team pages, to look at and |
| 9 | // time search at gate 6's size. Signed in through AppForTest, which says |
| 10 | // what it does in its name; this binary binds loopback only. |
| 11 | package main |
| 12 | |
| 13 | import ( |
| 14 | "context" |
| 15 | "crypto/rand" |
| 16 | "encoding/base64" |
| 17 | "fmt" |
| 18 | "io" |
| 19 | "log/slog" |
| 20 | "net/http" |
| 21 | "os" |
| 22 | "path/filepath" |
| 23 | "strconv" |
| 24 | |
| 25 | "amadan.net/rastrillo/rastrillo/blobs" |
| 26 | "amadan.net/rastrillo/rastrillo/db" |
| 27 | |
| 28 | "amadan.net/oficina/mapa/internal/mapa" |
| 29 | ) |
| 30 | |
| 31 | const owner = "paul@example.com" |
| 32 | |
| 33 | // address is where the preview listens: PREVIEW_ADDR, else loopback |
| 34 | // port 8747. It is always loopback; a tailnet proxy may front it. |
| 35 | var address = func() string { |
| 36 | if given := os.Getenv("PREVIEW_ADDR"); given != "" { |
| 37 | return given |
| 38 | } |
| 39 | return "127.0.0.1:8747" |
| 40 | }() |
| 41 | |
| 42 | func main() { |
| 43 | if err := run(); err != nil { |
| 44 | fmt.Fprintln(os.Stderr, "preview:", err) |
| 45 | os.Exit(1) |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | func run() error { |
| 50 | dir, err := os.MkdirTemp("", "mapa-preview-") |
| 51 | if err != nil { |
| 52 | return err |
| 53 | } |
| 54 | defer os.RemoveAll(dir) |
| 55 | config, err := previewConfig() |
| 56 | if err != nil { |
| 57 | return err |
| 58 | } |
| 59 | logger := slog.New(slog.NewTextHandler(io.Discard, nil)) |
| 60 | database, err := db.Open(filepath.Join(dir, "preview.db"), logger) |
| 61 | if err != nil { |
| 62 | return err |
| 63 | } |
| 64 | // Images go to a directory beside the database, as in development |
| 65 | // (images decision 4): CARLOS's bucket is not this preview's. |
| 66 | if config.Images.Objects, err = blobs.Dir(filepath.Join(dir, |
| 67 | "images")); err != nil { |
| 68 | return err |
| 69 | } |
| 70 | built, err := mapa.AppForTest(database, config, logger, owner) |
| 71 | if err != nil { |
| 72 | return err |
| 73 | } |
| 74 | extra, _ := strconv.Atoi(os.Getenv("PREVIEW_PAGES")) |
| 75 | if err := seed(built.Store, extra); err != nil { |
| 76 | return err |
| 77 | } |
| 78 | fmt.Println("mapa preview on http://" + address + "/ (synthetic, " + |
| 79 | "signed in as " + owner + ")") |
| 80 | return http.ListenAndServe(address, built.Mux) |
| 81 | } |
| 82 | |
| 83 | func previewConfig() (mapa.Config, error) { |
| 84 | dek := make([]byte, 32) |
| 85 | if _, err := rand.Read(dek); err != nil { |
| 86 | return mapa.Config{}, err |
| 87 | } |
| 88 | env := map[string]string{ |
| 89 | "MAPA_DEK": base64.StdEncoding.EncodeToString(dek), |
| 90 | "MAPA_INSTANCE_KEY": "preview", |
| 91 | "MAPA_OWNERS": owner, |
| 92 | "MAPA_ORIGIN": "http://" + address, |
| 93 | "MAPA_ALLOW_LOGGED_MAIL": "1", |
| 94 | "MAPA_TIME_ZONE": "Europe/Dublin", |
| 95 | } |
| 96 | // Behind a tailnet proxy the origin is the proxy's. Logged mail is |
| 97 | // refused off loopback, so the preview names an SMTP server that |
| 98 | // is never reached: it signs no one in by mail anyway. |
| 99 | if origin := os.Getenv("MAPA_ORIGIN"); origin != "" { |
| 100 | env["MAPA_ORIGIN"] = origin |
| 101 | delete(env, "MAPA_ALLOW_LOGGED_MAIL") |
| 102 | env["CARLOS_SMTP_HOST"], env["CARLOS_SMTP_PORT"] = "127.0.0.1", "1" |
| 103 | env["CARLOS_SMTP_FROM"] = "noreply@preview.invalid" |
| 104 | } |
| 105 | return mapa.LoadConfig(func(key string) string { return env[key] }) |
| 106 | } |
| 107 | |
| 108 | // seed writes synthetic pages through the real store, so the preview |
| 109 | // exercises the write path production does, events and all. |
| 110 | func seed(store *mapa.Store, extra int) error { |
| 111 | ctx := context.Background() |
| 112 | for _, page := range samples { |
| 113 | if _, err := store.CreateTeamPage(ctx, owner, page[0], |
| 114 | page[1]); err != nil { |
| 115 | return err |
| 116 | } |
| 117 | } |
| 118 | return mapa.SeedSynthetic(ctx, store, owner, extra) |
| 119 | } |
| 120 | |
| 121 | var samples = [][2]string{ |
| 122 | {"Brightwater Labs", "# Brightwater Labs\n\nA customer since 2024. " + |
| 123 | "Ana is the budget holder; Tom signs.\n\n- Renewal is tied to " + |
| 124 | "their grant cycle\n- Pilot scoped to one department ^pilot\n\n" + |
| 125 | "See [[Halden Robotics]] for the other pilot, and " + |
| 126 | "[[Pricing 2027]], which is not written yet."}, |
| 127 | {"Halden Robotics", "Robots for warehouses. Met them through " + |
| 128 | "Brightwater Labs at the conference.\n\n> They want the pilot " + |
| 129 | "in Q1.\n\nThe pilot block: [[Brightwater Labs#^pilot]]."}, |
| 130 | {"Onboarding", "1. Read [[Brightwater Labs]]\n2. Sit in on a call\n" + |
| 131 | "3. Write up what you learned"}, |
| 132 | } |
| 133 | |