diff options
author | Parker <contact@pkrm.dev> | 2024-06-24 16:24:09 -0500 |
---|---|---|
committer | Parker <contact@pkrm.dev> | 2024-06-24 16:24:09 -0500 |
commit | 5b92454760a8af14bd1031e72024946f868d1de6 (patch) | |
tree | f8384cbf0d142777d9bff341e13fd5882182908b /api/main.py | |
parent | 80a39d38bf829193c655a7320c86df2a3146db2a (diff) |
Major overhaul + Bare bones web UI
Diffstat (limited to 'api/main.py')
-rw-r--r-- | api/main.py | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/api/main.py b/api/main.py new file mode 100644 index 0000000..c869bc0 --- /dev/null +++ b/api/main.py @@ -0,0 +1,59 @@ +from fastapi import FastAPI, Depends, HTTPException, Security +from fastapi.middleware.cors import CORSMiddleware +from fastapi.responses import RedirectResponse +import string +import random + +from api.routes.links_route import router as links_router +from api.util.db_dependency import get_db +from api.util.check_api_key import check_api_key +from models import User + + +metadata_tags = [ + {"name": "links", "description": "Operations for managing links"}, +] + +app = FastAPI( + title="LinkLogger API", + version="1.0", + summary="Public API for a combined link shortener and IP logger", + license_info={ + "name": "The Unlicense", + "identifier": "Unlicense", + "url": "https://unlicense.org", + }, + openapi_tags=metadata_tags, +) + +app.add_middleware( + CORSMiddleware, + allow_origins=["*"], + allow_methods=["*"], + allow_headers=["*"], + allow_credentials=True, +) + +# Import routes +app.include_router(links_router) + +# Regenerate the API key for the user +@app.post("/regenerate") +async def login(api_key: str = Security(check_api_key), db = Depends(get_db)): + print(api_key['value']) + + user = db.query(User).filter(User.api_key == api_key['value']).first() + if not user: + raise HTTPException(status_code=401, detail="Invalid API key") + + # Generate a new API key + new_api_key = ''.join(random.choices(string.ascii_letters + string.digits, k=20)) + user.api_key = new_api_key + db.commit() + + return {"status": "success", "new_api_key": new_api_key} + +# Redirect /api -> /api/docs +@app.get("/") +async def redirect_to_docs(): + return RedirectResponse(url="/api/docs")
\ No newline at end of file |