Skip to content

Commit 0848a62

Browse files
committed
refactor: main.py
1 parent 4873407 commit 0848a62

File tree

3 files changed

+75
-69
lines changed

3 files changed

+75
-69
lines changed

backend/app/main.py

Lines changed: 3 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,10 @@
1-
from pathlib import Path
2-
3-
from fastapi import FastAPI, Request
4-
from fastapi.responses import RedirectResponse
1+
from fastapi import FastAPI
52
from fastapi.routing import APIRoute
6-
from fastapi.templating import Jinja2Templates
7-
from sqlmodel import Session
83
from starlette.middleware.cors import CORSMiddleware
94

105
from app.api.router import api_router
116
from app.core.config import settings
12-
from app.core.db import engine
13-
from app.modules.shortener.service import ShortUrlService
14-
15-
16-
# Setup Jinja2 templates
17-
templates_dir = Path(__file__).parent / "templates"
18-
templates = Jinja2Templates(directory=str(templates_dir))
7+
from app.modules.shortener.redirect import redirect_router
198

209

2110
def custom_generate_unique_id(route: APIRoute) -> str:
@@ -39,58 +28,4 @@ def custom_generate_unique_id(route: APIRoute) -> str:
3928
)
4029

4130
app.include_router(api_router, prefix=settings.API_V1_STR)
42-
43-
44-
@app.get("/s/{short_code}", tags=["redirect"], response_model=None)
45-
def redirect_to_url(request: Request, short_code: str):
46-
"""
47-
Redirect to the original URL based on the short code.
48-
This endpoint is public and doesn't require authentication.
49-
"""
50-
# Cache control headers to prevent browser caching
51-
no_cache_headers = {
52-
"Cache-Control": "no-store, no-cache, must-revalidate, max-age=0",
53-
"Pragma": "no-cache",
54-
"Expires": "0",
55-
}
56-
57-
with Session(engine) as session:
58-
service = ShortUrlService(session)
59-
short_url = service.get_short_url_by_code(short_code=short_code)
60-
61-
if not short_url:
62-
return templates.TemplateResponse(
63-
request=request,
64-
name="error.html",
65-
context={
66-
"title": "Link Not Found",
67-
"message": "The short URL you're looking for doesn't exist. It may have been deleted or never created.",
68-
"status_code": 404,
69-
"homepage_url": settings.FRONTEND_HOST,
70-
},
71-
status_code=404,
72-
headers=no_cache_headers,
73-
)
74-
75-
if not short_url.is_active:
76-
return templates.TemplateResponse(
77-
request=request,
78-
name="error.html",
79-
context={
80-
"title": "Link Deactivated",
81-
"message": "This short URL has been deactivated by its owner and is no longer available.",
82-
"status_code": 410,
83-
"homepage_url": settings.FRONTEND_HOST,
84-
},
85-
status_code=410,
86-
headers=no_cache_headers,
87-
)
88-
89-
# Increment click count
90-
service.increment_click_count(short_url=short_url)
91-
92-
return RedirectResponse(
93-
url=short_url.original_url,
94-
status_code=307,
95-
headers=no_cache_headers,
96-
)
31+
app.include_router(redirect_router)
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
from pathlib import Path
2+
3+
from fastapi import APIRouter, Request
4+
from fastapi.responses import RedirectResponse
5+
from fastapi.templating import Jinja2Templates
6+
from sqlmodel import Session
7+
8+
from app.core.config import settings
9+
from app.core.db import engine
10+
from app.modules.shortener.service import ShortUrlService
11+
12+
# Setup Jinja2 templates
13+
templates_dir = Path(__file__).parent.parent.parent / "templates"
14+
templates = Jinja2Templates(directory=str(templates_dir))
15+
16+
redirect_router = APIRouter(tags=["redirect"])
17+
18+
19+
@redirect_router.get("/s/{short_code}", response_model=None)
20+
def redirect_to_url(request: Request, short_code: str):
21+
"""
22+
Redirect to the original URL based on the short code.
23+
This endpoint is public and doesn't require authentication.
24+
"""
25+
# Cache control headers to prevent browser caching
26+
no_cache_headers = {
27+
"Cache-Control": "no-store, no-cache, must-revalidate, max-age=0",
28+
"Pragma": "no-cache",
29+
"Expires": "0",
30+
}
31+
32+
with Session(engine) as session:
33+
service = ShortUrlService(session)
34+
short_url = service.get_short_url_by_code(short_code=short_code)
35+
36+
if not short_url:
37+
return templates.TemplateResponse(
38+
request=request,
39+
name="error.html",
40+
context={
41+
"title": "Link Not Found",
42+
"message": "The short URL you're looking for doesn't exist. It may have been deleted or never created.",
43+
"status_code": 404,
44+
"homepage_url": settings.FRONTEND_HOST,
45+
},
46+
status_code=404,
47+
headers=no_cache_headers,
48+
)
49+
50+
if not short_url.is_active:
51+
return templates.TemplateResponse(
52+
request=request,
53+
name="error.html",
54+
context={
55+
"title": "Link Deactivated",
56+
"message": "This short URL has been deactivated by its owner and is no longer available.",
57+
"status_code": 410,
58+
"homepage_url": settings.FRONTEND_HOST,
59+
},
60+
status_code=410,
61+
headers=no_cache_headers,
62+
)
63+
64+
# Increment click count
65+
service.increment_click_count(short_url=short_url)
66+
67+
return RedirectResponse(
68+
url=short_url.original_url,
69+
status_code=307,
70+
headers=no_cache_headers,
71+
)

docker-compose.override.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ services:
9595
frontend:
9696
restart: "no"
9797
ports:
98-
- "5173:80"
98+
- "3000:80"
9999
build:
100100
context: ./frontend
101101
args:

0 commit comments

Comments
 (0)