| 1 | // Command example is abono's example app: sign-in by emailed link, the |
| 2 | // starter pages `abono pages` wrote (abono_pages.go, templates/abono/), |
| 3 | // and the sandbox standing in for Stripe, so the whole flow is clickable |
| 4 | // with no Stripe account. Run it with `go run .` and open |
| 5 | // http://localhost:8080. Nothing is mailed: each sign-in link is written |
| 6 | // to the log instead, to be opened from there. |
| 7 | // |
| 8 | // Its settings are environment variables, all optional: |
| 9 | // |
| 10 | // ABONO_EXAMPLE_ADDR where to listen; 127.0.0.1:8080 |
| 11 | // ABONO_EXAMPLE_ORIGIN the address people use; http://localhost:8080 |
| 12 | // ABONO_EXAMPLE_OWNER the account that sees the owner's pages; owner@example.com |
| 13 | // ABONO_EXAMPLE_PLANS what is for sale; DefaultPlans |
| 14 | // ABONO_EXAMPLE_DB the database file; example.db |
| 15 | // ABONO_EXAMPLE_INSTANCE_KEY seals sign-in state; a development-only key |
| 16 | // STRIPE_KEY, STRIPE_WEBHOOK_SECRET a Stripe test account instead of the sandbox |
| 17 | package main |
| 18 | |
| 19 | import ( |
| 20 | "log/slog" |
| 21 | "os" |
| 22 | |
| 23 | "amadan.net/rastrillo/rastrillo" |
| 24 | "amadan.net/rastrillo/rastrillo/db" |
| 25 | ) |
| 26 | |
| 27 | func main() { |
| 28 | logger := slog.New(slog.NewTextHandler(os.Stderr, nil)) |
| 29 | d, err := db.Open(env("ABONO_EXAMPLE_DB", "example.db"), logger) |
| 30 | if err != nil { |
| 31 | logger.Error("open database", "err", err) |
| 32 | os.Exit(1) |
| 33 | } |
| 34 | defer d.Close() |
| 35 | |
| 36 | var secrets []string |
| 37 | if s := os.Getenv("STRIPE_WEBHOOK_SECRET"); s != "" { |
| 38 | secrets = []string{s} |
| 39 | } |
| 40 | instanceKey := os.Getenv("ABONO_EXAMPLE_INSTANCE_KEY") |
| 41 | if instanceKey == "" { |
| 42 | instanceKey = "abono-example-development-only-instance-key" |
| 43 | logger.Warn("ABONO_EXAMPLE_INSTANCE_KEY not set; using a development-only key") |
| 44 | } |
| 45 | opts, err := Options(Config{ |
| 46 | DB: d, |
| 47 | InstanceKey: instanceKey, |
| 48 | Origin: env("ABONO_EXAMPLE_ORIGIN", "http://localhost:8080"), |
| 49 | Owner: env("ABONO_EXAMPLE_OWNER", "owner@example.com"), |
| 50 | Plans: os.Getenv("ABONO_EXAMPLE_PLANS"), |
| 51 | Key: os.Getenv("STRIPE_KEY"), |
| 52 | WebhookSecrets: secrets, |
| 53 | Logger: logger, |
| 54 | }) |
| 55 | if err != nil { |
| 56 | logger.Error("build app", "err", err) |
| 57 | os.Exit(1) |
| 58 | } |
| 59 | // Loopback by default: rastrillo's own default is every interface. |
| 60 | opts.Addr = env("ABONO_EXAMPLE_ADDR", "127.0.0.1:8080") |
| 61 | if err := rastrillo.Serve(opts); err != nil { |
| 62 | logger.Error("serve", "err", err) |
| 63 | os.Exit(1) |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | func env(name, def string) string { |
| 68 | if v := os.Getenv(name); v != "" { |
| 69 | return v |
| 70 | } |
| 71 | return def |
| 72 | } |
| 73 | |