create-hayate 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- create_hayate-0.1.0/LICENSE +21 -0
- create_hayate-0.1.0/PKG-INFO +54 -0
- create_hayate-0.1.0/README.md +35 -0
- create_hayate-0.1.0/pyproject.toml +48 -0
- create_hayate-0.1.0/src/create_hayate/__init__.py +3 -0
- create_hayate-0.1.0/src/create_hayate/__main__.py +6 -0
- create_hayate-0.1.0/src/create_hayate/cli.py +112 -0
- create_hayate-0.1.0/src/create_hayate/templates/api/README.md +26 -0
- create_hayate-0.1.0/src/create_hayate/templates/api/app.py +49 -0
- create_hayate-0.1.0/src/create_hayate/templates/api/gitignore +3 -0
- create_hayate-0.1.0/src/create_hayate/templates/api/pyproject.toml +17 -0
- create_hayate-0.1.0/src/create_hayate/templates/api/tests/test_app.py +28 -0
- create_hayate-0.1.0/src/create_hayate/templates/workers/README.md +33 -0
- create_hayate-0.1.0/src/create_hayate/templates/workers/app.py +49 -0
- create_hayate-0.1.0/src/create_hayate/templates/workers/entry.py +6 -0
- create_hayate-0.1.0/src/create_hayate/templates/workers/gitignore +6 -0
- create_hayate-0.1.0/src/create_hayate/templates/workers/pyproject.toml +17 -0
- create_hayate-0.1.0/src/create_hayate/templates/workers/tests/test_app.py +28 -0
- create_hayate-0.1.0/src/create_hayate/templates/workers/wrangler.toml +4 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Yusuke Hayashi
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: create-hayate
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Scaffold a hayate project that is tested from minute one: uvx create-hayate my-app
|
|
5
|
+
Keywords: hayate,scaffolding,project-template,cli
|
|
6
|
+
Author: Yusuke Hayashi
|
|
7
|
+
License-Expression: MIT
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Classifier: Development Status :: 3 - Alpha
|
|
10
|
+
Classifier: Environment :: Console
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
15
|
+
Classifier: Topic :: Software Development :: Code Generators
|
|
16
|
+
Requires-Python: >=3.12
|
|
17
|
+
Project-URL: Repository, https://github.com/hayatepy/create-hayate
|
|
18
|
+
Description-Content-Type: text/markdown
|
|
19
|
+
|
|
20
|
+
# create-hayate
|
|
21
|
+
|
|
22
|
+
Project scaffolding for [hayate](https://github.com/hayatepy/hayate) —
|
|
23
|
+
`uvx create-hayate my-app` and you have a running, tested project in minutes.
|
|
24
|
+
|
|
25
|
+
```sh
|
|
26
|
+
uvx create-hayate my-app --template workers
|
|
27
|
+
cd my-app
|
|
28
|
+
uv run pytest # green out of the box
|
|
29
|
+
uv run pywrangler dev
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## Templates
|
|
33
|
+
|
|
34
|
+
| Name | What you get | Serve with |
|
|
35
|
+
|---|---|---|
|
|
36
|
+
| `api` (default) | TODO API + tests that call the app core directly | `uv run uvicorn app:app --reload` |
|
|
37
|
+
| `workers` | The same app on Cloudflare Python Workers | `uv run pywrangler dev` / `deploy` |
|
|
38
|
+
|
|
39
|
+
`lambda`, `mcp`, and `auth` templates follow as those packages ship.
|
|
40
|
+
|
|
41
|
+
## Design
|
|
42
|
+
|
|
43
|
+
- **Zero-dependency CLI** (stdlib only: argparse + `string.Template`), templates
|
|
44
|
+
bundled in the package — no network fetch, works offline, version-pinned.
|
|
45
|
+
- Templates mirror the upstream `examples/` style, and CI generates every
|
|
46
|
+
template and runs its test suite, so they can't rot.
|
|
47
|
+
- One question at most (the template); `--no-input` for scripts and CI.
|
|
48
|
+
|
|
49
|
+
The internal design memo (Japanese, per project convention) lives in
|
|
50
|
+
[DESIGN.md](DESIGN.md).
|
|
51
|
+
|
|
52
|
+
## License
|
|
53
|
+
|
|
54
|
+
MIT
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# create-hayate
|
|
2
|
+
|
|
3
|
+
Project scaffolding for [hayate](https://github.com/hayatepy/hayate) —
|
|
4
|
+
`uvx create-hayate my-app` and you have a running, tested project in minutes.
|
|
5
|
+
|
|
6
|
+
```sh
|
|
7
|
+
uvx create-hayate my-app --template workers
|
|
8
|
+
cd my-app
|
|
9
|
+
uv run pytest # green out of the box
|
|
10
|
+
uv run pywrangler dev
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Templates
|
|
14
|
+
|
|
15
|
+
| Name | What you get | Serve with |
|
|
16
|
+
|---|---|---|
|
|
17
|
+
| `api` (default) | TODO API + tests that call the app core directly | `uv run uvicorn app:app --reload` |
|
|
18
|
+
| `workers` | The same app on Cloudflare Python Workers | `uv run pywrangler dev` / `deploy` |
|
|
19
|
+
|
|
20
|
+
`lambda`, `mcp`, and `auth` templates follow as those packages ship.
|
|
21
|
+
|
|
22
|
+
## Design
|
|
23
|
+
|
|
24
|
+
- **Zero-dependency CLI** (stdlib only: argparse + `string.Template`), templates
|
|
25
|
+
bundled in the package — no network fetch, works offline, version-pinned.
|
|
26
|
+
- Templates mirror the upstream `examples/` style, and CI generates every
|
|
27
|
+
template and runs its test suite, so they can't rot.
|
|
28
|
+
- One question at most (the template); `--no-input` for scripts and CI.
|
|
29
|
+
|
|
30
|
+
The internal design memo (Japanese, per project convention) lives in
|
|
31
|
+
[DESIGN.md](DESIGN.md).
|
|
32
|
+
|
|
33
|
+
## License
|
|
34
|
+
|
|
35
|
+
MIT
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["uv_build>=0.9,<1"]
|
|
3
|
+
build-backend = "uv_build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "create-hayate"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Scaffold a hayate project that is tested from minute one: uvx create-hayate my-app"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.12"
|
|
11
|
+
license = "MIT"
|
|
12
|
+
license-files = ["LICENSE"]
|
|
13
|
+
authors = [{ name = "Yusuke Hayashi" }]
|
|
14
|
+
keywords = ["hayate", "scaffolding", "project-template", "cli"]
|
|
15
|
+
classifiers = [
|
|
16
|
+
"Development Status :: 3 - Alpha",
|
|
17
|
+
"Environment :: Console",
|
|
18
|
+
"Intended Audience :: Developers",
|
|
19
|
+
"Programming Language :: Python :: 3.12",
|
|
20
|
+
"Programming Language :: Python :: 3.13",
|
|
21
|
+
"Programming Language :: Python :: 3.14",
|
|
22
|
+
"Topic :: Software Development :: Code Generators",
|
|
23
|
+
]
|
|
24
|
+
dependencies = []
|
|
25
|
+
|
|
26
|
+
[project.scripts]
|
|
27
|
+
create-hayate = "create_hayate.cli:main"
|
|
28
|
+
|
|
29
|
+
[project.urls]
|
|
30
|
+
Repository = "https://github.com/hayatepy/create-hayate"
|
|
31
|
+
|
|
32
|
+
[dependency-groups]
|
|
33
|
+
dev = [
|
|
34
|
+
"pytest>=8.3",
|
|
35
|
+
"ruff>=0.9",
|
|
36
|
+
]
|
|
37
|
+
|
|
38
|
+
[tool.pytest.ini_options]
|
|
39
|
+
testpaths = ["tests"]
|
|
40
|
+
|
|
41
|
+
[tool.ruff]
|
|
42
|
+
line-length = 100
|
|
43
|
+
target-version = "py312"
|
|
44
|
+
src = ["src", "tests"]
|
|
45
|
+
extend-exclude = ["src/create_hayate/templates"]
|
|
46
|
+
|
|
47
|
+
[tool.ruff.lint]
|
|
48
|
+
select = ["E", "F", "W", "I", "UP", "B", "SIM", "RUF"]
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
"""The whole CLI. Zero dependencies by design: argparse + shutil + string.Template.
|
|
2
|
+
|
|
3
|
+
Template trees live in ``templates/<name>/`` inside this package and are copied
|
|
4
|
+
file by file through ``string.Template`` (``$project_name`` is the only
|
|
5
|
+
variable). Keeping the machinery this small is a feature — see DESIGN.md.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import argparse
|
|
11
|
+
import re
|
|
12
|
+
import shutil
|
|
13
|
+
import sys
|
|
14
|
+
from importlib.resources import files
|
|
15
|
+
from pathlib import Path
|
|
16
|
+
from string import Template
|
|
17
|
+
|
|
18
|
+
TEMPLATES: dict[str, str] = {
|
|
19
|
+
"api": "TODO API + pytest, served by uvicorn",
|
|
20
|
+
"workers": "the same app on Cloudflare Python Workers",
|
|
21
|
+
}
|
|
22
|
+
DEFAULT_TEMPLATE = "api"
|
|
23
|
+
|
|
24
|
+
# One name serves as directory, distribution, and Workers service name,
|
|
25
|
+
# so enforce the strictest of the three.
|
|
26
|
+
_NAME_RE = re.compile(r"^[a-z][a-z0-9-]*$")
|
|
27
|
+
|
|
28
|
+
# Bundled name -> generated name. Build backends drop dotfiles from wheels,
|
|
29
|
+
# so dotfiles are bundled without the leading dot.
|
|
30
|
+
_RENAMES = {"gitignore": ".gitignore"}
|
|
31
|
+
|
|
32
|
+
_SKIP_DIRS = {"__pycache__"}
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def _render_tree(src, dest: Path, variables: dict[str, str]) -> None:
|
|
36
|
+
dest.mkdir(parents=True, exist_ok=True)
|
|
37
|
+
for entry in src.iterdir():
|
|
38
|
+
if entry.name in _SKIP_DIRS:
|
|
39
|
+
continue
|
|
40
|
+
target = dest / _RENAMES.get(entry.name, entry.name)
|
|
41
|
+
if entry.is_dir():
|
|
42
|
+
_render_tree(entry, target, variables)
|
|
43
|
+
else:
|
|
44
|
+
text = entry.read_text(encoding="utf-8")
|
|
45
|
+
target.write_text(Template(text).substitute(variables), encoding="utf-8", newline="\n")
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def _choose_template() -> str:
|
|
49
|
+
names = list(TEMPLATES)
|
|
50
|
+
print("Which template?")
|
|
51
|
+
for i, name in enumerate(names, start=1):
|
|
52
|
+
print(f" {i}) {name:<8}- {TEMPLATES[name]}")
|
|
53
|
+
while True:
|
|
54
|
+
try:
|
|
55
|
+
raw = input(f"Choose 1-{len(names)} [1]: ").strip()
|
|
56
|
+
except EOFError:
|
|
57
|
+
return DEFAULT_TEMPLATE
|
|
58
|
+
if not raw:
|
|
59
|
+
return names[0]
|
|
60
|
+
if raw in TEMPLATES:
|
|
61
|
+
return raw
|
|
62
|
+
if raw.isdigit() and 1 <= int(raw) <= len(names):
|
|
63
|
+
return names[int(raw) - 1]
|
|
64
|
+
print(f"Please answer 1-{len(names)} or a template name.")
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def main(argv: list[str] | None = None) -> int:
|
|
68
|
+
parser = argparse.ArgumentParser(
|
|
69
|
+
prog="create-hayate",
|
|
70
|
+
description="Scaffold a hayate project that is tested from minute one.",
|
|
71
|
+
)
|
|
72
|
+
parser.add_argument("name", help="project name; a directory of this name is created")
|
|
73
|
+
parser.add_argument(
|
|
74
|
+
"--template",
|
|
75
|
+
choices=tuple(TEMPLATES),
|
|
76
|
+
help="project template (prompted interactively when omitted)",
|
|
77
|
+
)
|
|
78
|
+
parser.add_argument(
|
|
79
|
+
"--no-input",
|
|
80
|
+
action="store_true",
|
|
81
|
+
help=f"never prompt; --template or the default ({DEFAULT_TEMPLATE}) is used",
|
|
82
|
+
)
|
|
83
|
+
args = parser.parse_args(argv)
|
|
84
|
+
|
|
85
|
+
if not _NAME_RE.match(args.name):
|
|
86
|
+
parser.error(
|
|
87
|
+
f"invalid project name {args.name!r}: use lowercase letters, digits, and"
|
|
88
|
+
" hyphens, starting with a letter (the name doubles as the Workers"
|
|
89
|
+
" service name)"
|
|
90
|
+
)
|
|
91
|
+
dest = Path.cwd() / args.name
|
|
92
|
+
if dest.exists():
|
|
93
|
+
parser.error(f"{dest} already exists")
|
|
94
|
+
|
|
95
|
+
template = args.template
|
|
96
|
+
if template is None:
|
|
97
|
+
interactive = not args.no_input and sys.stdin is not None and sys.stdin.isatty()
|
|
98
|
+
template = _choose_template() if interactive else DEFAULT_TEMPLATE
|
|
99
|
+
|
|
100
|
+
src = files("create_hayate").joinpath("templates", template)
|
|
101
|
+
try:
|
|
102
|
+
_render_tree(src, dest, {"project_name": args.name})
|
|
103
|
+
except BaseException:
|
|
104
|
+
shutil.rmtree(dest, ignore_errors=True)
|
|
105
|
+
raise
|
|
106
|
+
|
|
107
|
+
serve = "uv run uvicorn app:app --reload" if template == "api" else "uv run pywrangler dev"
|
|
108
|
+
print(f"\nCreated {args.name}/ from the {template} template. Next:\n")
|
|
109
|
+
print(f" cd {args.name}")
|
|
110
|
+
print(" uv run pytest")
|
|
111
|
+
print(f" {serve}")
|
|
112
|
+
return 0
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# $project_name
|
|
2
|
+
|
|
3
|
+
A TODO API built with [hayate](https://github.com/hayatepy/hayate) —
|
|
4
|
+
web-standards-first, zero dependencies, tested without a server.
|
|
5
|
+
|
|
6
|
+
## Test
|
|
7
|
+
|
|
8
|
+
```sh
|
|
9
|
+
uv run pytest
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
Tests call the app core directly (`await app.request(...)`); there is no test
|
|
13
|
+
client or server to boot.
|
|
14
|
+
|
|
15
|
+
## Serve
|
|
16
|
+
|
|
17
|
+
```sh
|
|
18
|
+
uv run uvicorn app:app --reload
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Then:
|
|
22
|
+
|
|
23
|
+
```sh
|
|
24
|
+
curl -X POST localhost:8000/todos -H 'content-type: application/json' -d '{"title": "try hayate"}'
|
|
25
|
+
curl localhost:8000/todos
|
|
26
|
+
```
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"""$project_name: a TODO API on hayate.
|
|
2
|
+
|
|
3
|
+
Request/Response follow WHATWG Fetch semantics, so this app is a pure
|
|
4
|
+
``fetch(Request) -> Response`` core: any ASGI server runs it, and the same
|
|
5
|
+
file works on Cloudflare Python Workers unchanged.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from hayate import Context, Hayate, HTTPException
|
|
9
|
+
|
|
10
|
+
app = Hayate()
|
|
11
|
+
|
|
12
|
+
TODOS: dict[str, dict] = {}
|
|
13
|
+
_serial = 0
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def _next_id() -> str:
|
|
17
|
+
global _serial
|
|
18
|
+
_serial += 1
|
|
19
|
+
return str(_serial)
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
@app.get("/todos")
|
|
23
|
+
async def list_todos(c: Context):
|
|
24
|
+
return c.json(list(TODOS.values()))
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
@app.post("/todos")
|
|
28
|
+
async def create_todo(c: Context):
|
|
29
|
+
data = await c.req.json()
|
|
30
|
+
if not isinstance(data, dict) or not isinstance(data.get("title"), str):
|
|
31
|
+
raise HTTPException(400, title="Body must be a JSON object with a string 'title'")
|
|
32
|
+
todo = {"id": _next_id(), "title": data["title"], "done": False}
|
|
33
|
+
TODOS[todo["id"]] = todo
|
|
34
|
+
return c.json(todo, status=201)
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
@app.get("/todos/:id")
|
|
38
|
+
async def show_todo(c: Context):
|
|
39
|
+
todo = TODOS.get(c.req.param("id"))
|
|
40
|
+
if todo is None:
|
|
41
|
+
raise HTTPException(404, title="Todo not found")
|
|
42
|
+
return c.json(todo)
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
@app.delete("/todos/:id")
|
|
46
|
+
async def delete_todo(c: Context):
|
|
47
|
+
if TODOS.pop(c.req.param("id"), None) is None:
|
|
48
|
+
raise HTTPException(404, title="Todo not found")
|
|
49
|
+
return c.body(None, status=204)
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "$project_name"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
requires-python = ">=3.12"
|
|
5
|
+
dependencies = ["hayate>=0.6.0"]
|
|
6
|
+
|
|
7
|
+
[dependency-groups]
|
|
8
|
+
dev = [
|
|
9
|
+
"pytest>=8.3",
|
|
10
|
+
"pytest-asyncio>=0.25",
|
|
11
|
+
"uvicorn>=0.30",
|
|
12
|
+
]
|
|
13
|
+
|
|
14
|
+
[tool.pytest.ini_options]
|
|
15
|
+
asyncio_mode = "auto"
|
|
16
|
+
testpaths = ["tests"]
|
|
17
|
+
pythonpath = ["."]
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"""Tests hit the app core directly: no server, no test client, no sockets."""
|
|
2
|
+
|
|
3
|
+
from app import app
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
async def test_crud_roundtrip():
|
|
7
|
+
res = await app.request("/todos", method="POST", json={"title": "ship it"})
|
|
8
|
+
assert res.status == 201
|
|
9
|
+
todo = await res.json()
|
|
10
|
+
assert todo["title"] == "ship it"
|
|
11
|
+
assert todo["done"] is False
|
|
12
|
+
|
|
13
|
+
res = await app.request(f"/todos/{todo['id']}")
|
|
14
|
+
assert res.status == 200
|
|
15
|
+
|
|
16
|
+
res = await app.request("/todos")
|
|
17
|
+
assert any(t["id"] == todo["id"] for t in await res.json())
|
|
18
|
+
|
|
19
|
+
res = await app.request(f"/todos/{todo['id']}", method="DELETE")
|
|
20
|
+
assert res.status == 204
|
|
21
|
+
|
|
22
|
+
res = await app.request(f"/todos/{todo['id']}")
|
|
23
|
+
assert res.status == 404
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
async def test_create_requires_string_title():
|
|
27
|
+
res = await app.request("/todos", method="POST", json={"title": 42})
|
|
28
|
+
assert res.status == 400
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# $project_name
|
|
2
|
+
|
|
3
|
+
A TODO API built with [hayate](https://github.com/hayatepy/hayate), deployed
|
|
4
|
+
to [Cloudflare Python Workers](https://developers.cloudflare.com/workers/languages/python/).
|
|
5
|
+
The app core is runtime-agnostic — the same `app.py` runs on any ASGI server too.
|
|
6
|
+
|
|
7
|
+
## Test
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
uv run pytest
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Tests call the app core directly (`await app.request(...)`); no server or
|
|
14
|
+
workerd needed.
|
|
15
|
+
|
|
16
|
+
## Develop locally
|
|
17
|
+
|
|
18
|
+
```sh
|
|
19
|
+
uv run pywrangler dev
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
Then:
|
|
23
|
+
|
|
24
|
+
```sh
|
|
25
|
+
curl -X POST localhost:8787/todos -H 'content-type: application/json' -d '{"title": "try hayate"}'
|
|
26
|
+
curl localhost:8787/todos
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Deploy
|
|
30
|
+
|
|
31
|
+
```sh
|
|
32
|
+
uv run pywrangler deploy
|
|
33
|
+
```
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"""$project_name: a TODO API on hayate.
|
|
2
|
+
|
|
3
|
+
Request/Response follow WHATWG Fetch semantics, so this app is a pure
|
|
4
|
+
``fetch(Request) -> Response`` core: any ASGI server runs it, and the same
|
|
5
|
+
file works on Cloudflare Python Workers unchanged.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from hayate import Context, Hayate, HTTPException
|
|
9
|
+
|
|
10
|
+
app = Hayate()
|
|
11
|
+
|
|
12
|
+
TODOS: dict[str, dict] = {}
|
|
13
|
+
_serial = 0
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def _next_id() -> str:
|
|
17
|
+
global _serial
|
|
18
|
+
_serial += 1
|
|
19
|
+
return str(_serial)
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
@app.get("/todos")
|
|
23
|
+
async def list_todos(c: Context):
|
|
24
|
+
return c.json(list(TODOS.values()))
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
@app.post("/todos")
|
|
28
|
+
async def create_todo(c: Context):
|
|
29
|
+
data = await c.req.json()
|
|
30
|
+
if not isinstance(data, dict) or not isinstance(data.get("title"), str):
|
|
31
|
+
raise HTTPException(400, title="Body must be a JSON object with a string 'title'")
|
|
32
|
+
todo = {"id": _next_id(), "title": data["title"], "done": False}
|
|
33
|
+
TODOS[todo["id"]] = todo
|
|
34
|
+
return c.json(todo, status=201)
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
@app.get("/todos/:id")
|
|
38
|
+
async def show_todo(c: Context):
|
|
39
|
+
todo = TODOS.get(c.req.param("id"))
|
|
40
|
+
if todo is None:
|
|
41
|
+
raise HTTPException(404, title="Todo not found")
|
|
42
|
+
return c.json(todo)
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
@app.delete("/todos/:id")
|
|
46
|
+
async def delete_todo(c: Context):
|
|
47
|
+
if TODOS.pop(c.req.param("id"), None) is None:
|
|
48
|
+
raise HTTPException(404, title="Todo not found")
|
|
49
|
+
return c.body(None, status=204)
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "$project_name"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
requires-python = ">=3.12"
|
|
5
|
+
dependencies = ["hayate>=0.6.0"]
|
|
6
|
+
|
|
7
|
+
[dependency-groups]
|
|
8
|
+
dev = [
|
|
9
|
+
"pytest>=8.3",
|
|
10
|
+
"pytest-asyncio>=0.25",
|
|
11
|
+
"workers-py",
|
|
12
|
+
]
|
|
13
|
+
|
|
14
|
+
[tool.pytest.ini_options]
|
|
15
|
+
asyncio_mode = "auto"
|
|
16
|
+
testpaths = ["tests"]
|
|
17
|
+
pythonpath = ["."]
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"""Tests hit the app core directly: no server, no test client, no sockets."""
|
|
2
|
+
|
|
3
|
+
from app import app
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
async def test_crud_roundtrip():
|
|
7
|
+
res = await app.request("/todos", method="POST", json={"title": "ship it"})
|
|
8
|
+
assert res.status == 201
|
|
9
|
+
todo = await res.json()
|
|
10
|
+
assert todo["title"] == "ship it"
|
|
11
|
+
assert todo["done"] is False
|
|
12
|
+
|
|
13
|
+
res = await app.request(f"/todos/{todo['id']}")
|
|
14
|
+
assert res.status == 200
|
|
15
|
+
|
|
16
|
+
res = await app.request("/todos")
|
|
17
|
+
assert any(t["id"] == todo["id"] for t in await res.json())
|
|
18
|
+
|
|
19
|
+
res = await app.request(f"/todos/{todo['id']}", method="DELETE")
|
|
20
|
+
assert res.status == 204
|
|
21
|
+
|
|
22
|
+
res = await app.request(f"/todos/{todo['id']}")
|
|
23
|
+
assert res.status == 404
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
async def test_create_requires_string_title():
|
|
27
|
+
res = await app.request("/todos", method="POST", json={"title": 42})
|
|
28
|
+
assert res.status == 400
|