poetry migration and general server cleanup
This commit is contained in:
@@ -6,14 +6,11 @@ from fastapi.middleware.cors import CORSMiddleware
|
||||
from fastapi.staticfiles import StaticFiles
|
||||
|
||||
import queries
|
||||
from __version__ import __version__
|
||||
from helpers import origins
|
||||
from models import About, Project
|
||||
from utils import VerifyToken
|
||||
from models import About, Project, Lucas
|
||||
|
||||
load_dotenv()
|
||||
app = FastAPI()
|
||||
auth = VerifyToken()
|
||||
|
||||
|
||||
app.add_middleware(
|
||||
@@ -28,19 +25,9 @@ app.mount("/static", StaticFiles(directory="static"), name="static")
|
||||
|
||||
|
||||
@app.get("/", status_code=status.HTTP_200_OK)
|
||||
async def root():
|
||||
available_routes = [
|
||||
"/",
|
||||
"/about",
|
||||
"/projects",
|
||||
"/static/resume.pdf",
|
||||
"/static/favicon.png",
|
||||
]
|
||||
return {
|
||||
"welcome": "backend api for lucasjensen.me",
|
||||
"version": __version__,
|
||||
"routes": available_routes,
|
||||
}
|
||||
async def root() -> Lucas:
|
||||
lucas = Lucas(about=queries.get_about(), projects=queries.get_projects())
|
||||
return lucas
|
||||
|
||||
|
||||
@app.get("/about", status_code=status.HTTP_200_OK)
|
||||
@@ -69,11 +56,9 @@ async def projects() -> list[Project]:
|
||||
|
||||
@app.get("/projects/{project_id}", status_code=status.HTTP_200_OK)
|
||||
async def project(project_id: int) -> Project:
|
||||
project = queries.get_project(project_id)
|
||||
|
||||
project = None
|
||||
try:
|
||||
project = queries.get_project(project_id)
|
||||
|
||||
except Exception as e:
|
||||
print(f"err getting projects: {e}")
|
||||
raise HTTPException(
|
||||
@@ -86,48 +71,3 @@ async def project(project_id: int) -> Project:
|
||||
detail=f"project with id {project_id} not found",
|
||||
)
|
||||
return project
|
||||
|
||||
|
||||
@app.post("/projects", status_code=status.HTTP_201_CREATED)
|
||||
async def post_project(project: Project, auth_result=Security(auth.verify)) -> Project:
|
||||
user_sub, test_sub = queries.get_subs().values()
|
||||
jwt_sub = auth_result.get("sub")
|
||||
if jwt_sub not in [user_sub, test_sub]:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
detail="unauthorized",
|
||||
)
|
||||
try:
|
||||
return queries.create_project(project)
|
||||
except Exception as e:
|
||||
print(f"err creating project: {e}")
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail=f"database error: {e}",
|
||||
)
|
||||
|
||||
|
||||
@app.delete("/projects/{project_id}", status_code=status.HTTP_204_NO_CONTENT)
|
||||
async def delete_project(project_id: int, auth_result=Security(auth.verify)):
|
||||
user_sub, test_sub = queries.get_subs().values()
|
||||
jwt_sub = auth_result.get("sub")
|
||||
if jwt_sub not in [user_sub, test_sub]:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
detail="unauthorized",
|
||||
)
|
||||
project = queries.get_project(project_id)
|
||||
|
||||
if project is None:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail=f"project with id {project_id} not found",
|
||||
)
|
||||
try:
|
||||
return queries.delete_project(project_id)
|
||||
except Exception as e:
|
||||
print(f"err deleting project: {e}")
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail=f"database error: {e}",
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user