From a8b6807b79a65bc04d969a41b69b993f7a44fea9 Mon Sep 17 00:00:00 2001 From: Lucas Jensen Date: Sat, 6 Jul 2024 10:36:37 -0700 Subject: [PATCH] reorganized --- server/README.md | 2 +- server/config.py | 27 --------------- server/helpers.py | 47 -------------------------- server/main.py | 20 +++++++---- server/{ => model}/queries.py | 2 +- server/pyproject.toml | 2 +- server/run.py | 3 ++ server/types/__init__.py | 3 ++ server/types/about.py | 9 +++++ server/types/lucas.py | 8 +++++ server/{models.py => types/project.py} | 13 ------- server/utils/__init__.py | 0 server/{ => utils}/db.py | 0 13 files changed, 40 insertions(+), 96 deletions(-) delete mode 100644 server/config.py delete mode 100644 server/helpers.py rename server/{ => model}/queries.py (98%) mode change 100644 => 100755 server/run.py create mode 100644 server/types/__init__.py create mode 100644 server/types/about.py create mode 100644 server/types/lucas.py rename server/{models.py => types/project.py} (60%) create mode 100644 server/utils/__init__.py rename server/{ => utils}/db.py (100%) diff --git a/server/README.md b/server/README.md index e4ab216..0067e92 100644 --- a/server/README.md +++ b/server/README.md @@ -18,7 +18,7 @@ mysql -u [username] -p [database] < create_tables.sql ### Run ```bash -uvicorn main:app --reload --port 8001 +./run.py ``` ### Test diff --git a/server/config.py b/server/config.py deleted file mode 100644 index f04d236..0000000 --- a/server/config.py +++ /dev/null @@ -1,27 +0,0 @@ -import os -from functools import lru_cache - -from pydantic_settings import BaseSettings - - -class Settings(BaseSettings): - auth0_domain: str - auth0_api_audience: str - auth0_issuer: str - auth0_algorithms: str - - -@lru_cache() -def get_settings(): - domain = os.getenv("AUTH0_DOMAIN") - audience = os.getenv("AUTH0_API_AUDIENCE") - issuer = os.getenv("AUTH0_ISSUER") - algorithms = os.getenv("AUTH0_ALGORITHMS") - if None in [domain, audience, issuer, algorithms]: - raise ValueError("Missing environment variables") - return Settings( - auth0_domain=domain, # type: ignore - auth0_api_audience=audience, # type: ignore - auth0_issuer=issuer, # type: ignore - auth0_algorithms=algorithms, # type: ignore - ) diff --git a/server/helpers.py b/server/helpers.py deleted file mode 100644 index 5c3a053..0000000 --- a/server/helpers.py +++ /dev/null @@ -1,47 +0,0 @@ -import os - -from dotenv import load_dotenv - -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", -] - - -def get_token() -> str: - import http.client - import json - - load_dotenv() - - client_id = os.getenv("CLIENT_ID") - client_secret = os.getenv("CLIENT_SECRET") - - conn = http.client.HTTPSConnection("lucasjensen.us.auth0.com") - - payload = ( - '{"client_id":"' - + f"{client_id}" - + '","client_secret":"' - + f"{client_secret}" - + '","audience":"' - + f"https://api.lucasjensen.me/" - + '","grant_type":"client_credentials"}' - ) - - headers = {"content-type": "application/json"} - - conn.request("POST", "/oauth/token", payload, headers) - - res = conn.getresponse() - - data = res.read() - body = json.loads(data.decode("utf-8")) - - return body["access_token"] diff --git a/server/main.py b/server/main.py index 18aa5a6..fe517d8 100644 --- a/server/main.py +++ b/server/main.py @@ -1,17 +1,25 @@ -import os - from dotenv import load_dotenv -from fastapi import FastAPI, HTTPException, Security, status +from fastapi import FastAPI, HTTPException, status from fastapi.middleware.cors import CORSMiddleware from fastapi.staticfiles import StaticFiles -import queries -from helpers import origins -from models import About, Project, Lucas +from model import queries +from 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, diff --git a/server/queries.py b/server/model/queries.py similarity index 98% rename from server/queries.py rename to server/model/queries.py index d191cfb..b580fef 100644 --- a/server/queries.py +++ b/server/model/queries.py @@ -1,4 +1,4 @@ -from db import connect_db +from utils.db import connect_db from models import About, Project diff --git a/server/pyproject.toml b/server/pyproject.toml index f1f53e3..5e5f819 100644 --- a/server/pyproject.toml +++ b/server/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "lucasjensen-fastapi" -version = "0.1.0" +version = "1.0.0" description = "A RESTful API for lucasjensen.me" authors = ["Lucas Jensen "] readme = "README.md" diff --git a/server/run.py b/server/run.py old mode 100644 new mode 100755 index f4b6f8a..bb25949 --- a/server/run.py +++ b/server/run.py @@ -1,3 +1,5 @@ +#!.venv/bin/python + import os import uvicorn from main import app @@ -6,5 +8,6 @@ from dotenv import load_dotenv if __name__ == "__main__": load_dotenv() + print("Starting development environment.\nDO NOT USE THIS IN PRODUCTION.") port = int(os.getenv("PORT", 5050)) uvicorn.run(app, port=port) diff --git a/server/types/__init__.py b/server/types/__init__.py new file mode 100644 index 0000000..c12d4fc --- /dev/null +++ b/server/types/__init__.py @@ -0,0 +1,3 @@ +from .about import About # noqa: F401 +from .project import Project # noqa: F401 +from .lucas import Lucas # noqa: F401 diff --git a/server/types/about.py b/server/types/about.py new file mode 100644 index 0000000..cfacb4a --- /dev/null +++ b/server/types/about.py @@ -0,0 +1,9 @@ +from pydantic import BaseModel + + +class About(BaseModel): + name: str + email: str + bio: str + github: str + gitea: str diff --git a/server/types/lucas.py b/server/types/lucas.py new file mode 100644 index 0000000..a491eee --- /dev/null +++ b/server/types/lucas.py @@ -0,0 +1,8 @@ +from pydantic import BaseModel +from about import About +from project import Project + + +class Lucas(BaseModel): + projects: list[Project] + about: About diff --git a/server/models.py b/server/types/project.py similarity index 60% rename from server/models.py rename to server/types/project.py index 2d2c331..aa17efb 100644 --- a/server/models.py +++ b/server/types/project.py @@ -2,14 +2,6 @@ from pydantic import BaseModel from typing import Optional -class About(BaseModel): - name: str - email: str - bio: str - github: str - gitea: str - - class Project(BaseModel): id: Optional[int] = None name: str @@ -17,8 +9,3 @@ class Project(BaseModel): source: Optional[str] = None live: Optional[str] = None is_self_hosted: Optional[bool] = False - - -class Lucas(BaseModel): - projects: list[Project] - about: About diff --git a/server/utils/__init__.py b/server/utils/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/server/db.py b/server/utils/db.py similarity index 100% rename from server/db.py rename to server/utils/db.py