rewrite URL shortener in Rust

This commit is contained in:
2026-07-10 04:46:34 +00:00
parent a250ce4c1d
commit a6e8be7553
16 changed files with 2906 additions and 1102 deletions
+25 -28
View File
@@ -1,43 +1,40 @@
# ===========================================================================
# 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://<host>/s):
# /s/ -> frontend index.html
# /s/static/... -> frontend CSS / JS
# /s/api/... -> Python backend (proxy)
# /s/<code> -> Python backend (redirect, proxy)
# ===========================================================================
# Drop these into the existing server {} block. The Rust binary serves both
# the API and its embedded, byte-identical frontend on 127.0.0.1:18082.
# --- Redirect bare /s to /s/ for clean UX ---
location = /s {
return 301 /s/;
}
# --- Serve frontend index.html at /s/ ---
# no-store prevents Cloudflare and browsers from caching the HTML entry point,
# so updated ?v= cache-busters on CSS/JS are always picked up immediately.
# Preserve the existing no-store policy for the HTML entry point.
location = /s/ {
root /root/repo/urlshortener/static;
try_files /index.html =404;
add_header Cache-Control "no-store";
proxy_pass http://127.0.0.1:18082;
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;
proxy_hide_header Cache-Control;
add_header Cache-Control "no-store";
}
# --- Serve frontend static assets (CSS, JS, etc.) ---
# ^~ ensures this takes priority over the general /s/ proxy below.
# Preserve the existing revalidation policy for CSS, JavaScript, and fonts.
location ^~ /s/static/ {
alias /root/repo/urlshortener/static/;
add_header Cache-Control "no-cache";
proxy_pass http://127.0.0.1:18082;
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;
proxy_hide_header Cache-Control;
add_header Cache-Control "no-cache";
}
# --- Proxy everything else under /s/ to the Python backend ---
# Covers: /s/api/shorten, /s/api/urls, /s/api/lookup, /s/<code> redirects
# API calls and short-code redirects.
location /s/ {
proxy_pass http://127.0.0.1:18082;
proxy_http_version 1.1;