From 3d44ce6e3df12ac0a3ee82e9e5dc9a266502f599 Mon Sep 17 00:00:00 2001 From: cabbagec Date: Wed, 18 Mar 2026 02:40:02 +0800 Subject: [PATCH] change deployment to use docker-compose --- Dockerfile | 15 --------------- docker-compose.yml | 10 ++++++---- nginx.conf | 47 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 19 deletions(-) delete mode 100644 Dockerfile create mode 100644 nginx.conf diff --git a/Dockerfile b/Dockerfile deleted file mode 100644 index 9cdc994..0000000 --- a/Dockerfile +++ /dev/null @@ -1,15 +0,0 @@ -FROM python:3.11-slim - -WORKDIR /app - -# Copy application files -COPY urlshort.py ./ -COPY config.json ./ - -# Data directory is mounted as a volume; pre-create it for local runs -RUN mkdir -p /app/data - -EXPOSE 8080 - -CMD ["python", "urlshort.py", "config.json"] - diff --git a/docker-compose.yml b/docker-compose.yml index 2952087..e523ef7 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,12 +1,14 @@ services: urlshort: - build: - context: . - dockerfile: Dockerfile + image: python:3.11-slim + working_dir: /app + command: ["python", "-u", "urlshort.py", "config.json"] ports: - "8080:8080" volumes: - # Persist the SQLite database outside the container + - ./urlshort.py:/app/urlshort.py:ro + - ./config.json:/app/config.json:ro + - ./static:/app/static:ro - ./data:/app/data restart: unless-stopped diff --git a/nginx.conf b/nginx.conf new file mode 100644 index 0000000..2d7a98b --- /dev/null +++ b/nginx.conf @@ -0,0 +1,47 @@ +# =========================================================================== +# URL Shortener — nginx location blocks +# Drop these into an existing server { } block. +# +# Assumptions: +# - The Python backend is reachable at http://urlshort:8080 +# (docker service name; swap for 127.0.0.1:8080 or upstream as needed) +# - Static frontend files live at /app/static/ +# (adjust the alias paths to match your deployment layout) +# +# Path mapping (base_url = http:///s): +# /s/ -> frontend index.html +# /s/static/... -> frontend CSS / JS +# /s/api/... -> Python backend (proxy) +# /s/ -> Python backend (redirect, proxy) +# =========================================================================== + +# --- Redirect bare /s to /s/ for clean UX --- +location = /s { + return 301 /s/; +} + +# --- Serve frontend index.html at /s/ --- +location = /s/ { + alias /app/static/index.html; +} + +# --- Serve frontend static assets (CSS, JS, etc.) --- +# ^~ ensures this takes priority over the general /s/ proxy below. +location ^~ /s/static/ { + alias /app/static/; + expires 7d; + add_header Cache-Control "public, immutable"; +} + +# --- Proxy everything else under /s/ to the Python backend --- +# Covers: /s/api/shorten, /s/api/urls, /s/api/lookup, /s/ redirects +location /s/ { + proxy_pass http://urlshort:8080; + proxy_http_version 1.1; + + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; +} +