aboutsummaryrefslogtreecommitdiff
path: root/api/main.py
blob: 5e38a1cf35fdfeddb4065cf968827c0070a0799c (plain)
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
from fastapi import FastAPI, Depends, Request, Path
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import RedirectResponse
from api.routes.auth_routes import router as auth_router
from api.routes.links_routes import router as links_router
from api.routes.user_routes import router as user_router
from api.routes.log_routes import router as log_router
from typing import Annotated
from apscheduler.schedulers.background import BackgroundScheduler
from contextlib import asynccontextmanager

from api.util.db_dependency import get_db
from api.util.clean_db import clean_db
from api.util.log import log
from models import Link
from config import LOG

scheduler = BackgroundScheduler()


# Handle the database cleanup scheduler
def start_scheduler():
    scheduler.add_job(clean_db, "interval", days=1)
    scheduler.start()


@asynccontextmanager
async def lifespan(app: FastAPI):
    try:
        start_scheduler()
        yield
    finally:
        scheduler.shutdown()


app = FastAPI(
    title="LinkLogger API",
    version="2.0",
    summary="Public API for a combined link shortener and IP logger",
    license_info={
        "name": "The Unlicense",
        "identifier": "Unlicense",
        "url": "https://unlicense.org",
    },
    lifespan=lifespan,
)

app.add_middleware(
    CORSMiddleware,
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# Import routes
app.include_router(auth_router, prefix="/api")
app.include_router(links_router, prefix="/api")
app.include_router(user_router, prefix="/api")
app.include_router(log_router, prefix="/api")


@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:
        # 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)


@app.get("/api/ping")
async def ping():
    return {"ping": "pong"}