From 44b47cceede53f7c9ce46a5288d4f2959c69172f Mon Sep 17 00:00:00 2001 From: Arjun Singh Date: Wed, 26 Jun 2024 18:24:46 +0545 Subject: [PATCH 1/2] python setup using fastapi --- .gitignore | 5 +++++ app.py | 39 +++++++++++++++++++++++++++++++++++++++ requirements.txt | 39 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 83 insertions(+) create mode 100644 app.py create mode 100644 requirements.txt diff --git a/.gitignore b/.gitignore index 6e91e09..e1cb073 100644 --- a/.gitignore +++ b/.gitignore @@ -161,3 +161,8 @@ cython_debug/ composer.phar src/php/vendor/ + + +__pycache__ +cache +venv diff --git a/app.py b/app.py new file mode 100644 index 0000000..d8aae8a --- /dev/null +++ b/app.py @@ -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) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..a6b9058 --- /dev/null +++ b/requirements.txt @@ -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 From 57a6ead1d3a14b601c86f0de7aa38f1833d09ba6 Mon Sep 17 00:00:00 2001 From: Arjun Singh Date: Wed, 26 Jun 2024 18:26:59 +0545 Subject: [PATCH 2/2] move codes to src --- app.py => src/python/app.py | 0 requirements.txt => src/python/requirements.txt | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename app.py => src/python/app.py (100%) rename requirements.txt => src/python/requirements.txt (100%) diff --git a/app.py b/src/python/app.py similarity index 100% rename from app.py rename to src/python/app.py diff --git a/requirements.txt b/src/python/requirements.txt similarity index 100% rename from requirements.txt rename to src/python/requirements.txt