// Command preview serves the REAL app — real handlers, templates and // store — over a throwaway database of synthetic customers, already signed // in, on loopback. It exists so a screen can be looked at before it ships: // the first index went live with a bullet point and an underlined link // that no test could see. // // go run ./scripts/preview # then open http://127.0.0.1:8746/ // // Signed in through AppForTest, which says what it does in its name; this // binary binds 127.0.0.1 only and refuses to run otherwise. package main import ( "context" "crypto/rand" "encoding/base64" "fmt" "io" "log/slog" "net/http" "os" "path/filepath" "strconv" fichas "amadan.net/oficina/fichas/internal/fichas" "amadan.net/rastrillo/rastrillo/db" ) const ( addr = "127.0.0.1:8746" owner = "paul@example.com" ) func main() { dir, err := os.MkdirTemp("", "fichas-preview-") check(err) defer os.RemoveAll(dir) dek := make([]byte, 32) _, err = rand.Read(dek) check(err) env := map[string]string{ "FICHAS_DEK": base64.StdEncoding.EncodeToString(dek), "FICHAS_INSTANCE_KEY": "preview", "FICHAS_OWNERS": owner, "FICHAS_ORIGIN": "http://" + addr, "FICHAS_ALLOW_LOGGED_MAIL": "1", } cfg, err := fichas.LoadConfig(func(k string) string { return env[k] }) check(err) logger := slog.New(slog.NewTextHandler(io.Discard, nil)) d, err := db.Open(filepath.Join(dir, "preview.db"), logger) check(err) mux, err := fichas.AppForTest(d, cfg, logger, owner) check(err) check(seed(d, cfg)) fmt.Println("fichas preview on http://" + addr + "/ (synthetic data, signed in as " + owner + ")") check(http.ListenAndServe(addr, mux)) } // seed writes synthetic customers through the real store, so the preview // exercises the same write path — events and all — that production does. // .example is RFC 2606's reserved TLD. func seed(d *db.DB, cfg fichas.Config) error { ctx := context.Background() m, err := fichas.EnsureMember(ctx, d.G, cfg.Sealer, owner) if err != nil { return err } me := fichas.Person(m.ID) customers := []struct { values map[string]string calls []string }{ {map[string]string{fichas.FieldName: "Brightwater Labs", fichas.FieldDomain: "brightwater.example", fichas.FieldEmail: "ana@brightwater.example", fichas.FieldPhone: "+353 1 555 0142", fichas.FieldStage: "Proposal", fichas.FieldNextAction: "2026-10-02", fichas.FieldNotes: "Renewal is tied to their grant cycle. Ana is the budget holder; Tom signs."}, []string{"Met at the conference. Asked for a follow-up after their board meets.", "Intro call. Current tool is a shared spreadsheet; they lose track of follow-ups.", "Walked Ana through the proposal. She wants the pilot scoped to one department first."}}, {map[string]string{fichas.FieldName: "Cedar & Pike Architects", fichas.FieldDomain: "cedarpike.example", fichas.FieldEmail: "office@cedarpike.example", fichas.FieldStage: "Lead"}, nil}, {map[string]string{fichas.FieldName: "Halden Robotics", fichas.FieldDomain: "halden.example", fichas.FieldEmail: "ops@halden.example", fichas.FieldPhone: "+44 20 5550 1877", fichas.FieldStage: "Contacted", fichas.FieldNextAction: "2026-09-30"}, []string{"Left a voicemail; followed up by email."}}, {map[string]string{fichas.FieldName: "Oriel Housing Trust", fichas.FieldDomain: "oriel.example", fichas.FieldEmail: "it@oriel.example", fichas.FieldStage: "Won"}, []string{"Signed. Rollout starts next month."}}, } // PREVIEW_EXTRA=5000 adds that many more synthetic customers, to look at // and time the list at a realistic size. if n, _ := strconv.Atoi(os.Getenv("PREVIEW_EXTRA")); n > 0 { stages := []string{"Lead", "Contacted", "Proposal", "Won", "Lost"} for i := 0; i < n; i++ { customers = append(customers, struct { values map[string]string calls []string }{map[string]string{ fichas.FieldName: fmt.Sprintf("Company %04d", i), fichas.FieldEmail: fmt.Sprintf("hello%04d@company%04d.example", i, i), fichas.FieldStage: stages[i%5], fichas.FieldNotes: "Renewal is tied to their grant cycle; the budget holder signs off in March.", }, nil}) } } for _, c := range customers { card, err := fichas.CreateCard(ctx, d.G, cfg.Sealer, fichas.TypeCustomer, me, c.values) if err != nil { return err } for _, body := range c.calls { if _, err := fichas.AddEntry(ctx, d.G, cfg.Sealer, card.ID, me, fichas.TypeCallLog, "manual", map[string]string{fichas.FieldBody: body}); err != nil { return err } } } return nil } func check(err error) { if err != nil { fmt.Fprintln(os.Stderr, "preview:", err) os.Exit(1) } }