feat: implement server
This commit is contained in:
parent
3b9cf54747
commit
cdb360d4f3
50
main.py
Normal file
50
main.py
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
from datetime import datetime
|
||||||
|
from fastapi import FastAPI, status
|
||||||
|
from fastapi.staticfiles import StaticFiles
|
||||||
|
from sqlmodel import ARRAY, Column, SQLModel, Field, Session, String, create_engine
|
||||||
|
from fastapi.middleware.cors import CORSMiddleware
|
||||||
|
import uvicorn
|
||||||
|
|
||||||
|
with open("db.secrets", "r") as f:
|
||||||
|
db_secrets = f.readline().strip()
|
||||||
|
|
||||||
|
|
||||||
|
app = FastAPI(title="cutt")
|
||||||
|
engine = create_engine(db_secrets)
|
||||||
|
# SQLModel.metadata.create_all(engine)
|
||||||
|
origins = [
|
||||||
|
"*",
|
||||||
|
"http://localhost",
|
||||||
|
"http://localhost:3000",
|
||||||
|
"http://localhost:8000",
|
||||||
|
]
|
||||||
|
|
||||||
|
app.add_middleware(
|
||||||
|
CORSMiddleware,
|
||||||
|
allow_origins=origins,
|
||||||
|
allow_credentials=True,
|
||||||
|
allow_methods=["*"],
|
||||||
|
allow_headers=["*"],
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class Chemistry(SQLModel, table=True):
|
||||||
|
id: int | None = Field(default=None, primary_key=True)
|
||||||
|
time: datetime | None = Field(default_factory=datetime.now)
|
||||||
|
user: str
|
||||||
|
love: list[str] = Field(sa_column=Column(ARRAY(String)))
|
||||||
|
hate: list[str] = Field(sa_column=Column(ARRAY(String)))
|
||||||
|
undecided: list[str] = Field(sa_column=Column(ARRAY(String)))
|
||||||
|
|
||||||
|
|
||||||
|
@app.post("/chemistry/", status_code=status.HTTP_200_OK)
|
||||||
|
def submit_chemistry(chemistry: Chemistry):
|
||||||
|
with Session(engine) as session:
|
||||||
|
session.add(chemistry)
|
||||||
|
session.commit()
|
||||||
|
|
||||||
|
|
||||||
|
app.mount("/", StaticFiles(directory="dist", html=True), name="site")
|
||||||
|
|
||||||
|
# if __name__ == "__main__":
|
||||||
|
# uvicorn.run("main:app", workers=1, port=8096)
|
13
pyproject.toml
Normal file
13
pyproject.toml
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
[project]
|
||||||
|
name = "cutt"
|
||||||
|
version = "0.1.0"
|
||||||
|
description = "Add your description here"
|
||||||
|
author = "julius"
|
||||||
|
readme = "README.md"
|
||||||
|
requires-python = ">=3.13"
|
||||||
|
dependencies = [
|
||||||
|
"fastapi[standard]>=0.115.7",
|
||||||
|
"psycopg>=3.2.4",
|
||||||
|
"sqlmodel>=0.0.22",
|
||||||
|
"uvicorn>=0.34.0",
|
||||||
|
]
|
Loading…
x
Reference in New Issue
Block a user