Merge pull request 'reorganized' (#6) from refactor into main

Reviewed-on: #6
This commit is contained in:
Lucas Jensen
2024-07-06 17:38:20 +00:00
13 changed files with 40 additions and 96 deletions

View File

@@ -18,7 +18,7 @@ mysql -u [username] -p [database] < create_tables.sql
### Run
```bash
uvicorn main:app --reload --port 8001
./run.py
```
### Test

View File

@@ -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
)

View File

@@ -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"]

View File

@@ -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,

View File

@@ -1,4 +1,4 @@
from db import connect_db
from utils.db import connect_db
from models import About, Project

View File

@@ -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 <lucas@lucasjensen.me>"]
readme = "README.md"

3
server/run.py Normal file → Executable file
View File

@@ -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)

3
server/types/__init__.py Normal file
View 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/types/about.py Normal file
View File

@@ -0,0 +1,9 @@
from pydantic import BaseModel
class About(BaseModel):
name: str
email: str
bio: str
github: str
gitea: str

8
server/types/lucas.py Normal file
View 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

View File

@@ -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

0
server/utils/__init__.py Normal file
View File