From c67746ea899cd2cf0a054950219c7a0ccfb0c43e Mon Sep 17 00:00:00 2001 From: Kumi Date: Thu, 26 Sep 2024 07:44:10 +0200 Subject: [PATCH] feat: enhance deployment and proxy support - Updated README to include detailed production deployment steps using uWSGI and Caddy for better performance and scalability. - Added setup instructions for local development. - Integrated ProxyFix middleware in the app to handle headers from reverse proxies, improving compatibility with various proxy setups. Addresses better production readiness and proxy configuration. --- README.md | 68 ++++++++++++++++++++++++++++++++++++++++++++++++ src/small/app.py | 8 ++++++ 2 files changed, 76 insertions(+) diff --git a/README.md b/README.md index abe85ef..5f0e8a0 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,8 @@ Small is an alternative frontend for Medium articles, built with Flask. It allow ## Usage +### Local / Development + 1. Start the Flask development server: ``` small @@ -45,6 +47,72 @@ Small is an alternative frontend for Medium articles, built with Flask. It allow - Original URL: `https://medium.com/@username/article-title-123abc` - Small URL: `http://localhost:5000/@username/article-title-123abc` +### Production + +For production use, it is recommended to deploy Small using a WSGI server like uWSGI, and behind a reverse proxy like Caddy. + +This is a basic guide to deploy Small using uWSGI and Caddy. + +1. Clone the repository: + ``` + git clone https://git.private.coffee/PrivateCoffee/small.git + cd small + ``` + +2. Create a virtual environment and activate it: + ``` + python -m venv venv + source venv/bin/activate + ``` + +3. Install the package: + ``` + pip install . + ``` + +4. Install uWSGI: + ``` + pip install uwsgi + ``` + +5. Create a `small.ini` file with the following content (adjust as needed): + ``` + [uwsgi] + module = small.app:app + + uid = small + gid = small + master = true + processes = 5 + + plugins = python3 + virtualenv = /srv/small/venv/ + chdir = /srv/small/ + + http-socket = /tmp/small.sock + chown-socket = caddy + ``` + +6. Start the uWSGI server (consider using a process manager like `systemd`): + ``` + uwsgi --ini small.ini + ``` + +7. Configure Caddy to reverse proxy requests to the uWSGI server: + ``` + small.example.com { + reverse_proxy unix//tmp/small.sock + } + ``` + +#### Proxy Fix + +If you are using a reverse proxy like Nginx, and it is setting the `X-Forwarded-Host` header instead of passing the `Host` header, you can use the `ProxyFix` middleware to fix the issue. To enable it, simply set the `PROXY_FIX` environment variable to `1`. + +For uWSGI, you can add the following line to the `small.ini` file: +``` +env = PROXY_FIX=1 +``` ## Contributing diff --git a/src/small/app.py b/src/small/app.py index 8ddaab8..6debce5 100644 --- a/src/small/app.py +++ b/src/small/app.py @@ -1,4 +1,5 @@ from flask import Flask +from werkzeug.middleware.proxy_fix import ProxyFix from os import environ @@ -14,6 +15,13 @@ app.register_blueprint(proxy.bp) def main(): port = int(environ.get("PORT", 8115)) + + if int(environ.get("DEBUG", 0)): + app.debug = True + + if int(environ.get("PROXY_FIX", 0)): + app.wsgi_app = ProxyFix(app.wsgi_app) + app.run(port=port)