-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
487 lines (397 loc) · 14.7 KB
/
main.py
File metadata and controls
487 lines (397 loc) · 14.7 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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
"""
FastAPI application for Comps.
Handles file uploads, comparison viewing, and database operations.
"""
# Standard library imports
import asyncio
import logging
import os
import random
from datetime import datetime, timedelta
from pathlib import Path
from fastapi import FastAPI, Form, HTTPException, Request, status
from fastapi.openapi.utils import get_openapi
from fastapi.responses import HTMLResponse, JSONResponse, RedirectResponse
from fastapi.security import APIKeyCookie
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from pydantic import BaseModel
import auth
from api.router import router as api_router
from database import (
delete_comparison,
get_comparison,
get_expired_comparisons,
get_user_comparisons,
init_db,
update_last_accessed,
)
from database_metrics import get_metrics
from db import backend_name, query_dicts
# Random name generator for comparisons
def generate_random_name():
"""
Generate a random, memorable name for a comparison when the user doesn't provide one.
Format: [Adjective] [Noun]
"""
adjectives = [
"Amazing",
"Brilliant",
"Curious",
"Dazzling",
"Elegant",
"Fantastic",
"Graceful",
"Harmonious",
"Incredible",
"Jubilant",
"Keen",
"Luminous",
"Majestic",
"Noble",
"Optimistic",
"Peaceful",
"Quaint",
"Radiant",
"Serene",
"Tranquil",
"Unique",
"Vibrant",
"Wonderful",
"Zealous",
]
nouns = [
"Aurora",
"Breeze",
"Cascade",
"Diamond",
"Echo",
"Fountain",
"Galaxy",
"Horizon",
"Island",
"Journey",
"Kaleidoscope",
"Lagoon",
"Mountain",
"Nebula",
"Ocean",
"Panorama",
"Quest",
"Rainbow",
"Sunset",
"Treasure",
"Universe",
"Valley",
"Waterfall",
"Zenith",
]
adjective = random.choice(adjectives)
noun = random.choice(nouns)
return f"{adjective} {noun}"
# Create FastAPI app with custom OpenAPI settings
app = FastAPI(
title="Comps API",
description="API for managing image comparisons",
version="1.0.0",
docs_url=None, # Disable default docs
redoc_url=None, # Disable default redoc
)
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
cookie_sec = APIKeyCookie(name="session")
# Store background tasks to prevent premature garbage collection
background_tasks = set()
# Get configuration from environment
DB_PATH = os.getenv("DB_PATH", "comparisons.db")
UPLOADS_PATH = os.getenv("UPLOADS_PATH", "uploads")
RETENTION_DAYS = int(os.getenv("RETENTION_DAYS", "7"))
# Ensure directories exist
Path(UPLOADS_PATH).mkdir(parents=True, exist_ok=True)
Path(os.path.dirname(DB_PATH)).mkdir(parents=True, exist_ok=True)
# Initialize database
init_db()
# Include API router
app.include_router(api_router)
# Mount static files and templates
app.mount("/static", StaticFiles(directory="static"), name="static")
app.mount("/uploads", StaticFiles(directory=UPLOADS_PATH), name="uploads")
templates = Jinja2Templates(directory="templates")
# Custom OpenAPI schema
def custom_openapi():
"""Generate a custom OpenAPI schema."""
if app.openapi_schema:
return app.openapi_schema
openapi_schema = get_openapi(
title="Comps API",
version="1.0.0",
description="API for creating and managing image comparisons",
routes=app.routes,
)
app.openapi_schema = openapi_schema
return app.openapi_schema
app.openapi = custom_openapi
# Custom Swagger UI route with dark mode
@app.get("/api/docs", include_in_schema=False)
async def custom_swagger_ui_html():
"""Serve the custom Swagger UI HTML page."""
return templates.TemplateResponse(
"swagger_ui.html", {"request": Request(scope={"type": "http"})}
)
# Serve OpenAPI schema
@app.get("/openapi.json", include_in_schema=False)
async def get_open_api_endpoint():
"""Serve the OpenAPI schema as a JSON response."""
return JSONResponse(app.openapi())
# Background task for cleanup
async def cleanup_old_comparisons():
"""
Periodically check for and delete comparisons that haven't been accessed
in more than RETENTION_DAYS days
"""
while True:
try:
logger.info("Starting cleanup of comparisons older than %s days", RETENTION_DAYS)
expired_ids = get_expired_comparisons(RETENTION_DAYS)
if expired_ids:
logger.info("Found %s expired comparisons to delete", len(expired_ids))
for comparison_id in expired_ids:
try:
logger.info("Deleting comparison %s", comparison_id)
delete_comparison(comparison_id, UPLOADS_PATH)
except OSError as e:
logger.error(
"Error deleting comparison %s: %s",
comparison_id,
str(e),
)
else:
logger.info("No expired comparisons found")
# Run once a day
await asyncio.sleep(86400) # 24 hours in seconds
except OSError as e:
logger.error("Error in cleanup task: %s", str(e))
# If there's an error, wait a bit before trying again
await asyncio.sleep(3600) # 1 hour in seconds
@app.on_event("startup")
async def start_cleanup_task():
"""Start the background cleanup task when the application starts"""
task = asyncio.create_task(cleanup_old_comparisons())
# Add task to the set of background tasks
background_tasks.add(task)
# Check if database migrations are complete
# Best-effort schema presence check
try:
query_dicts("SELECT 1 as ok")
logger.info("Database initialized successfully (%s)", backend_name())
except Exception as e:
logger.error("Error checking database state: %s", str(e))
@app.get("/health")
async def health_check():
"""Health check endpoint for monitoring"""
return {"status": "healthy"}
@app.get("/login")
async def login_page(request: Request):
"""Render the login page"""
# Check if user is already logged in
user = await auth.get_optional_user(request)
if user:
return RedirectResponse(url="/")
return templates.TemplateResponse("login.jinja", {"request": request})
@app.post("/login")
async def login(request: Request):
"""Handle login form submission"""
form_data = await request.form()
username = form_data.get("username")
invitation_code = form_data.get("invitation_code")
if not username or not invitation_code:
return templates.TemplateResponse(
"login.jinja",
{"request": request, "error": "Username and invitation code are required"},
)
# Try to authenticate
user = auth.authenticate_user(username, invitation_code)
if not user:
# If authentication fails, try to register (if the invitation code is valid)
user = auth.register_user(username, invitation_code)
if not user:
return templates.TemplateResponse(
"login.jinja",
{"request": request, "error": "Invalid username or invitation code"},
)
# Create access token
access_token = auth.create_access_token({"sub": str(user["id"])})
logger.info("User %s logged in successfully", user["username"])
# Create response with cookie
response = RedirectResponse(url="/", status_code=303)
response.set_cookie(
key="session",
value=access_token,
httponly=True,
max_age=60 * 60 * 24 * 7, # 1 week
samesite="lax",
)
return response
@app.get("/logout")
async def logout():
"""Log out the current user"""
response = RedirectResponse(url="/")
response.delete_cookie(key="session")
return response
# Admin metrics page
@app.get("/admin/metrics", response_class=HTMLResponse)
async def admin_metrics_page(request: Request):
user = await auth.get_optional_user(request)
if not user or not auth.is_admin(user):
return RedirectResponse(url="/", status_code=status.HTTP_302_FOUND)
metrics = get_metrics()
return templates.TemplateResponse(
"metrics.jinja", {"request": request, "user": user, "metrics": metrics}
)
@app.get("/admin", response_class=HTMLResponse)
async def admin_page(request: Request):
"""Admin page for managing invitation codes"""
user = await auth.get_optional_user(request)
if not user or not auth.is_admin(user):
return RedirectResponse(url="/")
invitation_codes = auth.get_user_invitation_codes(user["id"])
all_users = []
if auth.is_super_admin(user):
all_users = auth.get_all_users()
context = {
"request": request,
"user": user,
"invitation_codes": invitation_codes,
"all_users": all_users,
}
return templates.TemplateResponse("admin.jinja", context)
@app.post("/admin/create-invitation")
async def create_invitation(request: Request):
"""Create a new invitation code"""
# Get current user
user = await auth.get_optional_user(request)
# Check if user is admin
if not user or not auth.is_admin(user):
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="Not authorized")
# Create a new invitation code
auth.create_invitation_code(user["id"])
# Redirect back to admin page
return RedirectResponse(url="/admin", status_code=303)
class UserAdminUpdate(BaseModel):
"""Pydantic model for updating a user's admin status."""
user_id: int
is_admin: bool
@app.post("/admin/user/set-admin")
async def set_user_admin_status(request: Request, update: UserAdminUpdate):
"""Set a user's admin status (super admin only)"""
current_user = await auth.get_optional_user(request)
if not current_user or not auth.is_super_admin(current_user):
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="Not authorized")
auth.set_admin_status(update.user_id, update.is_admin)
return RedirectResponse(url="/admin", status_code=status.HTTP_303_SEE_OTHER)
@app.get("/account", response_class=HTMLResponse)
async def account_page(request: Request):
user = await auth.get_optional_user(request)
if not user:
return RedirectResponse(url="/login", status_code=status.HTTP_302_FOUND)
user_comparisons = get_user_comparisons(user["id"])
invitation_codes = auth.get_user_invitation_codes(user["id"])
api_keys = auth.get_user_api_keys(user["id"])
return templates.TemplateResponse(
"account.jinja",
{
"request": request,
"user": user,
"comparisons": user_comparisons,
"invitation_codes": invitation_codes,
"api_keys": api_keys,
},
)
@app.post("/account/api-keys/create")
async def create_api_key_endpoint(request: Request, key_name: str = Form(...)):
user = await auth.get_optional_user(request)
if not user:
return JSONResponse(status_code=401, content={"error": "Authentication required"})
if not key_name or not key_name.strip():
return JSONResponse(status_code=400, content={"error": "Key name cannot be empty"})
try:
new_key = auth.create_api_key(user["id"], key_name.strip())
return JSONResponse(status_code=201, content={"key": new_key})
except Exception as e:
return JSONResponse(status_code=500, content={"error": str(e)})
@app.delete("/account/api-keys/delete/{key_id}")
async def delete_api_key_endpoint(request: Request, key_id: int):
user = await auth.get_optional_user(request)
if not user:
return JSONResponse(status_code=401, content={"error": "Authentication required"})
success = auth.delete_api_key(user["id"], key_id)
if success:
return JSONResponse(content={"message": "API Key deleted successfully."})
else:
return JSONResponse(
status_code=404,
content={"error": "Key not found or you do not have permission to delete it."},
)
@app.get("/compare/{comparison_id}")
async def view_comparison(request: Request, comparison_id: str):
"""
View a comparison by its ID.
Retrieves the comparison data, updates the last accessed timestamp,
and renders the comparison template with all necessary data.
"""
# Get current user if logged in
user = await auth.get_optional_user(request)
# Update last accessed timestamp
update_last_accessed(comparison_id)
# Get comparison data
comparison = get_comparison(comparison_id)
if not comparison:
return JSONResponse(status_code=404, content={"error": "Comparison not found"})
# Get image positions
rows = query_dicts(
"""
SELECT ip.filename, ip.row_number, ip.column_position,
im.original_filename, im.image_size, im.custom_name
FROM image_positions ip
LEFT JOIN image_metadata im
ON ip.comparison_id = im.comparison_id AND ip.filename = im.filename
WHERE ip.comparison_id = ?
ORDER BY ip.row_number, ip.column_position
""",
(comparison_id,),
)
images = []
image_names = []
image_sizes = []
for row in rows:
images.append(f"{comparison_id}/{row['filename']}")
# Use custom name if available, otherwise use original filename
image_names.append(row.get("custom_name") or row.get("original_filename"))
image_sizes.append(row.get("image_size"))
# Calculate expiration date
expiration_date = datetime.now() + timedelta(days=comparison.get("expiration_days", 7))
never_expires = comparison.get("never_expire", False)
# Calculate the number of rows to show in dropdown (max 20)
dropdown_rows = min(comparison.get("total_rows", 1), 20)
context = {
"request": request,
"user": user,
"images": images,
"metadata": comparison,
"total_rows": comparison.get("total_rows", 1),
"total_columns": comparison.get("total_columns", 2),
"image_names": image_names,
"image_sizes": image_sizes,
"expiration_date": expiration_date.strftime("%Y-%m-%d"),
"expiration_days": comparison.get("expiration_days", 7),
"expiration_type": comparison.get("expiration_type", "from_last_access"),
"never_expire": never_expires,
"dropdown_rows": dropdown_rows,
}
return templates.TemplateResponse("compare.jinja", context)
@app.get("/")
async def home(request: Request):
"""Render the home page with upload form."""
user = await auth.get_optional_user(request)
return templates.TemplateResponse("index.jinja", {"request": request, "user": user})