Revert "♻️ Quality: demos/team copies little-coder's starter (10 duplicate findings → 0)"
This reverts commit 1dab580aabc46a9658a96f5c698972a60ac9ae13.
d6f3d15 parent: 49ca17f modified
demos/little-coder/README.en.md +1 -1 | @@ -83,7 +83,7 @@ Create a small Go and HTMX web application to manage my library: books with a ti | ||
| 83 | 83 | | `agent.little-coder.yaml` | The agent's configuration, commented. | |
| 84 | 84 | | `AGENTS.md` | The workspace rules, appended to the system prompt. | |
| 85 | 85 | | `README.md` | This document in French. | |
| 86 | -| `skills/go-htmx-app/` | Create a sample application; holds the `starter/` — also the one [`demos/team`](../team/) copies (its `new-app.sh`). | | |
| 86 | +| `skills/go-htmx-app/` | Create a sample application; holds the `starter/`. | | |
| 87 | 87 | | `skills/go-htmx-subject-app/` | Create an application about a given subject (adapt `app.go`). | |
| 88 | 88 | | `skills/run-and-check/` | Check, run, diagnose and fix. | |
| 89 | 89 | | `skills/htmx-patterns/` | htmx 2 reference, HTML side and Go handler side. | |
| @@ -83,7 +83,7 @@ Create a small Go and HTMX web application to manage my library: books with a ti | |||
| 83 | | `agent.little-coder.yaml` | The agent's configuration, commented. | | 83 | | `agent.little-coder.yaml` | The agent's configuration, commented. | |
| 84 | | `AGENTS.md` | The workspace rules, appended to the system prompt. | | 84 | | `AGENTS.md` | The workspace rules, appended to the system prompt. | |
| 85 | | `README.md` | This document in French. | | 85 | | `README.md` | This document in French. | |
| 86 | -| `skills/go-htmx-app/` | Create a sample application; holds the `starter/` — also the one [`demos/team`](../team/) copies (its `new-app.sh`). | | 86 | +| `skills/go-htmx-app/` | Create a sample application; holds the `starter/`. | |
| 87 | | `skills/go-htmx-subject-app/` | Create an application about a given subject (adapt `app.go`). | | 87 | | `skills/go-htmx-subject-app/` | Create an application about a given subject (adapt `app.go`). | |
| 88 | | `skills/run-and-check/` | Check, run, diagnose and fix. | | 88 | | `skills/run-and-check/` | Check, run, diagnose and fix. | |
| 89 | | `skills/htmx-patterns/` | htmx 2 reference, HTML side and Go handler side. | | 89 | | `skills/htmx-patterns/` | htmx 2 reference, HTML side and Go handler side. | |
modified
demos/little-coder/README.fr.md +1 -1 | @@ -83,7 +83,7 @@ Crée une petite application web en Go et HTMX pour gérer ma bibliothèque : de | ||
| 83 | 83 | | `agent.little-coder.yaml` | La configuration de l'agent, commentée. | |
| 84 | 84 | | `AGENTS.md` | Les règles de l'espace de travail, ajoutées au prompt système. | |
| 85 | 85 | | `README.en.md` | Ce document en anglais. | |
| 86 | -| `skills/go-htmx-app/` | Créer une application d'exemple ; contient le socle `starter/` — aussi celui que copie [`demos/team`](../team/) (son `new-app.sh`). | | |
| 86 | +| `skills/go-htmx-app/` | Créer une application d'exemple ; contient le socle `starter/`. | | |
| 87 | 87 | | `skills/go-htmx-subject-app/` | Créer une application sur un sujet donné (adapter `app.go`). | |
| 88 | 88 | | `skills/run-and-check/` | Vérifier, lancer, diagnostiquer et corriger. | |
| 89 | 89 | | `skills/htmx-patterns/` | Référence htmx 2 côté HTML et côté handler Go. | |
| @@ -83,7 +83,7 @@ Crée une petite application web en Go et HTMX pour gérer ma bibliothèque : de | |||
| 83 | | `agent.little-coder.yaml` | La configuration de l'agent, commentée. | | 83 | | `agent.little-coder.yaml` | La configuration de l'agent, commentée. | |
| 84 | | `AGENTS.md` | Les règles de l'espace de travail, ajoutées au prompt système. | | 84 | | `AGENTS.md` | Les règles de l'espace de travail, ajoutées au prompt système. | |
| 85 | | `README.en.md` | Ce document en anglais. | | 85 | | `README.en.md` | Ce document en anglais. | |
| 86 | -| `skills/go-htmx-app/` | Créer une application d'exemple ; contient le socle `starter/` — aussi celui que copie [`demos/team`](../team/) (son `new-app.sh`). | | 86 | +| `skills/go-htmx-app/` | Créer une application d'exemple ; contient le socle `starter/`. | |
| 87 | | `skills/go-htmx-subject-app/` | Créer une application sur un sujet donné (adapter `app.go`). | | 87 | | `skills/go-htmx-subject-app/` | Créer une application sur un sujet donné (adapter `app.go`). | |
| 88 | | `skills/run-and-check/` | Vérifier, lancer, diagnostiquer et corriger. | | 88 | | `skills/run-and-check/` | Vérifier, lancer, diagnostiquer et corriger. | |
| 89 | | `skills/htmx-patterns/` | Référence htmx 2 côté HTML et côté handler Go. | | 89 | | `skills/htmx-patterns/` | Référence htmx 2 côté HTML et côté handler Go. | |
modified
demos/team/README.en.md +3 -2 | @@ -116,7 +116,7 @@ book-library/ | ||
| 116 | 116 | |
| 117 | 117 | Every choice comes from a failure measured while tuning it: |
| 118 | 118 | |
| 119 | -- **little-coder's tested starter** (`../little-coder/skills/go-htmx-app/starter/`, the one copy both demos use), copied by **`sh new-app.sh <dir>`**. The coder miscopied the starter's path: it invented an absolute path, then found with `find` an old copy of the starter without `app.go`, and had to rewrite everything. The script leaves no path to copy, and refuses to overwrite an existing application. | |
| 119 | +- **little-coder's tested starter** (`starter/`), copied by **`sh new-app.sh <dir>`**. The coder miscopied the starter's path: it invented an absolute path, then found with `find` an old copy of the starter without `app.go`, and had to rewrite everything. The script leaves no path to copy, and refuses to overwrite an existing application. | |
| 120 | 120 | - **`app.go` is generated from the spec by `new-app.sh`, not written by the model.** Copying the Data section's values is mechanical, and yet that is where the Qwen 9B coder went wrong one time in two, even with reasoning: everything in `app.go`, an import of `gorilla/mux`, the spec rewritten. The script reads the `- Key: value` lines of the App and Data sections; when the spec is not in the format it says so, and the coder writes `app.go` by hand. |
| 121 | 121 | - **A spec with a fixed format, four sections.** The starter's criteria (page, add, search, checkbox, delete) are **not** written by the specifier: the tester derives them mechanically from the Data section (rows S1–S6). The specifier writes only the extra features (F1…) and their criteria (A1…). Deciding "what goes beyond the starter" was the costliest judgement: without an explicit list of what the starter already does, Qwen and Gemma alike filed the counter under "nothing more". |
| 122 | 122 | - **A refreshed value gets two criteria**: the route (`GET /items/count` contains `2`) **and** the page (`GET /` contains `hx-get="/items/count"`). With the first alone, a coder delivered a correct route and a counter hard-coded into the page. |
| @@ -164,7 +164,8 @@ Flaws seen with Ornith: the specifier sometimes reasons so long that it stops wi | ||
| 164 | 164 | | `agent.lead.yaml` | The coordinator and the team's declaration (`mmConfigs`). | |
| 165 | 165 | | `agent.specifier.yaml`, `agent.coder.yaml`, `agent.tester.yaml` | The three specialists. | |
| 166 | 166 | | `AGENTS.md` | Workspace rules (stack, modern Go, definition of done), read by all four. | |
| 167 | -| `new-app.sh` | Creates an application from little-coder's starter (`../little-coder/skills/go-htmx-app/starter/`) and generates its `app.go` from the spec: the demo needs `demos/little-coder` beside it. | | |
| 167 | +| `new-app.sh` | Creates an application from `starter/` and generates its `app.go` from the spec. | | |
| 168 | +| `starter/` | The tested starter (a copy of little-coder's). | | |
| 168 | 169 | | `skills-coder/` | `build-app`, `add-feature`, `fix-app`, `htmx-patterns`. | |
| 169 | 170 | | `skills-tester/` | `test-spec`. | |
| 170 | 171 | | `README.fr.md` | This document in French. | |
| @@ -116,7 +116,7 @@ book-library/ | |||
| 116 | 116 | ||
| 117 | Every choice comes from a failure measured while tuning it: | 117 | Every choice comes from a failure measured while tuning it: |
| 118 | 118 | ||
| 119 | -- **little-coder's tested starter** (`../little-coder/skills/go-htmx-app/starter/`, the one copy both demos use), copied by **`sh new-app.sh <dir>`**. The coder miscopied the starter's path: it invented an absolute path, then found with `find` an old copy of the starter without `app.go`, and had to rewrite everything. The script leaves no path to copy, and refuses to overwrite an existing application. | 119 | +- **little-coder's tested starter** (`starter/`), copied by **`sh new-app.sh <dir>`**. The coder miscopied the starter's path: it invented an absolute path, then found with `find` an old copy of the starter without `app.go`, and had to rewrite everything. The script leaves no path to copy, and refuses to overwrite an existing application. |
| 120 | - **`app.go` is generated from the spec by `new-app.sh`, not written by the model.** Copying the Data section's values is mechanical, and yet that is where the Qwen 9B coder went wrong one time in two, even with reasoning: everything in `app.go`, an import of `gorilla/mux`, the spec rewritten. The script reads the `- Key: value` lines of the App and Data sections; when the spec is not in the format it says so, and the coder writes `app.go` by hand. | 120 | - **`app.go` is generated from the spec by `new-app.sh`, not written by the model.** Copying the Data section's values is mechanical, and yet that is where the Qwen 9B coder went wrong one time in two, even with reasoning: everything in `app.go`, an import of `gorilla/mux`, the spec rewritten. The script reads the `- Key: value` lines of the App and Data sections; when the spec is not in the format it says so, and the coder writes `app.go` by hand. |
| 121 | - **A spec with a fixed format, four sections.** The starter's criteria (page, add, search, checkbox, delete) are **not** written by the specifier: the tester derives them mechanically from the Data section (rows S1–S6). The specifier writes only the extra features (F1…) and their criteria (A1…). Deciding "what goes beyond the starter" was the costliest judgement: without an explicit list of what the starter already does, Qwen and Gemma alike filed the counter under "nothing more". | 121 | - **A spec with a fixed format, four sections.** The starter's criteria (page, add, search, checkbox, delete) are **not** written by the specifier: the tester derives them mechanically from the Data section (rows S1–S6). The specifier writes only the extra features (F1…) and their criteria (A1…). Deciding "what goes beyond the starter" was the costliest judgement: without an explicit list of what the starter already does, Qwen and Gemma alike filed the counter under "nothing more". |
| 122 | - **A refreshed value gets two criteria**: the route (`GET /items/count` contains `2`) **and** the page (`GET /` contains `hx-get="/items/count"`). With the first alone, a coder delivered a correct route and a counter hard-coded into the page. | 122 | - **A refreshed value gets two criteria**: the route (`GET /items/count` contains `2`) **and** the page (`GET /` contains `hx-get="/items/count"`). With the first alone, a coder delivered a correct route and a counter hard-coded into the page. |
| @@ -164,7 +164,8 @@ Flaws seen with Ornith: the specifier sometimes reasons so long that it stops wi | |||
| 164 | | `agent.lead.yaml` | The coordinator and the team's declaration (`mmConfigs`). | | 164 | | `agent.lead.yaml` | The coordinator and the team's declaration (`mmConfigs`). | |
| 165 | | `agent.specifier.yaml`, `agent.coder.yaml`, `agent.tester.yaml` | The three specialists. | | 165 | | `agent.specifier.yaml`, `agent.coder.yaml`, `agent.tester.yaml` | The three specialists. | |
| 166 | | `AGENTS.md` | Workspace rules (stack, modern Go, definition of done), read by all four. | | 166 | | `AGENTS.md` | Workspace rules (stack, modern Go, definition of done), read by all four. | |
| 167 | -| `new-app.sh` | Creates an application from little-coder's starter (`../little-coder/skills/go-htmx-app/starter/`) and generates its `app.go` from the spec: the demo needs `demos/little-coder` beside it. | | 167 | +| `new-app.sh` | Creates an application from `starter/` and generates its `app.go` from the spec. | |
| 168 | +| `starter/` | The tested starter (a copy of little-coder's). | | ||
| 168 | | `skills-coder/` | `build-app`, `add-feature`, `fix-app`, `htmx-patterns`. | | 169 | | `skills-coder/` | `build-app`, `add-feature`, `fix-app`, `htmx-patterns`. | |
| 169 | | `skills-tester/` | `test-spec`. | | 170 | | `skills-tester/` | `test-spec`. | |
| 170 | | `README.fr.md` | This document in French. | | 171 | | `README.fr.md` | This document in French. | |
modified
demos/team/README.fr.md +3 -2 | @@ -116,7 +116,7 @@ book-library/ | ||
| 116 | 116 | |
| 117 | 117 | Chaque choix vient d'un échec mesuré pendant la mise au point : |
| 118 | 118 | |
| 119 | -- **Le socle testé de little-coder** (`../little-coder/skills/go-htmx-app/starter/`, la seule copie, commune aux deux démos), copié par **`sh new-app.sh <dir>`**. Le codeur recopiait mal le chemin du socle : il a inventé un chemin absolu, puis trouvé avec `find` une vieille copie du socle sans `app.go`, et dû tout réécrire. Le script ne laisse aucun chemin à recopier, et refuse d'écraser une application existante. | |
| 119 | +- **Le socle testé de little-coder** (`starter/`), copié par **`sh new-app.sh <dir>`**. Le codeur recopiait mal le chemin du socle : il a inventé un chemin absolu, puis trouvé avec `find` une vieille copie du socle sans `app.go`, et dû tout réécrire. Le script ne laisse aucun chemin à recopier, et refuse d'écraser une application existante. | |
| 120 | 120 | - **`app.go` est généré depuis la spec par `new-app.sh`, pas écrit par le modèle.** Recopier les valeurs de la section Data est mécanique, et c'est pourtant là que le codeur Qwen 9B dérapait une fois sur deux, même avec raisonnement : tout dans `app.go`, import de `gorilla/mux`, spec réécrite. Le script lit les lignes `- Clé: valeur` des sections App et Data. Si la spec n'est pas au format, il le dit, et le codeur écrit `app.go` à la main. |
| 121 | 121 | - **Une spec au format fixe, en quatre sections.** Les critères du socle (page, ajout, recherche, case, suppression) ne sont **pas** écrits par le spécificateur : le testeur les dérive mécaniquement de la section Data (lignes S1–S6). Le spécificateur n'écrit que les fonctionnalités en plus (F1…) et leurs critères (A1…). C'est le jugement « qu'est-ce qui dépasse le socle » qui coûtait le plus cher : sans liste explicite de ce que le socle fait déjà, Qwen comme Gemma rangeaient le compteur dans « rien de plus ». |
| 122 | 122 | - **Une valeur rafraîchie a deux critères** : la route (`GET /items/count` contient `2`) **et** la page (`GET /` contient `hx-get="/items/count"`). Avec le premier seul, un codeur a livré une route juste et un compteur codé en dur sur la page. |
| @@ -164,7 +164,8 @@ Défauts observés avec Ornith : le spécificateur raisonne parfois si longtemps | ||
| 164 | 164 | | `agent.lead.yaml` | Le coordinateur et la déclaration de l'équipe (`mmConfigs`). | |
| 165 | 165 | | `agent.specifier.yaml`, `agent.coder.yaml`, `agent.tester.yaml` | Les trois spécialistes. | |
| 166 | 166 | | `AGENTS.md` | Règles de l'espace de travail (pile, Go moderne, définition de « terminé »), lues par les quatre. | |
| 167 | -| `new-app.sh` | Crée une application depuis le socle de little-coder (`../little-coder/skills/go-htmx-app/starter/`) et génère son `app.go` depuis la spec : la démo a besoin de `demos/little-coder` à côté d'elle. | | |
| 167 | +| `new-app.sh` | Crée une application depuis `starter/` et génère son `app.go` depuis la spec. | | |
| 168 | +| `starter/` | Le socle testé (copie de celui de little-coder). | | |
| 168 | 169 | | `skills-coder/` | `build-app`, `add-feature`, `fix-app`, `htmx-patterns`. | |
| 169 | 170 | | `skills-tester/` | `test-spec`. | |
| 170 | 171 | | `README.en.md` | Ce document en anglais. | |
| @@ -116,7 +116,7 @@ book-library/ | |||
| 116 | 116 | ||
| 117 | Chaque choix vient d'un échec mesuré pendant la mise au point : | 117 | Chaque choix vient d'un échec mesuré pendant la mise au point : |
| 118 | 118 | ||
| 119 | -- **Le socle testé de little-coder** (`../little-coder/skills/go-htmx-app/starter/`, la seule copie, commune aux deux démos), copié par **`sh new-app.sh <dir>`**. Le codeur recopiait mal le chemin du socle : il a inventé un chemin absolu, puis trouvé avec `find` une vieille copie du socle sans `app.go`, et dû tout réécrire. Le script ne laisse aucun chemin à recopier, et refuse d'écraser une application existante. | 119 | +- **Le socle testé de little-coder** (`starter/`), copié par **`sh new-app.sh <dir>`**. Le codeur recopiait mal le chemin du socle : il a inventé un chemin absolu, puis trouvé avec `find` une vieille copie du socle sans `app.go`, et dû tout réécrire. Le script ne laisse aucun chemin à recopier, et refuse d'écraser une application existante. |
| 120 | - **`app.go` est généré depuis la spec par `new-app.sh`, pas écrit par le modèle.** Recopier les valeurs de la section Data est mécanique, et c'est pourtant là que le codeur Qwen 9B dérapait une fois sur deux, même avec raisonnement : tout dans `app.go`, import de `gorilla/mux`, spec réécrite. Le script lit les lignes `- Clé: valeur` des sections App et Data. Si la spec n'est pas au format, il le dit, et le codeur écrit `app.go` à la main. | 120 | - **`app.go` est généré depuis la spec par `new-app.sh`, pas écrit par le modèle.** Recopier les valeurs de la section Data est mécanique, et c'est pourtant là que le codeur Qwen 9B dérapait une fois sur deux, même avec raisonnement : tout dans `app.go`, import de `gorilla/mux`, spec réécrite. Le script lit les lignes `- Clé: valeur` des sections App et Data. Si la spec n'est pas au format, il le dit, et le codeur écrit `app.go` à la main. |
| 121 | - **Une spec au format fixe, en quatre sections.** Les critères du socle (page, ajout, recherche, case, suppression) ne sont **pas** écrits par le spécificateur : le testeur les dérive mécaniquement de la section Data (lignes S1–S6). Le spécificateur n'écrit que les fonctionnalités en plus (F1…) et leurs critères (A1…). C'est le jugement « qu'est-ce qui dépasse le socle » qui coûtait le plus cher : sans liste explicite de ce que le socle fait déjà, Qwen comme Gemma rangeaient le compteur dans « rien de plus ». | 121 | - **Une spec au format fixe, en quatre sections.** Les critères du socle (page, ajout, recherche, case, suppression) ne sont **pas** écrits par le spécificateur : le testeur les dérive mécaniquement de la section Data (lignes S1–S6). Le spécificateur n'écrit que les fonctionnalités en plus (F1…) et leurs critères (A1…). C'est le jugement « qu'est-ce qui dépasse le socle » qui coûtait le plus cher : sans liste explicite de ce que le socle fait déjà, Qwen comme Gemma rangeaient le compteur dans « rien de plus ». |
| 122 | - **Une valeur rafraîchie a deux critères** : la route (`GET /items/count` contient `2`) **et** la page (`GET /` contient `hx-get="/items/count"`). Avec le premier seul, un codeur a livré une route juste et un compteur codé en dur sur la page. | 122 | - **Une valeur rafraîchie a deux critères** : la route (`GET /items/count` contient `2`) **et** la page (`GET /` contient `hx-get="/items/count"`). Avec le premier seul, un codeur a livré une route juste et un compteur codé en dur sur la page. |
| @@ -164,7 +164,8 @@ Défauts observés avec Ornith : le spécificateur raisonne parfois si longtemps | |||
| 164 | | `agent.lead.yaml` | Le coordinateur et la déclaration de l'équipe (`mmConfigs`). | | 164 | | `agent.lead.yaml` | Le coordinateur et la déclaration de l'équipe (`mmConfigs`). | |
| 165 | | `agent.specifier.yaml`, `agent.coder.yaml`, `agent.tester.yaml` | Les trois spécialistes. | | 165 | | `agent.specifier.yaml`, `agent.coder.yaml`, `agent.tester.yaml` | Les trois spécialistes. | |
| 166 | | `AGENTS.md` | Règles de l'espace de travail (pile, Go moderne, définition de « terminé »), lues par les quatre. | | 166 | | `AGENTS.md` | Règles de l'espace de travail (pile, Go moderne, définition de « terminé »), lues par les quatre. | |
| 167 | -| `new-app.sh` | Crée une application depuis le socle de little-coder (`../little-coder/skills/go-htmx-app/starter/`) et génère son `app.go` depuis la spec : la démo a besoin de `demos/little-coder` à côté d'elle. | | 167 | +| `new-app.sh` | Crée une application depuis `starter/` et génère son `app.go` depuis la spec. | |
| 168 | +| `starter/` | Le socle testé (copie de celui de little-coder). | | ||
| 168 | | `skills-coder/` | `build-app`, `add-feature`, `fix-app`, `htmx-patterns`. | | 169 | | `skills-coder/` | `build-app`, `add-feature`, `fix-app`, `htmx-patterns`. | |
| 169 | | `skills-tester/` | `test-spec`. | | 170 | | `skills-tester/` | `test-spec`. | |
| 170 | | `README.en.md` | Ce document en anglais. | | 171 | | `README.en.md` | Ce document en anglais. | |
modified
demos/team/agent.coder.yaml +1 -1 | @@ -56,7 +56,7 @@ planTool: false | ||
| 56 | 56 | fetchContentTool: false |
| 57 | 57 | |
| 58 | 58 | # build-app, add-feature, fix-app, htmx-patterns. The starter app itself is |
| 59 | -# little-coder's (../little-coder/skills/go-htmx-app/starter), copied by new-app.sh. Relative: in | |
| 59 | +# in starter/, copied by new-app.sh. Relative: in | |
| 60 | 60 | # ACP mode (asked by the lead) it follows this file; started by hand, run |
| 61 | 61 | # from demos/team. |
| 62 | 62 | skillsDir: skills-coder |
| @@ -56,7 +56,7 @@ planTool: false | |||
| 56 | fetchContentTool: false | 56 | fetchContentTool: false |
| 57 | 57 | ||
| 58 | # build-app, add-feature, fix-app, htmx-patterns. The starter app itself is | 58 | # build-app, add-feature, fix-app, htmx-patterns. The starter app itself is |
| 59 | -# little-coder's (../little-coder/skills/go-htmx-app/starter), copied by new-app.sh. Relative: in | 59 | +# in starter/, copied by new-app.sh. Relative: in |
| 60 | # ACP mode (asked by the lead) it follows this file; started by hand, run | 60 | # ACP mode (asked by the lead) it follows this file; started by hand, run |
| 61 | # from demos/team. | 61 | # from demos/team. |
| 62 | skillsDir: skills-coder | 62 | skillsDir: skills-coder |
modified
demos/team/new-app.sh +3 -5 | @@ -1,8 +1,6 @@ | ||
| 1 | 1 | #!/bin/sh |
| 2 | -# new-app.sh <dir> — create a new app directory from the tested starter — | |
| 3 | -# little-coder's (../little-coder/skills/go-htmx-app/starter), one copy for | |
| 4 | -# both demos — and generate its app.go from specs/<dir>.spec.md when that | |
| 5 | -# spec exists. | |
| 2 | +# new-app.sh <dir> — create a new app directory from the tested starter, and | |
| 3 | +# generate its app.go from specs/<dir>.spec.md when that spec exists. | |
| 6 | 4 | # Usage (from demos/team): sh new-app.sh book-library |
| 7 | 5 | set -e |
| 8 | 6 | dir="$1" |
| @@ -12,7 +10,7 @@ if [ -e "$dir/go.mod" ]; then | ||
| 12 | 10 | echo "$dir already exists: not overwritten"; exit 1 |
| 13 | 11 | fi |
| 14 | 12 | mkdir -p "$dir" |
| 15 | -cp -R "$here/../little-coder/skills/go-htmx-app/starter/." "$dir/" | |
| 13 | +cp -R "$here/starter/." "$dir/" | |
| 16 | 14 | (cd "$dir" && go mod edit -module "$dir") |
| 17 | 15 | echo "created $dir from the starter" |
| 18 | 16 | |
| @@ -1,8 +1,6 @@ | |||
| 1 | #!/bin/sh | 1 | #!/bin/sh |
| 2 | -# new-app.sh <dir> — create a new app directory from the tested starter — | 2 | +# new-app.sh <dir> — create a new app directory from the tested starter, and |
| 3 | -# little-coder's (../little-coder/skills/go-htmx-app/starter), one copy for | 3 | +# generate its app.go from specs/<dir>.spec.md when that spec exists. |
| 4 | -# both demos — and generate its app.go from specs/<dir>.spec.md when that | ||
| 5 | -# spec exists. | ||
| 6 | # Usage (from demos/team): sh new-app.sh book-library | 4 | # Usage (from demos/team): sh new-app.sh book-library |
| 7 | set -e | 5 | set -e |
| 8 | dir="$1" | 6 | dir="$1" |
| @@ -12,7 +10,7 @@ if [ -e "$dir/go.mod" ]; then | |||
| 12 | echo "$dir already exists: not overwritten"; exit 1 | 10 | echo "$dir already exists: not overwritten"; exit 1 |
| 13 | fi | 11 | fi |
| 14 | mkdir -p "$dir" | 12 | mkdir -p "$dir" |
| 15 | -cp -R "$here/../little-coder/skills/go-htmx-app/starter/." "$dir/" | 13 | +cp -R "$here/starter/." "$dir/" |
| 16 | (cd "$dir" && go mod edit -module "$dir") | 14 | (cd "$dir" && go mod edit -module "$dir") |
| 17 | echo "created $dir from the starter" | 15 | echo "created $dir from the starter" |
| 18 | 16 | ||
added
demos/team/starter/README.md +33 -0 | new file mode 100644 | ||
| @@ -0,0 +1,33 @@ | ||
| 1 | +# htmx list app | |
| 2 | + | |
| 3 | +A small web application in modern Go with htmx: a list you can add to, tick, delete from and search live, on a responsive page. Every action swaps one HTML fragment — no JSON, no JavaScript to write. | |
| 4 | + | |
| 5 | +## Stack | |
| 6 | + | |
| 7 | +- Go standard library only: `net/http` (method + path routing), `html/template`, `embed`, `log/slog`. | |
| 8 | +- htmx 2 and Pico CSS from a CDN. | |
| 9 | +- Data in memory, behind a `sync.Mutex` (lost when the server stops). | |
| 10 | + | |
| 11 | +## Run | |
| 12 | + | |
| 13 | + go run . # http://localhost:8080 | |
| 14 | + PORT=9000 go run . # another port | |
| 15 | + | |
| 16 | +## Test and check | |
| 17 | + | |
| 18 | + go test ./... | |
| 19 | + sh check.sh # gofmt, vet, tests, then starts the app, queries it with curl, stops it: ends with CHECK OK | |
| 20 | + | |
| 21 | +## Routes | |
| 22 | + | |
| 23 | +| Route | Answer | | |
| 24 | +|---|---| | |
| 25 | +| `GET /` | the page | | |
| 26 | +| `POST /items` | add an item — fragment `item` | | |
| 27 | +| `GET /items/search?q=` | filter — fragment `items` | | |
| 28 | +| `POST /items/{id}/toggle` | done / not done — fragment `item` | | |
| 29 | +| `DELETE /items/{id}` | remove — empty answer, htmx removes the line | | |
| 30 | + | |
| 31 | +## Adapt it | |
| 32 | + | |
| 33 | +`app.go` holds the texts, the extra fields and the start-up data: the page, the handlers and the tests all read it. | |
| new file mode 100644 | |||
| @@ -0,0 +1,33 @@ | |||
| 1 | +# htmx list app | ||
| 2 | + | ||
| 3 | +A small web application in modern Go with htmx: a list you can add to, tick, delete from and search live, on a responsive page. Every action swaps one HTML fragment — no JSON, no JavaScript to write. | ||
| 4 | + | ||
| 5 | +## Stack | ||
| 6 | + | ||
| 7 | +- Go standard library only: `net/http` (method + path routing), `html/template`, `embed`, `log/slog`. | ||
| 8 | +- htmx 2 and Pico CSS from a CDN. | ||
| 9 | +- Data in memory, behind a `sync.Mutex` (lost when the server stops). | ||
| 10 | + | ||
| 11 | +## Run | ||
| 12 | + | ||
| 13 | + go run . # http://localhost:8080 | ||
| 14 | + PORT=9000 go run . # another port | ||
| 15 | + | ||
| 16 | +## Test and check | ||
| 17 | + | ||
| 18 | + go test ./... | ||
| 19 | + sh check.sh # gofmt, vet, tests, then starts the app, queries it with curl, stops it: ends with CHECK OK | ||
| 20 | + | ||
| 21 | +## Routes | ||
| 22 | + | ||
| 23 | +| Route | Answer | | ||
| 24 | +|---|---| | ||
| 25 | +| `GET /` | the page | | ||
| 26 | +| `POST /items` | add an item — fragment `item` | | ||
| 27 | +| `GET /items/search?q=` | filter — fragment `items` | | ||
| 28 | +| `POST /items/{id}/toggle` | done / not done — fragment `item` | | ||
| 29 | +| `DELETE /items/{id}` | remove — empty answer, htmx removes the line | | ||
| 30 | + | ||
| 31 | +## Adapt it | ||
| 32 | + | ||
| 33 | +`app.go` holds the texts, the extra fields and the start-up data: the page, the handlers and the tests all read it. | ||
added
demos/team/starter/app.go +19 -0 | new file mode 100644 | ||
| @@ -0,0 +1,19 @@ | ||
| 1 | +package main | |
| 2 | + | |
| 3 | +// app describes what this list holds. To adapt the app to another subject, | |
| 4 | +// rewrite only this file: the page, the handlers and the tests all read it. | |
| 5 | +// The types are in appinfo.go. | |
| 6 | +var app = appInfo{ | |
| 7 | + Lang: "en", | |
| 8 | + Title: "htmx tasks", | |
| 9 | + TitleLabel: "Task", | |
| 10 | + Fields: []string{}, | |
| 11 | + DoneLabel: "Done", | |
| 12 | + AddLabel: "Add", | |
| 13 | + SearchLabel: "Search", | |
| 14 | + DeleteConfirm: "Delete this task?", | |
| 15 | + Seeds: []seed{ | |
| 16 | + {Title: "Read the htmx docs"}, | |
| 17 | + {Title: "Write a Go handler"}, | |
| 18 | + }, | |
| 19 | +} | |
| new file mode 100644 | |||
| @@ -0,0 +1,19 @@ | |||
| 1 | +package main | ||
| 2 | + | ||
| 3 | +// app describes what this list holds. To adapt the app to another subject, | ||
| 4 | +// rewrite only this file: the page, the handlers and the tests all read it. | ||
| 5 | +// The types are in appinfo.go. | ||
| 6 | +var app = appInfo{ | ||
| 7 | + Lang: "en", | ||
| 8 | + Title: "htmx tasks", | ||
| 9 | + TitleLabel: "Task", | ||
| 10 | + Fields: []string{}, | ||
| 11 | + DoneLabel: "Done", | ||
| 12 | + AddLabel: "Add", | ||
| 13 | + SearchLabel: "Search", | ||
| 14 | + DeleteConfirm: "Delete this task?", | ||
| 15 | + Seeds: []seed{ | ||
| 16 | + {Title: "Read the htmx docs"}, | ||
| 17 | + {Title: "Write a Go handler"}, | ||
| 18 | + }, | ||
| 19 | +} | ||
added
demos/team/starter/appinfo.go +20 -0 | new file mode 100644 | ||
| @@ -0,0 +1,20 @@ | ||
| 1 | +package main | |
| 2 | + | |
| 3 | +// appInfo holds the texts of the page and the shape of an item. | |
| 4 | +type appInfo struct { | |
| 5 | + Lang string // html lang attribute: "en", "fr"… | |
| 6 | + Title string // page title and heading | |
| 7 | + TitleLabel string // label of the main text of an item | |
| 8 | + Fields []string // labels of the extra text fields, e.g. {"Author", "Year"} | |
| 9 | + DoneLabel string // label of the done checkbox: "Done", "Read"…; "" = no checkbox | |
| 10 | + AddLabel string // text of the add button | |
| 11 | + SearchLabel string // placeholder of the search box | |
| 12 | + DeleteConfirm string // question asked before a delete | |
| 13 | + Seeds []seed // items present at start-up | |
| 14 | +} | |
| 15 | + | |
| 16 | +// seed is an item present at start-up: one value per entry of Fields. | |
| 17 | +type seed struct { | |
| 18 | + Title string | |
| 19 | + Fields []string | |
| 20 | +} | |
| new file mode 100644 | |||
| @@ -0,0 +1,20 @@ | |||
| 1 | +package main | ||
| 2 | + | ||
| 3 | +// appInfo holds the texts of the page and the shape of an item. | ||
| 4 | +type appInfo struct { | ||
| 5 | + Lang string // html lang attribute: "en", "fr"… | ||
| 6 | + Title string // page title and heading | ||
| 7 | + TitleLabel string // label of the main text of an item | ||
| 8 | + Fields []string // labels of the extra text fields, e.g. {"Author", "Year"} | ||
| 9 | + DoneLabel string // label of the done checkbox: "Done", "Read"…; "" = no checkbox | ||
| 10 | + AddLabel string // text of the add button | ||
| 11 | + SearchLabel string // placeholder of the search box | ||
| 12 | + DeleteConfirm string // question asked before a delete | ||
| 13 | + Seeds []seed // items present at start-up | ||
| 14 | +} | ||
| 15 | + | ||
| 16 | +// seed is an item present at start-up: one value per entry of Fields. | ||
| 17 | +type seed struct { | ||
| 18 | + Title string | ||
| 19 | + Fields []string | ||
| 20 | +} | ||
added
demos/team/starter/check.sh +47 -0 | new file mode 100644 | ||
| @@ -0,0 +1,47 @@ | ||
| 1 | +#!/bin/sh | |
| 2 | +# check.sh — format (gofmt -w), vet, test, then build the app, start it in the | |
| 3 | +# background, query it like a browser and like htmx, and stop it. | |
| 4 | +# Usage: sh check.sh [port] (default port 8099) | |
| 5 | +# Prints "CHECK OK" at the end when everything passed. | |
| 6 | +port="${1:-8099}" | |
| 7 | +bin="$(mktemp -d)/app" | |
| 8 | + | |
| 9 | +echo "== gofmt" | |
| 10 | +unformatted="$(gofmt -l .)" | |
| 11 | +if [ -n "$unformatted" ]; then | |
| 12 | + gofmt -w . && echo "formatted: $unformatted" | |
| 13 | +fi | |
| 14 | +echo "== go vet" | |
| 15 | +go vet ./... || exit 1 | |
| 16 | +echo "== go test" | |
| 17 | +go test ./... 2>&1 | tail -30 | |
| 18 | +go test ./... >/dev/null 2>&1 || exit 1 | |
| 19 | +echo "== go build" | |
| 20 | +go build -o "$bin" . || exit 1 | |
| 21 | + | |
| 22 | +echo "== run on port $port" | |
| 23 | +if curl -s -o /dev/null "localhost:$port/"; then | |
| 24 | + echo "CHECK FAILED: port $port is already used by another server — run: sh check.sh 8098"; exit 1 | |
| 25 | +fi | |
| 26 | +PORT="$port" "$bin" > "$bin.log" 2>&1 & | |
| 27 | +app=$! | |
| 28 | +sleep 2 | |
| 29 | +if ! kill -0 "$app" 2>/dev/null; then | |
| 30 | + echo "CHECK FAILED: the app stopped at start-up:"; cat "$bin.log"; exit 1 | |
| 31 | +fi | |
| 32 | +page="$(curl -s -o /dev/null -w '%{http_code}' "localhost:$port/")" | |
| 33 | +title="$(curl -s "localhost:$port/" | sed -n 's:.*<title>\(.*\)</title>.*:\1:p')" | |
| 34 | +echo "GET / -> $page, page title: $title" | |
| 35 | +echo "POST /items (htmx) ->" | |
| 36 | +fragment="$(curl -s -X POST -H 'HX-Request: true' -d 'title=check' "localhost:$port/items")" | |
| 37 | +echo "$fragment" | head -5 | |
| 38 | +kill "$app" 2>/dev/null | |
| 39 | +wait "$app" 2>/dev/null | |
| 40 | +echo "== server log" | |
| 41 | +cat "$bin.log" | |
| 42 | + | |
| 43 | +[ "$page" = "200" ] || { echo "CHECK FAILED: GET / answered $page"; exit 1; } | |
| 44 | +case "$fragment" in | |
| 45 | + *"<li"*) echo "CHECK OK" ;; | |
| 46 | + *) echo "CHECK FAILED: POST /items did not answer an <li> fragment"; exit 1 ;; | |
| 47 | +esac | |
| new file mode 100644 | |||
| @@ -0,0 +1,47 @@ | |||
| 1 | +#!/bin/sh | ||
| 2 | +# check.sh — format (gofmt -w), vet, test, then build the app, start it in the | ||
| 3 | +# background, query it like a browser and like htmx, and stop it. | ||
| 4 | +# Usage: sh check.sh [port] (default port 8099) | ||
| 5 | +# Prints "CHECK OK" at the end when everything passed. | ||
| 6 | +port="${1:-8099}" | ||
| 7 | +bin="$(mktemp -d)/app" | ||
| 8 | + | ||
| 9 | +echo "== gofmt" | ||
| 10 | +unformatted="$(gofmt -l .)" | ||
| 11 | +if [ -n "$unformatted" ]; then | ||
| 12 | + gofmt -w . && echo "formatted: $unformatted" | ||
| 13 | +fi | ||
| 14 | +echo "== go vet" | ||
| 15 | +go vet ./... || exit 1 | ||
| 16 | +echo "== go test" | ||
| 17 | +go test ./... 2>&1 | tail -30 | ||
| 18 | +go test ./... >/dev/null 2>&1 || exit 1 | ||
| 19 | +echo "== go build" | ||
| 20 | +go build -o "$bin" . || exit 1 | ||
| 21 | + | ||
| 22 | +echo "== run on port $port" | ||
| 23 | +if curl -s -o /dev/null "localhost:$port/"; then | ||
| 24 | + echo "CHECK FAILED: port $port is already used by another server — run: sh check.sh 8098"; exit 1 | ||
| 25 | +fi | ||
| 26 | +PORT="$port" "$bin" > "$bin.log" 2>&1 & | ||
| 27 | +app=$! | ||
| 28 | +sleep 2 | ||
| 29 | +if ! kill -0 "$app" 2>/dev/null; then | ||
| 30 | + echo "CHECK FAILED: the app stopped at start-up:"; cat "$bin.log"; exit 1 | ||
| 31 | +fi | ||
| 32 | +page="$(curl -s -o /dev/null -w '%{http_code}' "localhost:$port/")" | ||
| 33 | +title="$(curl -s "localhost:$port/" | sed -n 's:.*<title>\(.*\)</title>.*:\1:p')" | ||
| 34 | +echo "GET / -> $page, page title: $title" | ||
| 35 | +echo "POST /items (htmx) ->" | ||
| 36 | +fragment="$(curl -s -X POST -H 'HX-Request: true' -d 'title=check' "localhost:$port/items")" | ||
| 37 | +echo "$fragment" | head -5 | ||
| 38 | +kill "$app" 2>/dev/null | ||
| 39 | +wait "$app" 2>/dev/null | ||
| 40 | +echo "== server log" | ||
| 41 | +cat "$bin.log" | ||
| 42 | + | ||
| 43 | +[ "$page" = "200" ] || { echo "CHECK FAILED: GET / answered $page"; exit 1; } | ||
| 44 | +case "$fragment" in | ||
| 45 | + *"<li"*) echo "CHECK OK" ;; | ||
| 46 | + *) echo "CHECK FAILED: POST /items did not answer an <li> fragment"; exit 1 ;; | ||
| 47 | +esac | ||
added
demos/team/starter/go.mod +5 -0 | new file mode 100644 | ||
| @@ -0,0 +1,5 @@ | ||
| 1 | +module htmx-app | |
| 2 | + | |
| 3 | +go 1.27.0 | |
| 4 | + | |
| 5 | +toolchain go1.27.4 | |
| new file mode 100644 | |||
| @@ -0,0 +1,5 @@ | |||
| 1 | +module htmx-app | ||
| 2 | + | ||
| 3 | +go 1.27.0 | ||
| 4 | + | ||
| 5 | +toolchain go1.27.4 | ||
added
demos/team/starter/handlers.go +81 -0 | new file mode 100644 | ||
| @@ -0,0 +1,81 @@ | ||
| 1 | +package main | |
| 2 | + | |
| 3 | +import ( | |
| 4 | + "html/template" | |
| 5 | + "log/slog" | |
| 6 | + "net/http" | |
| 7 | + "strconv" | |
| 8 | + "strings" | |
| 9 | +) | |
| 10 | + | |
| 11 | +type handlers struct { | |
| 12 | + store *store | |
| 13 | + tmpl *template.Template | |
| 14 | +} | |
| 15 | + | |
| 16 | +func (h *handlers) routes() http.Handler { | |
| 17 | + mux := http.NewServeMux() | |
| 18 | + mux.HandleFunc("GET /{$}", h.index) | |
| 19 | + mux.HandleFunc("POST /items", h.create) | |
| 20 | + mux.HandleFunc("GET /items/search", h.search) | |
| 21 | + mux.HandleFunc("POST /items/{id}/toggle", h.toggle) | |
| 22 | + mux.HandleFunc("DELETE /items/{id}", h.delete) | |
| 23 | + return mux | |
| 24 | +} | |
| 25 | + | |
| 26 | +// render writes a template: "index.html" for the page, "items" or "item" for htmx fragments. | |
| 27 | +func (h *handlers) render(w http.ResponseWriter, name string, data any) { | |
| 28 | + w.Header().Set("Content-Type", "text/html; charset=utf-8") | |
| 29 | + if err := h.tmpl.ExecuteTemplate(w, name, data); err != nil { | |
| 30 | + slog.Error("render", "template", name, "err", err) | |
| 31 | + } | |
| 32 | +} | |
| 33 | + | |
| 34 | +func (h *handlers) index(w http.ResponseWriter, r *http.Request) { | |
| 35 | + h.render(w, "index.html", map[string]any{"Items": h.store.List()}) | |
| 36 | +} | |
| 37 | + | |
| 38 | +func (h *handlers) create(w http.ResponseWriter, r *http.Request) { | |
| 39 | + title := strings.TrimSpace(r.FormValue("title")) | |
| 40 | + if title == "" { | |
| 41 | + http.Error(w, "title is required", http.StatusBadRequest) | |
| 42 | + return | |
| 43 | + } | |
| 44 | + fields := make([]string, len(app.Fields)) | |
| 45 | + for i := range app.Fields { | |
| 46 | + fields[i] = strings.TrimSpace(r.FormValue("field" + strconv.Itoa(i))) | |
| 47 | + } | |
| 48 | + h.render(w, "item", h.store.Add(title, fields)) | |
| 49 | +} | |
| 50 | + | |
| 51 | +func (h *handlers) search(w http.ResponseWriter, r *http.Request) { | |
| 52 | + h.render(w, "items", h.store.Search(r.URL.Query().Get("q"))) | |
| 53 | +} | |
| 54 | + | |
| 55 | +func (h *handlers) toggle(w http.ResponseWriter, r *http.Request) { | |
| 56 | + id, err := strconv.Atoi(r.PathValue("id")) | |
| 57 | + if err != nil { | |
| 58 | + http.Error(w, "bad id", http.StatusBadRequest) | |
| 59 | + return | |
| 60 | + } | |
| 61 | + it, ok := h.store.Toggle(id) | |
| 62 | + if !ok { | |
| 63 | + http.NotFound(w, r) | |
| 64 | + return | |
| 65 | + } | |
| 66 | + h.render(w, "item", it) | |
| 67 | +} | |
| 68 | + | |
| 69 | +// delete answers 200 with an empty body: htmx swaps the <li> for nothing. | |
| 70 | +func (h *handlers) delete(w http.ResponseWriter, r *http.Request) { | |
| 71 | + id, err := strconv.Atoi(r.PathValue("id")) | |
| 72 | + if err != nil { | |
| 73 | + http.Error(w, "bad id", http.StatusBadRequest) | |
| 74 | + return | |
| 75 | + } | |
| 76 | + if !h.store.Delete(id) { | |
| 77 | + http.NotFound(w, r) | |
| 78 | + return | |
| 79 | + } | |
| 80 | + w.WriteHeader(http.StatusOK) | |
| 81 | +} | |
| new file mode 100644 | |||
| @@ -0,0 +1,81 @@ | |||
| 1 | +package main | ||
| 2 | + | ||
| 3 | +import ( | ||
| 4 | + "html/template" | ||
| 5 | + "log/slog" | ||
| 6 | + "net/http" | ||
| 7 | + "strconv" | ||
| 8 | + "strings" | ||
| 9 | +) | ||
| 10 | + | ||
| 11 | +type handlers struct { | ||
| 12 | + store *store | ||
| 13 | + tmpl *template.Template | ||
| 14 | +} | ||
| 15 | + | ||
| 16 | +func (h *handlers) routes() http.Handler { | ||
| 17 | + mux := http.NewServeMux() | ||
| 18 | + mux.HandleFunc("GET /{$}", h.index) | ||
| 19 | + mux.HandleFunc("POST /items", h.create) | ||
| 20 | + mux.HandleFunc("GET /items/search", h.search) | ||
| 21 | + mux.HandleFunc("POST /items/{id}/toggle", h.toggle) | ||
| 22 | + mux.HandleFunc("DELETE /items/{id}", h.delete) | ||
| 23 | + return mux | ||
| 24 | +} | ||
| 25 | + | ||
| 26 | +// render writes a template: "index.html" for the page, "items" or "item" for htmx fragments. | ||
| 27 | +func (h *handlers) render(w http.ResponseWriter, name string, data any) { | ||
| 28 | + w.Header().Set("Content-Type", "text/html; charset=utf-8") | ||
| 29 | + if err := h.tmpl.ExecuteTemplate(w, name, data); err != nil { | ||
| 30 | + slog.Error("render", "template", name, "err", err) | ||
| 31 | + } | ||
| 32 | +} | ||
| 33 | + | ||
| 34 | +func (h *handlers) index(w http.ResponseWriter, r *http.Request) { | ||
| 35 | + h.render(w, "index.html", map[string]any{"Items": h.store.List()}) | ||
| 36 | +} | ||
| 37 | + | ||
| 38 | +func (h *handlers) create(w http.ResponseWriter, r *http.Request) { | ||
| 39 | + title := strings.TrimSpace(r.FormValue("title")) | ||
| 40 | + if title == "" { | ||
| 41 | + http.Error(w, "title is required", http.StatusBadRequest) | ||
| 42 | + return | ||
| 43 | + } | ||
| 44 | + fields := make([]string, len(app.Fields)) | ||
| 45 | + for i := range app.Fields { | ||
| 46 | + fields[i] = strings.TrimSpace(r.FormValue("field" + strconv.Itoa(i))) | ||
| 47 | + } | ||
| 48 | + h.render(w, "item", h.store.Add(title, fields)) | ||
| 49 | +} | ||
| 50 | + | ||
| 51 | +func (h *handlers) search(w http.ResponseWriter, r *http.Request) { | ||
| 52 | + h.render(w, "items", h.store.Search(r.URL.Query().Get("q"))) | ||
| 53 | +} | ||
| 54 | + | ||
| 55 | +func (h *handlers) toggle(w http.ResponseWriter, r *http.Request) { | ||
| 56 | + id, err := strconv.Atoi(r.PathValue("id")) | ||
| 57 | + if err != nil { | ||
| 58 | + http.Error(w, "bad id", http.StatusBadRequest) | ||
| 59 | + return | ||
| 60 | + } | ||
| 61 | + it, ok := h.store.Toggle(id) | ||
| 62 | + if !ok { | ||
| 63 | + http.NotFound(w, r) | ||
| 64 | + return | ||
| 65 | + } | ||
| 66 | + h.render(w, "item", it) | ||
| 67 | +} | ||
| 68 | + | ||
| 69 | +// delete answers 200 with an empty body: htmx swaps the <li> for nothing. | ||
| 70 | +func (h *handlers) delete(w http.ResponseWriter, r *http.Request) { | ||
| 71 | + id, err := strconv.Atoi(r.PathValue("id")) | ||
| 72 | + if err != nil { | ||
| 73 | + http.Error(w, "bad id", http.StatusBadRequest) | ||
| 74 | + return | ||
| 75 | + } | ||
| 76 | + if !h.store.Delete(id) { | ||
| 77 | + http.NotFound(w, r) | ||
| 78 | + return | ||
| 79 | + } | ||
| 80 | + w.WriteHeader(http.StatusOK) | ||
| 81 | +} | ||
added
demos/team/starter/handlers_test.go +80 -0 | new file mode 100644 | ||
| @@ -0,0 +1,80 @@ | ||
| 1 | +package main | |
| 2 | + | |
| 3 | +import ( | |
| 4 | + "html/template" | |
| 5 | + "net/http" | |
| 6 | + "net/http/httptest" | |
| 7 | + "net/url" | |
| 8 | + "strconv" | |
| 9 | + "strings" | |
| 10 | + "testing" | |
| 11 | +) | |
| 12 | + | |
| 13 | +func newTestServer(t *testing.T) http.Handler { | |
| 14 | + t.Helper() | |
| 15 | + h := &handlers{store: newStore(), tmpl: parseTemplates()} | |
| 16 | + return h.routes() | |
| 17 | +} | |
| 18 | + | |
| 19 | +// The expected texts come from app.go, so these tests keep passing when the | |
| 20 | +// app is adapted to another subject. | |
| 21 | +func TestRoutes(t *testing.T) { | |
| 22 | + if len(app.Seeds) == 0 { | |
| 23 | + t.Fatal("app.Seeds must hold at least one item") | |
| 24 | + } | |
| 25 | + created := url.Values{"title": {"New entry"}} | |
| 26 | + for i := range app.Fields { | |
| 27 | + created.Set("field"+strconv.Itoa(i), "value "+strconv.Itoa(i)) | |
| 28 | + } | |
| 29 | + first := app.Seeds[0].Title | |
| 30 | + | |
| 31 | + tests := []struct { | |
| 32 | + name string | |
| 33 | + method string | |
| 34 | + path string | |
| 35 | + form url.Values | |
| 36 | + wantCode int | |
| 37 | + wantBody string | |
| 38 | + }{ | |
| 39 | + {"page", "GET", "/", nil, http.StatusOK, "htmx.org"}, | |
| 40 | + {"page title", "GET", "/", nil, http.StatusOK, template.HTMLEscapeString(app.Title)}, | |
| 41 | + {"create", "POST", "/items", created, http.StatusOK, "New entry"}, | |
| 42 | + {"create empty", "POST", "/items", url.Values{"title": {" "}}, http.StatusBadRequest, ""}, | |
| 43 | + {"search", "GET", "/items/search?q=" + url.QueryEscape(first), nil, http.StatusOK, template.HTMLEscapeString(first)}, | |
| 44 | + {"toggle", "POST", "/items/1/toggle", nil, http.StatusOK, `class="done"`}, | |
| 45 | + {"toggle unknown", "POST", "/items/99/toggle", nil, http.StatusNotFound, ""}, | |
| 46 | + {"delete", "DELETE", "/items/1", nil, http.StatusOK, ""}, | |
| 47 | + {"delete unknown", "DELETE", "/items/99", nil, http.StatusNotFound, ""}, | |
| 48 | + } | |
| 49 | + for _, tt := range tests { | |
| 50 | + t.Run(tt.name, func(t *testing.T) { | |
| 51 | + srv := newTestServer(t) | |
| 52 | + req := httptest.NewRequest(tt.method, tt.path, strings.NewReader(tt.form.Encode())) | |
| 53 | + if tt.form != nil { | |
| 54 | + req.Header.Set("Content-Type", "application/x-www-form-urlencoded") | |
| 55 | + } | |
| 56 | + rec := httptest.NewRecorder() | |
| 57 | + srv.ServeHTTP(rec, req) | |
| 58 | + if rec.Code != tt.wantCode { | |
| 59 | + t.Fatalf("status = %d, want %d", rec.Code, tt.wantCode) | |
| 60 | + } | |
| 61 | + if !strings.Contains(rec.Body.String(), tt.wantBody) { | |
| 62 | + t.Errorf("body does not contain %q:\n%s", tt.wantBody, rec.Body) | |
| 63 | + } | |
| 64 | + }) | |
| 65 | + } | |
| 66 | +} | |
| 67 | + | |
| 68 | +func TestCreateKeepsFields(t *testing.T) { | |
| 69 | + if len(app.Fields) == 0 { | |
| 70 | + t.Skip("no extra fields in app.go") | |
| 71 | + } | |
| 72 | + form := url.Values{"title": {"With fields"}, "field0": {"first field value"}} | |
| 73 | + req := httptest.NewRequest("POST", "/items", strings.NewReader(form.Encode())) | |
| 74 | + req.Header.Set("Content-Type", "application/x-www-form-urlencoded") | |
| 75 | + rec := httptest.NewRecorder() | |
| 76 | + newTestServer(t).ServeHTTP(rec, req) | |
| 77 | + if !strings.Contains(rec.Body.String(), "first field value") { | |
| 78 | + t.Errorf("the fragment does not show the field:\n%s", rec.Body) | |
| 79 | + } | |
| 80 | +} | |
| new file mode 100644 | |||
| @@ -0,0 +1,80 @@ | |||
| 1 | +package main | ||
| 2 | + | ||
| 3 | +import ( | ||
| 4 | + "html/template" | ||
| 5 | + "net/http" | ||
| 6 | + "net/http/httptest" | ||
| 7 | + "net/url" | ||
| 8 | + "strconv" | ||
| 9 | + "strings" | ||
| 10 | + "testing" | ||
| 11 | +) | ||
| 12 | + | ||
| 13 | +func newTestServer(t *testing.T) http.Handler { | ||
| 14 | + t.Helper() | ||
| 15 | + h := &handlers{store: newStore(), tmpl: parseTemplates()} | ||
| 16 | + return h.routes() | ||
| 17 | +} | ||
| 18 | + | ||
| 19 | +// The expected texts come from app.go, so these tests keep passing when the | ||
| 20 | +// app is adapted to another subject. | ||
| 21 | +func TestRoutes(t *testing.T) { | ||
| 22 | + if len(app.Seeds) == 0 { | ||
| 23 | + t.Fatal("app.Seeds must hold at least one item") | ||
| 24 | + } | ||
| 25 | + created := url.Values{"title": {"New entry"}} | ||
| 26 | + for i := range app.Fields { | ||
| 27 | + created.Set("field"+strconv.Itoa(i), "value "+strconv.Itoa(i)) | ||
| 28 | + } | ||
| 29 | + first := app.Seeds[0].Title | ||
| 30 | + | ||
| 31 | + tests := []struct { | ||
| 32 | + name string | ||
| 33 | + method string | ||
| 34 | + path string | ||
| 35 | + form url.Values | ||
| 36 | + wantCode int | ||
| 37 | + wantBody string | ||
| 38 | + }{ | ||
| 39 | + {"page", "GET", "/", nil, http.StatusOK, "htmx.org"}, | ||
| 40 | + {"page title", "GET", "/", nil, http.StatusOK, template.HTMLEscapeString(app.Title)}, | ||
| 41 | + {"create", "POST", "/items", created, http.StatusOK, "New entry"}, | ||
| 42 | + {"create empty", "POST", "/items", url.Values{"title": {" "}}, http.StatusBadRequest, ""}, | ||
| 43 | + {"search", "GET", "/items/search?q=" + url.QueryEscape(first), nil, http.StatusOK, template.HTMLEscapeString(first)}, | ||
| 44 | + {"toggle", "POST", "/items/1/toggle", nil, http.StatusOK, `class="done"`}, | ||
| 45 | + {"toggle unknown", "POST", "/items/99/toggle", nil, http.StatusNotFound, ""}, | ||
| 46 | + {"delete", "DELETE", "/items/1", nil, http.StatusOK, ""}, | ||
| 47 | + {"delete unknown", "DELETE", "/items/99", nil, http.StatusNotFound, ""}, | ||
| 48 | + } | ||
| 49 | + for _, tt := range tests { | ||
| 50 | + t.Run(tt.name, func(t *testing.T) { | ||
| 51 | + srv := newTestServer(t) | ||
| 52 | + req := httptest.NewRequest(tt.method, tt.path, strings.NewReader(tt.form.Encode())) | ||
| 53 | + if tt.form != nil { | ||
| 54 | + req.Header.Set("Content-Type", "application/x-www-form-urlencoded") | ||
| 55 | + } | ||
| 56 | + rec := httptest.NewRecorder() | ||
| 57 | + srv.ServeHTTP(rec, req) | ||
| 58 | + if rec.Code != tt.wantCode { | ||
| 59 | + t.Fatalf("status = %d, want %d", rec.Code, tt.wantCode) | ||
| 60 | + } | ||
| 61 | + if !strings.Contains(rec.Body.String(), tt.wantBody) { | ||
| 62 | + t.Errorf("body does not contain %q:\n%s", tt.wantBody, rec.Body) | ||
| 63 | + } | ||
| 64 | + }) | ||
| 65 | + } | ||
| 66 | +} | ||
| 67 | + | ||
| 68 | +func TestCreateKeepsFields(t *testing.T) { | ||
| 69 | + if len(app.Fields) == 0 { | ||
| 70 | + t.Skip("no extra fields in app.go") | ||
| 71 | + } | ||
| 72 | + form := url.Values{"title": {"With fields"}, "field0": {"first field value"}} | ||
| 73 | + req := httptest.NewRequest("POST", "/items", strings.NewReader(form.Encode())) | ||
| 74 | + req.Header.Set("Content-Type", "application/x-www-form-urlencoded") | ||
| 75 | + rec := httptest.NewRecorder() | ||
| 76 | + newTestServer(t).ServeHTTP(rec, req) | ||
| 77 | + if !strings.Contains(rec.Body.String(), "first field value") { | ||
| 78 | + t.Errorf("the fragment does not show the field:\n%s", rec.Body) | ||
| 79 | + } | ||
| 80 | +} | ||
added
demos/team/starter/main.go +45 -0 | new file mode 100644 | ||
| @@ -0,0 +1,45 @@ | ||
| 1 | +package main | |
| 2 | + | |
| 3 | +import ( | |
| 4 | + "context" | |
| 5 | + "errors" | |
| 6 | + "log/slog" | |
| 7 | + "net/http" | |
| 8 | + "os" | |
| 9 | + "os/signal" | |
| 10 | + "syscall" | |
| 11 | + "time" | |
| 12 | +) | |
| 13 | + | |
| 14 | +func main() { | |
| 15 | + h := &handlers{store: newStore(), tmpl: parseTemplates()} | |
| 16 | + | |
| 17 | + port := os.Getenv("PORT") | |
| 18 | + if port == "" { | |
| 19 | + port = "8080" | |
| 20 | + } | |
| 21 | + srv := &http.Server{ | |
| 22 | + Addr: ":" + port, | |
| 23 | + Handler: h.routes(), | |
| 24 | + ReadHeaderTimeout: 5 * time.Second, | |
| 25 | + } | |
| 26 | + | |
| 27 | + ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM) | |
| 28 | + defer stop() | |
| 29 | + | |
| 30 | + go func() { | |
| 31 | + slog.Info("listening", "url", "http://localhost:"+port) | |
| 32 | + if err := srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) { | |
| 33 | + slog.Error("server stopped", "err", err) | |
| 34 | + stop() | |
| 35 | + } | |
| 36 | + }() | |
| 37 | + | |
| 38 | + <-ctx.Done() | |
| 39 | + shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second) | |
| 40 | + defer cancel() | |
| 41 | + if err := srv.Shutdown(shutdownCtx); err != nil { | |
| 42 | + slog.Error("shutdown", "err", err) | |
| 43 | + } | |
| 44 | + slog.Info("bye") | |
| 45 | +} | |
| new file mode 100644 | |||
| @@ -0,0 +1,45 @@ | |||
| 1 | +package main | ||
| 2 | + | ||
| 3 | +import ( | ||
| 4 | + "context" | ||
| 5 | + "errors" | ||
| 6 | + "log/slog" | ||
| 7 | + "net/http" | ||
| 8 | + "os" | ||
| 9 | + "os/signal" | ||
| 10 | + "syscall" | ||
| 11 | + "time" | ||
| 12 | +) | ||
| 13 | + | ||
| 14 | +func main() { | ||
| 15 | + h := &handlers{store: newStore(), tmpl: parseTemplates()} | ||
| 16 | + | ||
| 17 | + port := os.Getenv("PORT") | ||
| 18 | + if port == "" { | ||
| 19 | + port = "8080" | ||
| 20 | + } | ||
| 21 | + srv := &http.Server{ | ||
| 22 | + Addr: ":" + port, | ||
| 23 | + Handler: h.routes(), | ||
| 24 | + ReadHeaderTimeout: 5 * time.Second, | ||
| 25 | + } | ||
| 26 | + | ||
| 27 | + ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM) | ||
| 28 | + defer stop() | ||
| 29 | + | ||
| 30 | + go func() { | ||
| 31 | + slog.Info("listening", "url", "http://localhost:"+port) | ||
| 32 | + if err := srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) { | ||
| 33 | + slog.Error("server stopped", "err", err) | ||
| 34 | + stop() | ||
| 35 | + } | ||
| 36 | + }() | ||
| 37 | + | ||
| 38 | + <-ctx.Done() | ||
| 39 | + shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second) | ||
| 40 | + defer cancel() | ||
| 41 | + if err := srv.Shutdown(shutdownCtx); err != nil { | ||
| 42 | + slog.Error("shutdown", "err", err) | ||
| 43 | + } | ||
| 44 | + slog.Info("bye") | ||
| 45 | +} | ||
added
demos/team/starter/store.go +82 -0 | new file mode 100644 | ||
| @@ -0,0 +1,82 @@ | ||
| 1 | +package main | |
| 2 | + | |
| 3 | +import ( | |
| 4 | + "strings" | |
| 5 | + "sync" | |
| 6 | +) | |
| 7 | + | |
| 8 | +// Item is one entry of the list. Fields holds one value per app.Fields label. | |
| 9 | +type Item struct { | |
| 10 | + ID int | |
| 11 | + Title string | |
| 12 | + Fields []string | |
| 13 | + Done bool | |
| 14 | +} | |
| 15 | + | |
| 16 | +// store keeps the items in memory, safe for concurrent handlers. | |
| 17 | +type store struct { | |
| 18 | + mu sync.Mutex | |
| 19 | + items []Item | |
| 20 | + nextID int | |
| 21 | +} | |
| 22 | + | |
| 23 | +func newStore() *store { | |
| 24 | + s := &store{nextID: 1} | |
| 25 | + for _, sd := range app.Seeds { | |
| 26 | + s.Add(sd.Title, sd.Fields) | |
| 27 | + } | |
| 28 | + return s | |
| 29 | +} | |
| 30 | + | |
| 31 | +func (s *store) List() []Item { | |
| 32 | + s.mu.Lock() | |
| 33 | + defer s.mu.Unlock() | |
| 34 | + return append([]Item(nil), s.items...) | |
| 35 | +} | |
| 36 | + | |
| 37 | +func (s *store) Add(title string, fields []string) Item { | |
| 38 | + s.mu.Lock() | |
| 39 | + defer s.mu.Unlock() | |
| 40 | + it := Item{ID: s.nextID, Title: title, Fields: fields} | |
| 41 | + s.nextID++ | |
| 42 | + s.items = append(s.items, it) | |
| 43 | + return it | |
| 44 | +} | |
| 45 | + | |
| 46 | +// Toggle flips Done and returns the updated item; ok is false for an unknown id. | |
| 47 | +func (s *store) Toggle(id int) (Item, bool) { | |
| 48 | + s.mu.Lock() | |
| 49 | + defer s.mu.Unlock() | |
| 50 | + for i := range s.items { | |
| 51 | + if s.items[i].ID == id { | |
| 52 | + s.items[i].Done = !s.items[i].Done | |
| 53 | + return s.items[i], true | |
| 54 | + } | |
| 55 | + } | |
| 56 | + return Item{}, false | |
| 57 | +} | |
| 58 | + | |
| 59 | +func (s *store) Delete(id int) bool { | |
| 60 | + s.mu.Lock() | |
| 61 | + defer s.mu.Unlock() | |
| 62 | + for i := range s.items { | |
| 63 | + if s.items[i].ID == id { | |
| 64 | + s.items = append(s.items[:i], s.items[i+1:]...) | |
| 65 | + return true | |
| 66 | + } | |
| 67 | + } | |
| 68 | + return false | |
| 69 | +} | |
| 70 | + | |
| 71 | +// Search returns the items whose title or fields contain q, ignoring case. | |
| 72 | +func (s *store) Search(q string) []Item { | |
| 73 | + q = strings.ToLower(strings.TrimSpace(q)) | |
| 74 | + var found []Item | |
| 75 | + for _, it := range s.List() { | |
| 76 | + text := strings.ToLower(it.Title + " " + strings.Join(it.Fields, " ")) | |
| 77 | + if strings.Contains(text, q) { | |
| 78 | + found = append(found, it) | |
| 79 | + } | |
| 80 | + } | |
| 81 | + return found | |
| 82 | +} | |
| new file mode 100644 | |||
| @@ -0,0 +1,82 @@ | |||
| 1 | +package main | ||
| 2 | + | ||
| 3 | +import ( | ||
| 4 | + "strings" | ||
| 5 | + "sync" | ||
| 6 | +) | ||
| 7 | + | ||
| 8 | +// Item is one entry of the list. Fields holds one value per app.Fields label. | ||
| 9 | +type Item struct { | ||
| 10 | + ID int | ||
| 11 | + Title string | ||
| 12 | + Fields []string | ||
| 13 | + Done bool | ||
| 14 | +} | ||
| 15 | + | ||
| 16 | +// store keeps the items in memory, safe for concurrent handlers. | ||
| 17 | +type store struct { | ||
| 18 | + mu sync.Mutex | ||
| 19 | + items []Item | ||
| 20 | + nextID int | ||
| 21 | +} | ||
| 22 | + | ||
| 23 | +func newStore() *store { | ||
| 24 | + s := &store{nextID: 1} | ||
| 25 | + for _, sd := range app.Seeds { | ||
| 26 | + s.Add(sd.Title, sd.Fields) | ||
| 27 | + } | ||
| 28 | + return s | ||
| 29 | +} | ||
| 30 | + | ||
| 31 | +func (s *store) List() []Item { | ||
| 32 | + s.mu.Lock() | ||
| 33 | + defer s.mu.Unlock() | ||
| 34 | + return append([]Item(nil), s.items...) | ||
| 35 | +} | ||
| 36 | + | ||
| 37 | +func (s *store) Add(title string, fields []string) Item { | ||
| 38 | + s.mu.Lock() | ||
| 39 | + defer s.mu.Unlock() | ||
| 40 | + it := Item{ID: s.nextID, Title: title, Fields: fields} | ||
| 41 | + s.nextID++ | ||
| 42 | + s.items = append(s.items, it) | ||
| 43 | + return it | ||
| 44 | +} | ||
| 45 | + | ||
| 46 | +// Toggle flips Done and returns the updated item; ok is false for an unknown id. | ||
| 47 | +func (s *store) Toggle(id int) (Item, bool) { | ||
| 48 | + s.mu.Lock() | ||
| 49 | + defer s.mu.Unlock() | ||
| 50 | + for i := range s.items { | ||
| 51 | + if s.items[i].ID == id { | ||
| 52 | + s.items[i].Done = !s.items[i].Done | ||
| 53 | + return s.items[i], true | ||
| 54 | + } | ||
| 55 | + } | ||
| 56 | + return Item{}, false | ||
| 57 | +} | ||
| 58 | + | ||
| 59 | +func (s *store) Delete(id int) bool { | ||
| 60 | + s.mu.Lock() | ||
| 61 | + defer s.mu.Unlock() | ||
| 62 | + for i := range s.items { | ||
| 63 | + if s.items[i].ID == id { | ||
| 64 | + s.items = append(s.items[:i], s.items[i+1:]...) | ||
| 65 | + return true | ||
| 66 | + } | ||
| 67 | + } | ||
| 68 | + return false | ||
| 69 | +} | ||
| 70 | + | ||
| 71 | +// Search returns the items whose title or fields contain q, ignoring case. | ||
| 72 | +func (s *store) Search(q string) []Item { | ||
| 73 | + q = strings.ToLower(strings.TrimSpace(q)) | ||
| 74 | + var found []Item | ||
| 75 | + for _, it := range s.List() { | ||
| 76 | + text := strings.ToLower(it.Title + " " + strings.Join(it.Fields, " ")) | ||
| 77 | + if strings.Contains(text, q) { | ||
| 78 | + found = append(found, it) | ||
| 79 | + } | ||
| 80 | + } | ||
| 81 | + return found | ||
| 82 | +} | ||
added
demos/team/starter/templates.go +16 -0 | new file mode 100644 | ||
| @@ -0,0 +1,16 @@ | ||
| 1 | +package main | |
| 2 | + | |
| 3 | +import ( | |
| 4 | + "embed" | |
| 5 | + "html/template" | |
| 6 | +) | |
| 7 | + | |
| 8 | +//go:embed templates/*.html | |
| 9 | +var templatesFS embed.FS | |
| 10 | + | |
| 11 | +// parseTemplates loads the page and its fragments; `app` gives them the | |
| 12 | +// texts of app.go. | |
| 13 | +func parseTemplates() *template.Template { | |
| 14 | + funcs := template.FuncMap{"app": func() appInfo { return app }} | |
| 15 | + return template.Must(template.New("").Funcs(funcs).ParseFS(templatesFS, "templates/*.html")) | |
| 16 | +} | |
| new file mode 100644 | |||
| @@ -0,0 +1,16 @@ | |||
| 1 | +package main | ||
| 2 | + | ||
| 3 | +import ( | ||
| 4 | + "embed" | ||
| 5 | + "html/template" | ||
| 6 | +) | ||
| 7 | + | ||
| 8 | +//go:embed templates/*.html | ||
| 9 | +var templatesFS embed.FS | ||
| 10 | + | ||
| 11 | +// parseTemplates loads the page and its fragments; `app` gives them the | ||
| 12 | +// texts of app.go. | ||
| 13 | +func parseTemplates() *template.Template { | ||
| 14 | + funcs := template.FuncMap{"app": func() appInfo { return app }} | ||
| 15 | + return template.Must(template.New("").Funcs(funcs).ParseFS(templatesFS, "templates/*.html")) | ||
| 16 | +} | ||
added
demos/team/starter/templates/index.html +40 -0 | new file mode 100644 | ||
| @@ -0,0 +1,40 @@ | ||
| 1 | +<!doctype html> | |
| 2 | +<html lang="{{(app).Lang}}"> | |
| 3 | +<head> | |
| 4 | + <meta charset="utf-8"> | |
| 5 | + <meta name="viewport" content="width=device-width, initial-scale=1"> | |
| 6 | + <title>{{(app).Title}}</title> | |
| 7 | + <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@picocss/pico@2/css/pico.min.css"> | |
| 8 | + <script src="https://unpkg.com/htmx.org@2.0.4"></script> | |
| 9 | + <style> | |
| 10 | + #items li { display: flex; flex-wrap: wrap; align-items: center; gap: .75rem; } | |
| 11 | + #items li.done span { text-decoration: line-through; opacity: .6; } | |
| 12 | + #items li button { margin-left: auto; width: auto; padding: .25rem .75rem; } | |
| 13 | + @media (max-width: 576px) { form fieldset[role=group] { flex-direction: column; } } | |
| 14 | + </style> | |
| 15 | +</head> | |
| 16 | +<body> | |
| 17 | + <main class="container"> | |
| 18 | + <h1>{{(app).Title}}</h1> | |
| 19 | + <form hx-post="/items" hx-target="#items" hx-swap="beforeend" hx-on::after-request="this.reset()"> | |
| 20 | + <fieldset role="group"> | |
| 21 | + <input name="title" required placeholder="{{(app).TitleLabel}}" aria-label="{{(app).TitleLabel}}"> | |
| 22 | + {{range $i, $label := (app).Fields}}<input name="field{{$i}}" placeholder="{{$label}}" aria-label="{{$label}}"> | |
| 23 | + {{end}}<button>{{(app).AddLabel}}</button> | |
| 24 | + </fieldset> | |
| 25 | + </form> | |
| 26 | + <input type="search" name="q" placeholder="{{(app).SearchLabel}}" aria-label="{{(app).SearchLabel}}" | |
| 27 | + hx-get="/items/search" hx-trigger="input changed delay:300ms, search" hx-target="#items"> | |
| 28 | + <ul id="items">{{template "items" .Items}}</ul> | |
| 29 | + </main> | |
| 30 | +</body> | |
| 31 | +</html> | |
| 32 | + | |
| 33 | +{{define "items"}}{{range .}}{{template "item" .}}{{end}}{{end}} | |
| 34 | + | |
| 35 | +{{define "item"}}<li id="item-{{.ID}}"{{if .Done}} class="done"{{end}}> | |
| 36 | + {{if (app).DoneLabel}}<input type="checkbox" aria-label="{{(app).DoneLabel}}" title="{{(app).DoneLabel}}" hx-post="/items/{{.ID}}/toggle" hx-target="#item-{{.ID}}" hx-swap="outerHTML"{{if .Done}} checked{{end}}>{{end}} | |
| 37 | + <span>{{.Title}}</span> | |
| 38 | + {{range .Fields}}{{if .}}<small>{{.}}</small>{{end}}{{end}} | |
| 39 | + <button class="secondary outline" hx-delete="/items/{{.ID}}" hx-target="#item-{{.ID}}" hx-swap="outerHTML" hx-confirm="{{(app).DeleteConfirm}}">✕</button> | |
| 40 | +</li>{{end}} | |
| new file mode 100644 | |||
| @@ -0,0 +1,40 @@ | |||
| 1 | +<!doctype html> | ||
| 2 | +<html lang="{{(app).Lang}}"> | ||
| 3 | +<head> | ||
| 4 | + <meta charset="utf-8"> | ||
| 5 | + <meta name="viewport" content="width=device-width, initial-scale=1"> | ||
| 6 | + <title>{{(app).Title}}</title> | ||
| 7 | + <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@picocss/pico@2/css/pico.min.css"> | ||
| 8 | + <script src="https://unpkg.com/htmx.org@2.0.4"></script> | ||
| 9 | + <style> | ||
| 10 | + #items li { display: flex; flex-wrap: wrap; align-items: center; gap: .75rem; } | ||
| 11 | + #items li.done span { text-decoration: line-through; opacity: .6; } | ||
| 12 | + #items li button { margin-left: auto; width: auto; padding: .25rem .75rem; } | ||
| 13 | + @media (max-width: 576px) { form fieldset[role=group] { flex-direction: column; } } | ||
| 14 | + </style> | ||
| 15 | +</head> | ||
| 16 | +<body> | ||
| 17 | + <main class="container"> | ||
| 18 | + <h1>{{(app).Title}}</h1> | ||
| 19 | + <form hx-post="/items" hx-target="#items" hx-swap="beforeend" hx-on::after-request="this.reset()"> | ||
| 20 | + <fieldset role="group"> | ||
| 21 | + <input name="title" required placeholder="{{(app).TitleLabel}}" aria-label="{{(app).TitleLabel}}"> | ||
| 22 | + {{range $i, $label := (app).Fields}}<input name="field{{$i}}" placeholder="{{$label}}" aria-label="{{$label}}"> | ||
| 23 | + {{end}}<button>{{(app).AddLabel}}</button> | ||
| 24 | + </fieldset> | ||
| 25 | + </form> | ||
| 26 | + <input type="search" name="q" placeholder="{{(app).SearchLabel}}" aria-label="{{(app).SearchLabel}}" | ||
| 27 | + hx-get="/items/search" hx-trigger="input changed delay:300ms, search" hx-target="#items"> | ||
| 28 | + <ul id="items">{{template "items" .Items}}</ul> | ||
| 29 | + </main> | ||
| 30 | +</body> | ||
| 31 | +</html> | ||
| 32 | + | ||
| 33 | +{{define "items"}}{{range .}}{{template "item" .}}{{end}}{{end}} | ||
| 34 | + | ||
| 35 | +{{define "item"}}<li id="item-{{.ID}}"{{if .Done}} class="done"{{end}}> | ||
| 36 | + {{if (app).DoneLabel}}<input type="checkbox" aria-label="{{(app).DoneLabel}}" title="{{(app).DoneLabel}}" hx-post="/items/{{.ID}}/toggle" hx-target="#item-{{.ID}}" hx-swap="outerHTML"{{if .Done}} checked{{end}}>{{end}} | ||
| 37 | + <span>{{.Title}}</span> | ||
| 38 | + {{range .Fields}}{{if .}}<small>{{.}}</small>{{end}}{{end}} | ||
| 39 | + <button class="secondary outline" hx-delete="/items/{{.ID}}" hx-target="#item-{{.ID}}" hx-swap="outerHTML" hx-confirm="{{(app).DeleteConfirm}}">✕</button> | ||
| 40 | +</li>{{end}} | ||