Compare commits

...

3 Commits
main ... dev

Author SHA1 Message Date
55e7ee145c
Add database cleanup scheduler 2024-12-19 16:35:18 -06:00
cfbaf8a0b2
Set cookies to secure (HTTPS) 2024-12-19 06:54:10 -06:00
c8e7ea7365
Bump vite 6.0.3 -> 6.0.4 2024-12-19 06:53:48 -06:00
6 changed files with 55 additions and 7 deletions

View File

@ -6,10 +6,31 @@ 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(
@ -21,6 +42,7 @@ app = FastAPI(
"identifier": "Unlicense",
"url": "https://unlicense.org",
},
lifespan=lifespan,
)
app.add_middleware(

View File

@ -43,7 +43,7 @@ async def login_for_access_token(
key="access_token",
value=access_token,
httponly=True, # Prevents client-side access
# secure=True, # Cookies are only sent over HTTPS
secure=True, # Cookies are only sent over HTTPS
)
return response

25
api/util/clean_db.py Normal file
View File

@ -0,0 +1,25 @@
import datetime
from api.util.db_dependency import get_db
from models import Link, Log
"""
Remove expired short links and their associated logs
"""
def clean_db():
db = next(get_db())
# Get all expired short links
expired_links = (
db.query(Link)
.filter(Link.expire_date < datetime.datetime.today())
.all()
)
# Delete all expired short links and their logs
for link in expired_links:
logs = db.query(Log).filter(Log.link == link.link).all()
for log in logs:
db.delete(log)
db.delete(link)

View File

@ -29,7 +29,7 @@
"globals": "^15.11.0",
"typescript": "~5.6.2",
"typescript-eslint": "^8.11.0",
"vite": "^6.0.3"
"vite": "^6.0.4"
},
"packageManager": "yarn@1.22.22+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e"
}

View File

@ -1593,10 +1593,10 @@ uri-js@^4.2.2:
dependencies:
punycode "^2.1.0"
vite@^6.0.3:
version "6.0.3"
resolved "https://registry.yarnpkg.com/vite/-/vite-6.0.3.tgz#cc01f403e326a9fc1e064235df8a6de084c8a491"
integrity sha512-Cmuo5P0ENTN6HxLSo6IHsjCLn/81Vgrp81oaiFFMRa8gGDj5xEjIcEpf2ZymZtZR8oU0P2JX5WuUp/rlXcHkAw==
vite@^6.0.4:
version "6.0.4"
resolved "https://registry.yarnpkg.com/vite/-/vite-6.0.4.tgz#fe7cfaedff7c701d5582be5c4ed6a2150538ea9d"
integrity sha512-zwlH6ar+6o6b4Wp+ydhtIKLrGM/LoqZzcdVmkGAFun0KHTzIzjh+h0kungEx7KJg/PYnC80I4TII9WkjciSR6Q==
dependencies:
esbuild "^0.24.0"
postcss "^8.4.49"

View File

@ -9,4 +9,5 @@ bcrypt==4.1.3
SQLAlchemy==2.0.31
validators==0.28.3
requests==2.32.3
ua-parser==0.18.0
ua-parser==0.18.0
APScheduler==3.11.0