-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroutes.py
More file actions
381 lines (316 loc) Β· 11 KB
/
routes.py
File metadata and controls
381 lines (316 loc) Β· 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
import os
from datetime import datetime, timezone
from typing import Optional
from app.utils.cache import list_cache_clean, clear_cache
from fastapi import (
APIRouter,
Form,
Request,
status,
HTTPException,
BackgroundTasks,
Header,
Query,
)
from fastapi.responses import (
HTMLResponse,
PlainTextResponse,
RedirectResponse,
JSONResponse,
)
from fastapi.templating import Jinja2Templates
from pydantic import BaseModel, Field
from app import __version__
from app.utils import db
from app.utils.cache import (
get_from_cache,
get_recent_from_cache,
get_short_from_cache,
set_cache_pair,
increment_visit_cache,
url_cache,
remove_cache_key,
rev_cache,
)
from app.utils.config import (
DOMAIN,
MAX_RECENT_URLS,
CACHE_PURGE_TOKEN,
QR_DIR,
)
from app.utils.helper import (
generate_code,
sanitize_url,
is_valid_url,
authorize_url,
format_date,
)
from app.utils.qr import generate_qr_with_logo
templates = Jinja2Templates(directory="app/templates")
# Routers
ui_router = APIRouter()
api_router = APIRouter()
api_v1 = APIRouter(prefix=os.getenv("API_VERSION", "/api/v1"), tags=["v1"])
# ---------------- UI ROUTES ----------------
@ui_router.get("/", response_class=HTMLResponse)
async def index(request: Request):
session = request.session
new_short_url = session.pop("new_short_url", None)
qr_enabled = session.pop("qr_enabled", False)
original_url = session.pop("original_url", None)
short_code = session.pop("short_code", None)
info_message = session.pop("info_message", None)
error = session.pop("error", None)
qr_image = None
qr_data = None
if qr_enabled and new_short_url and short_code:
qr_data = new_short_url
qr_filename = f"{short_code}.png"
generate_qr_with_logo(qr_data, str(QR_DIR / qr_filename))
qr_image = f"/qr/{qr_filename}"
recent_urls = db.get_recent_urls(MAX_RECENT_URLS) or get_recent_from_cache(
MAX_RECENT_URLS
)
return templates.TemplateResponse(
"index.html",
{
"request": request,
"urls": recent_urls,
"new_short_url": new_short_url,
"qr_image": qr_image,
"qr_data": qr_data,
"qr_enabled": qr_enabled,
"original_url": original_url,
"error": error,
"info_message": info_message,
"db_available": db.get_collection() is not None,
},
)
@ui_router.post("/shorten", response_class=RedirectResponse)
async def create_short_url(
request: Request,
original_url: str = Form(""),
generate_qr: Optional[str] = Form(None),
qr_type: str = Form("short"),
):
session = request.session
original_url = sanitize_url(original_url) # sanitize the URL input
if not original_url or not is_valid_url(original_url): # validate the URL
session["error"] = "Please enter a valid URL."
session["original_url"] = original_url # preserve user input
return RedirectResponse("/", status_code=status.HTTP_303_SEE_OTHER)
if not authorize_url(
original_url
): # authorize the URL based on whitelist/blacklist
session["error"] = "This domain is not allowed."
session["original_url"] = original_url # preserve user input
return RedirectResponse("/", status_code=status.HTTP_303_SEE_OTHER)
short_code: Optional[str] = get_short_from_cache(original_url)
if not short_code and db.is_connected():
existing = db.find_by_original_url(original_url)
db_code = (existing.get("short_code") if existing else None) or (
existing.get("code") if existing else None
)
if isinstance(db_code, str):
short_code = db_code
set_cache_pair(short_code, original_url)
if not short_code:
short_code = generate_code()
set_cache_pair(short_code, original_url)
if db.is_connected():
db.insert_url(short_code, original_url)
session.update(
{
"new_short_url": f"{DOMAIN.rstrip('/')}/{short_code}",
"short_code": short_code,
"qr_enabled": bool(generate_qr),
"qr_type": qr_type,
"original_url": original_url,
}
)
return RedirectResponse("/", status_code=status.HTTP_303_SEE_OTHER)
@ui_router.get("/history", response_class=HTMLResponse)
async def recent_urls(request: Request):
recent_urls_list = db.get_recent_urls(MAX_RECENT_URLS) or get_recent_from_cache(
MAX_RECENT_URLS
)
return templates.TemplateResponse(
"recent.html",
{
"request": request,
"urls": recent_urls_list,
"format_date": format_date,
"db_available": db.get_collection() is not None,
"get_visit_count_from_cache": increment_visit_cache,
},
)
@ui_router.get("/cache/list")
def cache_list_ui():
return list_cache_clean()
@ui_router.delete("/cache/purge", response_class=PlainTextResponse)
def cache_purge_ui(x_cache_token: str = Header(..., alias="X-Cache-Token")):
"""
Force delete everything from cache (secured by header)
"""
if x_cache_token != CACHE_PURGE_TOKEN:
raise HTTPException(status_code=401, detail="Unauthorized")
if not url_cache and not rev_cache:
return "No URLs in cache"
clear_cache()
return "cleared ALL"
@ui_router.patch("/cache/remove")
def cache_remove_one_ui(
key: str = Query(..., description="short_code OR original_url"),
x_cache_token: str = Header(..., alias="X-Cache-Token"),
):
# π Header security
if x_cache_token != CACHE_PURGE_TOKEN:
raise HTTPException(status_code=401, detail="Unauthorized")
removed = remove_cache_key(key)
if not removed:
raise HTTPException(
status_code=404,
detail="Key not found in cache.",
)
return {
"status": "deleted",
}
@ui_router.get("/{short_code}")
def redirect_short_ui(short_code: str, background_tasks: BackgroundTasks):
cached_url = get_from_cache(short_code)
if cached_url:
if db.is_connected():
background_tasks.add_task(db.increment_visit, short_code)
else:
increment_visit_cache(short_code)
return RedirectResponse(cached_url)
if db.is_connected():
doc = db.increment_visit(short_code)
if doc and doc.get("original_url"):
set_cache_pair(short_code, doc["original_url"])
return RedirectResponse(doc["original_url"])
recent_cache = get_recent_from_cache(MAX_RECENT_URLS)
for item in recent_cache or []:
code = item.get("short_code") or item.get("code")
if code == short_code:
original_url = item.get("original_url")
if original_url:
set_cache_pair(short_code, original_url)
return RedirectResponse(original_url)
raise HTTPException(status_code=404, detail="Page not found")
@ui_router.delete("/history/{short_code}")
def delete_recent_api(short_code: str):
recent = get_recent_from_cache(MAX_RECENT_URLS) or []
removed_from_cache = False
for i, item in enumerate(recent):
code = item.get("short_code") or item.get("code")
if code == short_code:
recent.pop(i) # remove from cache
removed_from_cache = True
break
db_available = db.is_connected()
db_deleted = False
if db_available:
db_deleted = db.delete_by_short_code(short_code)
if not removed_from_cache and not db_deleted:
raise HTTPException(
status_code=404, detail=f"short_code '{short_code}' not found"
)
return {
"success": True,
"status": "deleted",
"short_code": short_code,
"db_deleted": db_deleted,
"db_available": db_available,
}
# ---------------- API ROUTES ----------------
@api_router.get("/", response_class=HTMLResponse, tags=["Home"])
async def read_root(_: Request):
return """
<html>
<head>
<title>π tiny API π</title>
<style>
body {
margin: 0;
height: 100vh;
display: flex;
align-items: center;
justify-content: center;
background: linear-gradient(180deg, #0b1220, #050b14);
font-family: "Poppins", system-ui, Arial, sans-serif;
color: #f8fafc;
}
.card {
background: rgba(255, 255, 255, 0.06);
backdrop-filter: blur(12px);
border-radius: 16px;
padding: 50px 40px;
text-align: center;
box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
max-width: 520px;
width: 90%;
}
h1 {
font-size: 2.8em;
margin-bottom: 12px;
background: linear-gradient(90deg, #5ab9ff, #4cb39f);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
}
p {
font-size: 1.1em;
color: #cbd5e1;
margin-bottom: 30px;
}
a {
display: inline-block;
padding: 14px 26px;
border-radius: 12px;
background: linear-gradient(90deg, #4cb39f, #5ab9ff);
color: #fff;
text-decoration: none;
font-weight: 700;
}
</style>
</head>
<body>
<div class="card">
<h1>π tiny API</h1>
<p>FastAPI backend for the Tiny URL shortener</p>
<a href="/docs">View API Documentation</a>
</div>
</body>
</html>
"""
@api_router.get("/version")
def api_version():
return {"version": __version__}
class ShortenRequest(BaseModel):
url: str = Field(..., examples=["https://abcdkbd.com"])
@api_v1.post("/shorten")
def shorten_api(payload: ShortenRequest):
original_url = sanitize_url(payload.url)
if not is_valid_url(original_url):
return JSONResponse(status_code=400, content={"error": "INVALID_URL"})
if not authorize_url(original_url):
return JSONResponse(status_code=400, content={"error": "DOMAIN_NOT_ALLOWED"})
short_code = get_short_from_cache(original_url)
if not short_code:
short_code = generate_code()
set_cache_pair(short_code, original_url)
if db.is_connected():
db.insert_url(short_code, original_url)
return {
"success": True,
"input_url": original_url,
"short_code": short_code,
"created_on": datetime.now(timezone.utc),
}
@api_router.get("/health")
def health():
return {
"db": db.get_connection_state(),
"cache_size": len(url_cache),
}
api_router.include_router(api_v1)