Skip to content

Commit d0e4ef7

Browse files
committed
Add Python worker
1 parent afeb27d commit d0e4ef7

File tree

9 files changed

+490
-0
lines changed

9 files changed

+490
-0
lines changed

.github/workflows/ci.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches: [main]
6+
pull_request:
7+
branches: [main]
8+
9+
jobs:
10+
lint:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v4
14+
- uses: astral-sh/ruff-action@v3

.gitignore

Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
# Pyodide
2+
.venv-pyodide
3+
src/vendor
4+
5+
# Byte-compiled / optimized / DLL files
6+
__pycache__/
7+
*.py[codz]
8+
*$py.class
9+
10+
# C extensions
11+
*.so
12+
13+
# Distribution / packaging
14+
.Python
15+
build/
16+
develop-eggs/
17+
dist/
18+
downloads/
19+
eggs/
20+
.eggs/
21+
lib/
22+
lib64/
23+
parts/
24+
sdist/
25+
var/
26+
wheels/
27+
share/python-wheels/
28+
*.egg-info/
29+
.installed.cfg
30+
*.egg
31+
MANIFEST
32+
33+
# PyInstaller
34+
# Usually these files are written by a python script from a template
35+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
36+
*.manifest
37+
*.spec
38+
39+
# Installer logs
40+
pip-log.txt
41+
pip-delete-this-directory.txt
42+
43+
# Unit test / coverage reports
44+
htmlcov/
45+
.tox/
46+
.nox/
47+
.coverage
48+
.coverage.*
49+
.cache
50+
nosetests.xml
51+
coverage.xml
52+
*.cover
53+
*.py.cover
54+
.hypothesis/
55+
.pytest_cache/
56+
cover/
57+
58+
# Translations
59+
*.mo
60+
*.pot
61+
62+
# Django stuff:
63+
*.log
64+
local_settings.py
65+
db.sqlite3
66+
db.sqlite3-journal
67+
68+
# Flask stuff:
69+
instance/
70+
.webassets-cache
71+
72+
# Scrapy stuff:
73+
.scrapy
74+
75+
# Sphinx documentation
76+
docs/_build/
77+
78+
# PyBuilder
79+
.pybuilder/
80+
target/
81+
82+
# Jupyter Notebook
83+
.ipynb_checkpoints
84+
85+
# IPython
86+
profile_default/
87+
ipython_config.py
88+
89+
# pyenv
90+
# For a library or package, you might want to ignore these files since the code is
91+
# intended to run in multiple environments; otherwise, check them in:
92+
# .python-version
93+
94+
# pipenv
95+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
96+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
97+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
98+
# install all needed dependencies.
99+
#Pipfile.lock
100+
101+
# UV
102+
# Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
103+
# This is especially recommended for binary packages to ensure reproducibility, and is more
104+
# commonly ignored for libraries.
105+
#uv.lock
106+
107+
# poetry
108+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
109+
# This is especially recommended for binary packages to ensure reproducibility, and is more
110+
# commonly ignored for libraries.
111+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
112+
#poetry.lock
113+
#poetry.toml
114+
115+
# pdm
116+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
117+
# pdm recommends including project-wide configuration in pdm.toml, but excluding .pdm-python.
118+
# https://pdm-project.org/en/latest/usage/project/#working-with-version-control
119+
#pdm.lock
120+
#pdm.toml
121+
.pdm-python
122+
.pdm-build/
123+
124+
# pixi
125+
# Similar to Pipfile.lock, it is generally recommended to include pixi.lock in version control.
126+
#pixi.lock
127+
# Pixi creates a virtual environment in the .pixi directory, just like venv module creates one
128+
# in the .venv directory. It is recommended not to include this directory in version control.
129+
.pixi
130+
131+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
132+
__pypackages__/
133+
134+
# Celery stuff
135+
celerybeat-schedule
136+
celerybeat.pid
137+
138+
# SageMath parsed files
139+
*.sage.py
140+
141+
# Environments
142+
.env
143+
.envrc
144+
.venv
145+
env/
146+
venv/
147+
ENV/
148+
env.bak/
149+
venv.bak/
150+
151+
# Spyder project settings
152+
.spyderproject
153+
.spyproject
154+
155+
# Rope project settings
156+
.ropeproject
157+
158+
# mkdocs documentation
159+
/site
160+
161+
# mypy
162+
.mypy_cache/
163+
.dmypy.json
164+
dmypy.json
165+
166+
# Pyre type checker
167+
.pyre/
168+
169+
# pytype static type analyzer
170+
.pytype/
171+
172+
# Cython debug symbols
173+
cython_debug/
174+
175+
# PyCharm
176+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
177+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
178+
# and can be added to the global gitignore or merged into this file. For a more nuclear
179+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
180+
#.idea/
181+
182+
# Abstra
183+
# Abstra is an AI-powered process automation framework.
184+
# Ignore directories containing user credentials, local state, and settings.
185+
# Learn more at https://abstra.io/docs
186+
.abstra/
187+
188+
# Visual Studio Code
189+
# Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore
190+
# that can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore
191+
# and can be added to the global gitignore or merged into this file. However, if you prefer,
192+
# you could uncomment the following to ignore the entire vscode folder
193+
# .vscode/
194+
195+
# Ruff stuff:
196+
.ruff_cache/
197+
198+
# PyPI configuration file
199+
.pypirc
200+
201+
# Cursor
202+
# Cursor is an AI-powered code editor. `.cursorignore` specifies files/directories to
203+
# exclude from AI features like autocomplete and code analysis. Recommended for sensitive data
204+
# refer to https://docs.cursor.com/context/ignore-files
205+
.cursorignore
206+
.cursorindexingignore
207+
208+
# Marimo
209+
marimo/_static/
210+
marimo/_lsp/
211+
__marimo__/

