| 1 | # todo-haskell |
| 2 | |
| 3 | TodoMVC for the CARLOS bakeoff, in Haskell. The server renders every |
| 4 | page, and every action is a plain HTML form, so the app works with |
| 5 | JavaScript turned off. A small ES module (`static/app.js`) takes over |
| 6 | when JavaScript is on, so it behaves like TodoMVC's own apps. |
| 7 | |
| 8 | Live at |
| 9 | [https://todo-haskell.bfj.oncarlos.com](https://todo-haskell.bfj.oncarlos.com). |
| 10 | |
| 11 | ## The stack |
| 12 | |
| 13 | | Piece | Choice | Why | |
| 14 | |---|---|---| |
| 15 | | HTTP server | [warp](https://hackage.haskell.org/package/warp) + [wai](https://hackage.haskell.org/package/wai) | The standard Haskell server. It serves a socket you hand it, so a unix socket is no different from TCP, and it has a graceful-shutdown hook for SIGTERM | |
| 16 | | Routing | Pattern matching on `pathInfo` | Eleven routes. A case expression over the path segments is shorter and clearer than a routing library | |
| 17 | | HTML | [lucid2](https://hackage.haskell.org/package/lucid2) | Templates are ordinary Haskell functions, and text is escaped unless you ask otherwise. It depends on nothing outside GHC's own libraries | |
| 18 | | SQLite | [sqlite-simple](https://hackage.haskell.org/package/sqlite-simple) | The usual binding. Its `direct-sqlite` dependency compiles SQLite's C source into the binary, so there's no system library to find | |
| 19 | | Cookies | [cookie](https://hackage.haskell.org/package/cookie) | Parses `Cookie` and renders `Set-Cookie` correctly | |
| 20 | | Forms | `parseSimpleQuery` from http-types | Every body is `application/x-www-form-urlencoded`, so the query parser is all it takes | |
| 21 | | Static files | [file-embed](https://hackage.haskell.org/package/file-embed) | The CSS and JS are compiled into the binary. The two TodoMVC stylesheets are embedded straight from `shared/`, so they're served byte for byte | |
| 22 | | Randomness | `getrandom(2)` through the FFI | One libc call, no library, and it doesn't need `/dev/urandom` inside the sandbox | |
| 23 | | Tests | [hspec](https://hackage.haskell.org/package/hspec) with wai-extra's `Network.Wai.Test` | Drives the WAI application in-process against an in-memory database, cookies and all | |
| 24 | |
| 25 | The one SQLite connection sits behind an `MVar`. SQLite allows one writer |
| 26 | at a time anyway, and a request here is a few indexed statements, so a |
| 27 | pool would add code without adding speed. |
| 28 | |
| 29 | Warp's `x509` flag is turned off in `cabal.project`. It only exposes TLS |
| 30 | client certificates, and it would pull in a whole crypto library; the |
| 31 | Carloku edge terminates TLS. |
| 32 | |
| 33 | ## How it fits the contract |
| 34 | |
| 35 | - **Lists.** The first visit mints a 256-bit random token, stores it in |
| 36 | `lists`, and sets it as the `todo_list` cookie. Every query about todos |
| 37 | is scoped by that list's id, so a POST naming someone else's todo |
| 38 | matches nothing and answers 404. A cookie the app doesn't hold gets a |
| 39 | fresh list. |
| 40 | - **Origin check.** A POST with an `Origin` header that isn't the app's |
| 41 | own answers 403. The own origin is `https://$CARLOS_HOST` with |
| 42 | `--socket`, and `http://<address>` with `--listen`. |
| 43 | - **Without JavaScript.** Each todo's toggle is a transparent submit |
| 44 | button laid exactly over TodoMVC's checkbox, and the toggle-all button |
| 45 | sits over the chevron. The title is a link to `?edit=<id>`. The forms |
| 46 | use `display: contents`, so TodoMVC's CSS lays the page out unchanged. |
| 47 | - **With JavaScript.** `app.js` adds the `js` class to `<html>`, which |
| 48 | hides the stand-in buttons. Actions post the same forms with an |
| 49 | `X-Fragment: 1` header, and the server answers with a fresh |
| 50 | `section.main` and `footer.footer` instead of a redirect. |
| 51 | - **Storage.** WAL mode, `synchronous = NORMAL`. Migrations are a list in |
| 52 | `Todo.Db`, applied in order and counted in `PRAGMA user_version`. They |
| 53 | only ever add. |
| 54 | - **Shutdown.** SIGTERM closes the listening socket, requests in flight |
| 55 | get up to five seconds, the WAL is checkpointed, and the process exits |
| 56 | 0. |
| 57 | |
| 58 | ## Layout |
| 59 | |
| 60 | | Path | What | |
| 61 | |---|---| |
| 62 | | `app/Main.hs` | Flags, the socket, signals | |
| 63 | | `src/Todo/App.hs` | Routes and handlers, including `GET /` | |
| 64 | | `src/Todo/Html.hs` | The page and the fragment | |
| 65 | | `src/Todo/Db.hs` | Schema, migrations, queries | |
| 66 | | `src/Todo/Cookie.hs` | Token minting and the cookie | |
| 67 | | `src/Todo/Form.hs`, `Model.hs`, `Assets.hs` | Form bodies, types, embedded files | |
| 68 | | `static/app.js`, `static/app.css` | The JavaScript layer and the no-JS controls | |
| 69 | | `test/Spec.hs` | Tests | |
| 70 | |
| 71 | ## Building, running, testing |
| 72 | |
| 73 | The x86 build uses ghcup's GHC 9.10.3 and cabal 3.16 (`source |
| 74 | ../../scripts/env.sh`). The shipped binary is built inside the bakeoff's |
| 75 | emulated arm64 root, which has Debian's GHC 9.6.6 and cabal 3.10, and |
| 76 | linked fully static. `cabal.project` pins the Hackage index so both |
| 77 | resolve the same library versions. |
| 78 | |
| 79 | ``` |
| 80 | make build # dist/app, for this machine |
| 81 | make build-arm64 # dist/app-linux-arm64, static, built under qemu |
| 82 | make test # the hspec suite |
| 83 | make clean # build output only |
| 84 | make clean-deps # also every compiled library (sources stay downloaded) |
| 85 | ``` |
| 86 | |
| 87 | Cabal's stores live in `.cache/cabal-x86` and `.cache/cabal-arm64`, so |
| 88 | `make clean-deps` can remove them without touching anything shared. A |
| 89 | cold `make build-arm64` compiles about fifty libraries under emulation |
| 90 | and takes a long while; after that, a change to one module rebuilds in |
| 91 | a minute or two. |
| 92 | |
| 93 | Run it locally and point the shared suite at it: |
| 94 | |
| 95 | ``` |
| 96 | dist/app --listen 127.0.0.1:8080 --db "$TMPDIR/haskell.db" |
| 97 | ../../scripts/test-app.sh http://127.0.0.1:8080 |
| 98 | ``` |
| 99 | |
| 100 | `--listen` takes only a `127.x.x.x` address. |
| 101 | |
| 102 | ## Deploying |
| 103 | |
| 104 | Once the app and its instance exist on Carloku (see `BRIEF.md`), commit, |
| 105 | then: |
| 106 | |
| 107 | ``` |
| 108 | make build-arm64 |
| 109 | ../../scripts/boot-check.sh dist/app-linux-arm64 |
| 110 | make deploy MESSAGE="what changed" |
| 111 | ``` |
| 112 | |
| 113 | The version is the commit's short SHA, compiled in via a generated |
| 114 | `gen/BuildVersion.hs`, and `make deploy` passes the same SHA to |
| 115 | `carlos deploy`. The app's build probe is `/api/version`, so the deploy |
| 116 | only reports live once the new process says it's running that build. |
| 117 | |