Implement dynamic service rendering with Flask

Refactored the website to serve dynamic content using Flask, replacing static HTML pages. This allows for the centralized management of service data through a JSON file. Optimizations include:

- Added a .gitignore file to exclude Python and Flask-specific temporary files.
- Migrated static assets into an organized directory structure to facilitate Flask's static file serving.
- Removed redundant HTML files and created Flask template versions with dynamic content rendering.
- Introduced Caddy server configuration for the new Flask architecture, including headers for security and CORS policy, and reverse proxy settings for route handling.

With these changes, website maintenance and updates are simplified, allowing for service information to be updated in a single location (`services.json`), which then propagates to the user-facing pages automatically.
This commit is contained in:
Kumi 2023-12-31 13:59:13 +01:00
parent 016f03b06b
commit 193546fcde
Signed by: kumi
GPG key ID: ECBCC9082395383F
19 changed files with 508 additions and 413 deletions

23
main.py Normal file
View file

@ -0,0 +1,23 @@
from flask import Flask, render_template, send_from_directory
from jinja2 import TemplateNotFound
import json
import pathlib
app = Flask(__name__)
@app.route('/assets/<path:path>')
def send_assets(path):
return send_from_directory('assets', path)
@app.route('/', defaults={'path': 'index'})
@app.route('/<path:path>.html')
def catch_all(path):
try:
services = json.loads((pathlib.Path(__file__).parent / "services.json").read_text())
return render_template(f'{path}.html', services=services)
except TemplateNotFound:
return "404 Not Found", 404
if __name__ == '__main__':
app.run(port=9810)