From eadc9289338e8537c1ee70274007eea2dc395bde Mon Sep 17 00:00:00 2001 From: Parker Date: Tue, 5 Nov 2024 21:39:25 -0600 Subject: [PATCH] Re-add the link logging --- app/main.py | 73 ++++++++++++++++++++++++++++------------------------- 1 file changed, 38 insertions(+), 35 deletions(-) diff --git a/app/main.py b/app/main.py index 2d13ae2..0ac6d09 100644 --- a/app/main.py +++ b/app/main.py @@ -1,4 +1,4 @@ -from fastapi import FastAPI, Depends, Request +from fastapi import FastAPI, Depends, Request, Path from fastapi.middleware.cors import CORSMiddleware from fastapi.responses import RedirectResponse, JSONResponse from fastapi.templating import Jinja2Templates @@ -10,11 +10,15 @@ from fastapi.exceptions import HTTPException from starlette.status import HTTP_404_NOT_FOUND from app.util.authentication import get_current_user_from_cookie +from app.util.db_dependency import get_db +from app.util.log import log from app.schemas.auth_schemas import User +from models import Link + app = FastAPI( title="LinkLogger API", - version="1.0", + version="2.0", summary="Public API for a combined link shortener and IP logger", license_info={ "name": "The Unlicense", @@ -63,32 +67,32 @@ async def dashboard( ) -# @app.get("/{link}") -# async def log_redirect( -# link: Annotated[str, Path(title="Redirect link")], -# request: Request, -# db=Depends(get_db), -# ): -# link = link.upper() -# # If `link` is not exactly 5 characters, return redirect to base url -# if len(link) != 5: -# return RedirectResponse(url="/login") +@app.get("/c/{link}") +async def log_redirect( + link: Annotated[str, Path(title="Redirect link")], + request: Request, + db=Depends(get_db), +): + link = link.upper() + # Links must be 5 characters long + if len(link) != 5: + return RedirectResponse(url="/login") -# # Make sure the link exists in the database -# link_record: Link = db.query(Link).filter(Link.link == link).first() -# if not link_record: -# db.close() -# return RedirectResponse(url="/login") -# else: -# # Log the visit -# if request.headers.get("X-Real-IP"): -# ip = request.headers.get("X-Real-IP").split(",")[0] -# else: -# ip = request.client.host -# user_agent = request.headers.get("User-Agent") -# log(link, ip, user_agent) -# db.close() -# return RedirectResponse(url=link_record.redirect_link) + # Make sure the link exists in the database + link_record: Link = db.query(Link).filter(Link.link == link).first() + if not link_record: + db.close() + return RedirectResponse(url="/login") + else: + # Get the IP and log the request + if request.headers.get("X-Real-IP"): + ip = request.headers.get("X-Real-IP").split(",")[0] + else: + ip = request.client.host + user_agent = request.headers.get("User-Agent") + log(link, ip, user_agent) + db.close() + return RedirectResponse(url=link_record.redirect_link) # Redirect /api -> /api/docs @@ -100,12 +104,11 @@ async def redirect_to_docs(): # Custom handler for 404 errors @app.exception_handler(HTTP_404_NOT_FOUND) async def custom_404_handler(request: Request, exc: HTTPException): + # If the request is from /api, return a JSON response + if request.url.path.startswith("/api"): + return JSONResponse( + status_code=404, + content={"message": "Resource not found"}, + ) + # Otherwise, redirect to the login page return RedirectResponse(url="/login") - - -@app.exception_handler(HTTPException) -async def http_exception_handler(request, exc): - return JSONResponse( - status_code=exc.status_code, - content={"detail": f"{exc.detail}"}, - )