summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.dockerignore1
-rw-r--r--.gitignore4
-rw-r--r--Dockerfile4
-rw-r--r--app/app.py6
-rw-r--r--app/blueprints/__init__.py (renamed from app/routers/__init__.py)0
-rw-r--r--app/blueprints/web.py9
-rw-r--r--app/main.py24
-rw-r--r--app/routers/web.py9
-rw-r--r--app/services/__pycache__/__init__.cpython-314.pycbin157 -> 0 bytes
-rw-r--r--app/services/__pycache__/images.cpython-314.pycbin1250 -> 0 bytes
-rw-r--r--app/services/images.py18
-rw-r--r--app/templates/image.html9
-rw-r--r--app/templates/index.html6
-rw-r--r--requirements.txt3
14 files changed, 33 insertions, 60 deletions
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/routers/__init__.py b/app/blueprints/__init__.py
index e69de29..e69de29 100644
--- a/app/routers/__init__.py
+++ b/app/blueprints/__init__.py
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/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
--- a/app/services/__pycache__/__init__.cpython-314.pyc
+++ /dev/null
Binary files 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
--- a/app/services/__pycache__/images.cpython-314.pyc
+++ /dev/null
Binary files 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 @@
-<html>
-<head>
- <title>Item Details</title>
- <link href="{{ url_for('static', path='/styles.css') }}" rel="stylesheet">
-</head>
-<body>
- <h1><a href="{{ url_for('read_item', id=id) }}">Item ID: {{ id }}</a></h1>
-</body>
-</html> \ 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 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://dbolger.dev/assets/css/main.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/water.css@2/out/water.css">
- <link rel="stylesheet" href="{{ url_for('static', path='index.css') }}">
+ <link rel="stylesheet" href="{{ url_for('static', filename='index.css') }}">
<title>Dylan's Photo Blog</title>
</head>
@@ -31,8 +31,8 @@
<main class="gallery">
{% for img in images %}
<div class="gallery-item">
- <a href="{{ url_for('photos', path=img) }}">
- <img src="{{ url_for('photos', path=img) }}" alt="" loading="lazy">
+ <a href="{{ url_for('static', filename=img) }}">
+ <img src="{{ url_for('static', filename=img) }}" alt="" loading="lazy">
</a>
</div>
{% 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