From 04892822306bf9a01de19cc70bcfc59d7fca9f8f Mon Sep 17 00:00:00 2001 From: Dylan Bolger Date: Tue, 18 Aug 2026 11:44:36 -0500 Subject: move from fastapi to flask, various cleanups involved --- .dockerignore | 1 + .gitignore | 4 ++-- Dockerfile | 4 ++-- app/app.py | 6 ++++++ app/blueprints/__init__.py | 0 app/blueprints/web.py | 9 ++++++++ app/main.py | 24 ---------------------- app/routers/__init__.py | 0 app/routers/web.py | 9 -------- app/services/__pycache__/__init__.cpython-314.pyc | Bin 157 -> 0 bytes app/services/__pycache__/images.cpython-314.pyc | Bin 1250 -> 0 bytes app/services/images.py | 18 ++++++++-------- app/templates/image.html | 9 -------- app/templates/index.html | 6 +++--- requirements.txt | 3 ++- 15 files changed, 33 insertions(+), 60 deletions(-) create mode 100644 app/app.py create mode 100644 app/blueprints/__init__.py create mode 100644 app/blueprints/web.py delete mode 100644 app/main.py delete mode 100644 app/routers/__init__.py delete mode 100644 app/routers/web.py delete mode 100644 app/services/__pycache__/__init__.cpython-314.pyc delete mode 100644 app/services/__pycache__/images.cpython-314.pyc delete mode 100644 app/templates/image.html diff --git a/.dockerignore b/.dockerignore index ae9d5d1..feb9f1d 100644 --- a/.dockerignore +++ b/.dockerignore @@ -7,6 +7,7 @@ **/.pytest_cache **/.env **/.venv +**/photos venv/ ENV/ .aws/ diff --git a/.gitignore b/.gitignore index bdaf325..b81db3b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ .vscode/ photos/ -photoblog.db -/*.html \ No newline at end of file +/*.html +**/__pycache__/** \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index e135ca9..a49444b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -8,6 +8,6 @@ RUN pip install -r requirements.txt COPY . . -EXPOSE 80 +EXPOSE 8000 -CMD ["fastapi", "run", "app/main.py", "--port", "80"] \ No newline at end of file +CMD ["gunicorn", "-w", "4", "-b", "0.0.0.0", "app.app:app"] \ No newline at end of file diff --git a/app/app.py b/app/app.py new file mode 100644 index 0000000..8f2896c --- /dev/null +++ b/app/app.py @@ -0,0 +1,6 @@ +from flask import Flask + +from .blueprints import web + +app = Flask(__name__) +app.register_blueprint(web.web_controller) \ No newline at end of file diff --git a/app/blueprints/__init__.py b/app/blueprints/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/app/blueprints/web.py b/app/blueprints/web.py new file mode 100644 index 0000000..f4e2949 --- /dev/null +++ b/app/blueprints/web.py @@ -0,0 +1,9 @@ +from flask import Blueprint, render_template, current_app +from ..services import images + +web_controller = Blueprint('web', __name__, template_folder='templates') + +@web_controller.route('/') +def main_page(): + imgs = images.get_all_images(current_app) + return render_template('index.html', images=imgs) \ No newline at end of file diff --git a/app/main.py b/app/main.py deleted file mode 100644 index b6adaad..0000000 --- a/app/main.py +++ /dev/null @@ -1,24 +0,0 @@ -from fastapi import FastAPI -from fastapi.staticfiles import StaticFiles -from fastapi.templating import Jinja2Templates -from fastapi.middleware.cors import CORSMiddleware -from .routers import web -from .services import images - -app = FastAPI( - docs_url=None, - redoc_url=None, - openapi_url=None - ) - -origins = ["https://dbolger.dev"] - -app.add_middleware(CORSMiddleware, allow_origins=origins, allow_credentials=True, - allow_methods=["*"], - allow_headers=["*"]) - -app.state.image_service = images -app.state.templates = Jinja2Templates(directory="./app/templates") -app.include_router(web.router) -app.mount("/static", StaticFiles(directory="./app/static"), name="static") -app.mount("/photos", StaticFiles(directory=images.photos_dir), name="photos") \ No newline at end of file diff --git a/app/routers/__init__.py b/app/routers/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/app/routers/web.py b/app/routers/web.py deleted file mode 100644 index 678c63e..0000000 --- a/app/routers/web.py +++ /dev/null @@ -1,9 +0,0 @@ -from fastapi import APIRouter, Request - -router = APIRouter() - -@router.get("/") -async def main_page(request: Request): - images = request.app.state.image_service.get_all_images() - return request.app.state.templates.TemplateResponse( - request=request, name="index.html", context={"images": images}) \ No newline at end of file diff --git a/app/services/__pycache__/__init__.cpython-314.pyc b/app/services/__pycache__/__init__.cpython-314.pyc deleted file mode 100644 index b31d35f..0000000 Binary files a/app/services/__pycache__/__init__.cpython-314.pyc and /dev/null differ diff --git a/app/services/__pycache__/images.cpython-314.pyc b/app/services/__pycache__/images.cpython-314.pyc deleted file mode 100644 index 2514767..0000000 Binary files a/app/services/__pycache__/images.cpython-314.pyc and /dev/null differ diff --git a/app/services/images.py b/app/services/images.py index 3ce8045..4938014 100644 --- a/app/services/images.py +++ b/app/services/images.py @@ -1,21 +1,19 @@ from pathlib import Path +from flask import Flask, current_app import random -print(str(Path.cwd())) -root_dir = Path.cwd() -photos_dir = Path(f"{root_dir}photos") - image_cache = set() -def cache_images(): +def cache_images(app: Flask): + photos_dir = Path(f"{app.static_folder}/photos") for file_path in photos_dir.rglob("*"): if file_path.is_file(): - image_path = file_path.relative_to(root_dir).name - image_cache.add(str(image_path)) + img_path = file_path.relative_to(app.static_folder) + image_cache.add(str(img_path)) -def get_all_images(): +def get_all_images(current_app: Flask): # if not image_cache: - cache_images() - print(f"returning {len(image_cache)} images") + cache_images(current_app) + print(f"Returning {len(image_cache)} images") shuffled = random.sample(list(image_cache), k=len(image_cache)) return shuffled diff --git a/app/templates/image.html b/app/templates/image.html deleted file mode 100644 index c070faf..0000000 --- a/app/templates/image.html +++ /dev/null @@ -1,9 +0,0 @@ - - - Item Details - - - -

Item ID: {{ id }}

- - \ No newline at end of file diff --git a/app/templates/index.html b/app/templates/index.html index 991088a..a489c5e 100644 --- a/app/templates/index.html +++ b/app/templates/index.html @@ -6,7 +6,7 @@ - + Dylan's Photo Blog @@ -31,8 +31,8 @@
{% for img in images %} {% endfor %} diff --git a/requirements.txt b/requirements.txt index be08dc6..f163f4d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,2 @@ -fastapi[standard] \ No newline at end of file +flask +gunicorn \ No newline at end of file -- cgit v1.2.3