.python-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3.12

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# uv-cloudflare-workers-example
2+
3+
[![Deploy to Cloudflare](https://deploy.workers.cloudflare.com/button)](https://deploy.workers.cloudflare.com/?url=https://github.com/astral-sh/uv-cloudflare-workers-example)
4+
5+
Deploying a FastAPI application to [Cloudflare Workers](https://developers.cloudflare.com/workers/) with [uv](https://docs.astral.sh/uv/).
6+
7+
## Usage
8+
9+
From the repository root, deploy to Cloudflare with:
10+
11+
```bash
12+
./build.sh
13+
npx wrangler deploy
14+
```
15+
16+
You can then navigate to the [Worker URL](https://uv-cloudflare-workers-example.astral-1ad.workers.dev)
17+
to see the "Hello from Python!" message, `/hello/{name}` to see a templatized response, and `/version` to see the
18+
version of `cffi` (an extension module containing native code).

build.sh

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/bin/bash
2+
set -euxo pipefail
3+
4+
PYODIDE_VERSION=0.27.7
5+
6+
# Clean up previous builds.
7+
echo "Cleaning up previous builds..."
8+
rm -rf ./src/vendor
9+
rm -rf .venv-pyodide
10+
11+
# Install pyodide CLI.
12+
uv tool install --with pyodide-build --with pip pyodide-cli --python 3.12
13+
14+
# Create the xbuild environment.
15+
pyodide xbuildenv install $PYODIDE_VERSION
16+
17+
# Create virtual environment.
18+
pyodide venv .venv-pyodide
19+
20+
# Install requirements.
21+
echo "Installing dependencies..."
22+
uv export --python .venv-pyodide --index https://pyodide.astral.sh/$PYODIDE_VERSION | uv pip sync \
23+
--no-installer-metadata \
24+
--no-compile-bytecode \
25+
--only-binary :all: \
26+
--target src/vendor \
27+
--python .venv-pyodide \
28+
--index https://pyodide.astral.sh/$PYODIDE_VERSION \
29+
-
30+
31+
echo "Build completed successfully!"

pyproject.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[project]
2+
name = "uv-cloudflare-workers-example"
3+
version = "0.0.1"
4+
readme = "README.md"
5+
requires-python = ">=3.12"
6+
dependencies = [
7+
"cffi",
8+
"fastapi",
9+
"jinja2",
10+
]

src/worker.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from fastapi import FastAPI
2+
3+
4+
async def on_fetch(request, env):
5+
import asgi
6+
7+
return await asgi.fetch(app, request, env)
8+
9+
10+
app = FastAPI()
11+
12+
13+
@app.get("/")
14+
async def root():
15+
return {"message": "Hello from Python!"}
16+
17+
18+
@app.get("/hello/{name}")
19+
async def hello(name: str):
20+
import jinja2
21+
22+
environment = jinja2.Environment()
23+
template = environment.from_string("Hello, {{ name }}!")
24+
message = template.render(name=name)
25+
return {"message": message}
26+
27+
28+
@app.get("/version")
29+
async def version():
30+
import cffi
31+
32+
return {"version": cffi.__version__}

0 commit comments

Comments
 (0)