Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

python setup using fastapi #1

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,8 @@ cython_debug/

composer.phar
src/php/vendor/


__pycache__
cache
venv
39 changes: 39 additions & 0 deletions src/python/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import pathlib
import qrcode

from datetime import datetime
from hashlib import md5

from fastapi import FastAPI
from fastapi.responses import FileResponse


app = FastAPI()


@app.get("/")
def generate_qr(chl: str):
date = datetime.now().strftime("%Y/%m/%d")
chl_hash = md5(chl.encode()).hexdigest()

cache_path = pathlib.Path("cache") / date
cache_path.mkdir(parents=True, exist_ok=True)

file_name = f"{chl_hash}.png"
headers = {"X-QRR-Hash": chl_hash, "X-QRR-Cache": "HIT"}

img_path = cache_path / file_name

if not img_path.exists():
headers["X-QRR-Cache"] = "MISS"

qr = qrcode.QRCode(
box_size=10,
border=4,
)
qr.add_data(chl)
qr.make(fit=True)
img = qr.make_image(back_color="transparent")
img.save(str(img_path))

return FileResponse(path=img_path, media_type="image/png", headers=headers)
39 changes: 39 additions & 0 deletions src/python/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
annotated-types==0.7.0
anyio==4.4.0
certifi==2024.6.2
click==8.1.7
dnspython==2.6.1
email_validator==2.2.0
exceptiongroup==1.2.1
fastapi==0.111.0
fastapi-cli==0.0.4
h11==0.14.0
httpcore==1.0.5
httptools==0.6.1
httpx==0.27.0
idna==3.7
Jinja2==3.1.4
markdown-it-py==3.0.0
MarkupSafe==2.1.5
mdurl==0.1.2
orjson==3.10.5
pillow==10.3.0
pydantic==2.7.4
pydantic_core==2.18.4
Pygments==2.18.0
pypng==0.20220715.0
python-dotenv==1.0.1
python-multipart==0.0.9
PyYAML==6.0.1
qrcode==7.4.2
rich==13.7.1
shellingham==1.5.4
sniffio==1.3.1
starlette==0.37.2
typer==0.12.3
typing_extensions==4.12.2
ujson==5.10.0
uvicorn==0.30.1
uvloop==0.19.0
watchfiles==0.22.0
websockets==12.0