more moving of files
This commit is contained in:
1
server/app/__init__.py
Normal file
1
server/app/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
from .main import app # noqa: F401
|
||||
81
server/app/main.py
Normal file
81
server/app/main.py
Normal file
@@ -0,0 +1,81 @@
|
||||
from dotenv import load_dotenv
|
||||
from fastapi import FastAPI, HTTPException, status
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from fastapi.staticfiles import StaticFiles
|
||||
|
||||
from app.model import queries
|
||||
from app.types import About, Project, Lucas
|
||||
|
||||
load_dotenv()
|
||||
app = FastAPI()
|
||||
|
||||
origins = [
|
||||
"http://localhost",
|
||||
"http://localhost:3000",
|
||||
"https://localhost:3000",
|
||||
"http://127.0.0.1:3000",
|
||||
"https://lucasjensen.me/",
|
||||
"https://lucasjensen.me",
|
||||
"https://www.lucasjensen.me/",
|
||||
"https://www.lucasjensen.me",
|
||||
]
|
||||
|
||||
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origins=origins,
|
||||
allow_credentials=True,
|
||||
allow_methods=["*"],
|
||||
allow_headers=["*"],
|
||||
)
|
||||
|
||||
app.mount("/static", StaticFiles(directory="static"), name="static")
|
||||
|
||||
|
||||
@app.get("/", status_code=status.HTTP_200_OK)
|
||||
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)
|
||||
async def about() -> About:
|
||||
try:
|
||||
return queries.get_about()
|
||||
except Exception as e:
|
||||
print(f"err getting about: {e}")
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail=f"database error: {e}",
|
||||
)
|
||||
|
||||
|
||||
@app.get("/projects", status_code=status.HTTP_200_OK)
|
||||
async def projects() -> list[Project]:
|
||||
try:
|
||||
return queries.get_projects()
|
||||
except Exception as e:
|
||||
print(f"err getting projects: {e}")
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail=f"database error: {e}",
|
||||
)
|
||||
|
||||
|
||||
@app.get("/projects/{project_id}", status_code=status.HTTP_200_OK)
|
||||
async def project(project_id: int) -> Project:
|
||||
project = None
|
||||
try:
|
||||
project = queries.get_project(project_id)
|
||||
except Exception as e:
|
||||
print(f"err getting projects: {e}")
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail=f"database error: {e}",
|
||||
)
|
||||
if project is None:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail=f"project with id {project_id} not found",
|
||||
)
|
||||
return project
|
||||
0
server/app/model/__init__.py
Normal file
0
server/app/model/__init__.py
Normal file
31
server/app/model/queries.py
Normal file
31
server/app/model/queries.py
Normal file
@@ -0,0 +1,31 @@
|
||||
from app.utils.db import connect_db
|
||||
from app.types import About, Project
|
||||
|
||||
|
||||
def get_projects() -> list[Project]:
|
||||
db = connect_db()
|
||||
cursor = db.cursor(dictionary=True)
|
||||
cursor.execute("SELECT * FROM projects")
|
||||
data = cursor.fetchall()
|
||||
projects = [Project(**p) for p in data] # type: ignore
|
||||
db.close()
|
||||
return projects
|
||||
|
||||
|
||||
def get_project(project_id: int) -> Project | None:
|
||||
db = connect_db()
|
||||
cursor = db.cursor(dictionary=True)
|
||||
cursor.execute("SELECT * FROM projects WHERE id=%s", (project_id,))
|
||||
data = cursor.fetchone()
|
||||
db.close()
|
||||
|
||||
return None if data is None else Project(**data) # type: ignore
|
||||
|
||||
|
||||
def get_about() -> About:
|
||||
db = connect_db()
|
||||
cursor = db.cursor(dictionary=True)
|
||||
cursor.execute("SELECT name, email, bio, github, gitea FROM self")
|
||||
data = {key: val for key, val in cursor.fetchone().items()} # type: ignore
|
||||
db.close()
|
||||
return About(**data)
|
||||
3
server/app/types/__init__.py
Normal file
3
server/app/types/__init__.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from .about import About # noqa: F401
|
||||
from .project import Project # noqa: F401
|
||||
from .lucas import Lucas # noqa: F401
|
||||
9
server/app/types/about.py
Normal file
9
server/app/types/about.py
Normal file
@@ -0,0 +1,9 @@
|
||||
from pydantic import BaseModel
|
||||
|
||||
|
||||
class About(BaseModel):
|
||||
name: str
|
||||
email: str
|
||||
bio: str
|
||||
github: str
|
||||
gitea: str
|
||||
8
server/app/types/lucas.py
Normal file
8
server/app/types/lucas.py
Normal file
@@ -0,0 +1,8 @@
|
||||
from pydantic import BaseModel
|
||||
from .about import About
|
||||
from .project import Project
|
||||
|
||||
|
||||
class Lucas(BaseModel):
|
||||
projects: list[Project]
|
||||
about: About
|
||||
11
server/app/types/project.py
Normal file
11
server/app/types/project.py
Normal file
@@ -0,0 +1,11 @@
|
||||
from pydantic import BaseModel
|
||||
from typing import Optional
|
||||
|
||||
|
||||
class Project(BaseModel):
|
||||
id: Optional[int] = None
|
||||
name: str
|
||||
description: str
|
||||
source: Optional[str] = None
|
||||
live: Optional[str] = None
|
||||
is_self_hosted: Optional[bool] = False
|
||||
0
server/app/utils/__init__.py
Normal file
0
server/app/utils/__init__.py
Normal file
34
server/app/utils/db.py
Normal file
34
server/app/utils/db.py
Normal file
@@ -0,0 +1,34 @@
|
||||
import os
|
||||
|
||||
import mysql.connector
|
||||
from dotenv import load_dotenv
|
||||
from fastapi import HTTPException, status
|
||||
|
||||
|
||||
def connect_db() -> mysql.connector.MySQLConnection:
|
||||
load_dotenv()
|
||||
host = os.getenv("DB_HOST")
|
||||
user = os.getenv("DB_USER")
|
||||
password = os.getenv("DB_PASS")
|
||||
database = os.getenv("DB_NAME")
|
||||
|
||||
if None in (host, user, password, database):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail="err reading env vars",
|
||||
)
|
||||
|
||||
try:
|
||||
return mysql.connector.connect(
|
||||
host=host,
|
||||
user=user,
|
||||
password=password,
|
||||
database=database,
|
||||
auth_plugin="mysql_native_password",
|
||||
) # type: ignore
|
||||
except Exception as e:
|
||||
print(f"err connecting to db: {e}")
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail="err connecting to db",
|
||||
)
|
||||
Reference in New Issue
Block a user