teambuilding/main.py

59 lines
1.3 KiB
Python
Raw Permalink Normal View History

2024-11-01 10:02:01 +00:00
import uvicorn
from datetime import datetime
from fastapi import FastAPI, Request
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
app = FastAPI(title="Team Prefs", root_path="/team")
origins = [
"*",
"http://localhost",
"http://localhost:3000",
"https://localhost",
"https://0124816.xyz",
"http://0124816.xyz:3001",
]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
class Submission(BaseModel):
person: str
players: list[str]
db_file = "team.db"
@app.get("/players/")
def get_players() -> list[str]:
players = [line.strip() for line in open("players", "r")]
return players
@app.post("/submit/")
def submit(sub: Submission, request: Request):
print(datetime.now().strftime("%Y%m%d-%H%M%S"), sub.person, sub.players)
with open(db_file, "a") as f:
f.write(
"\t".join(
[
request.client.host,
datetime.now().strftime("%Y%m%d-%H%M%S"),
sub.person,
",".join(sub.players),
]
)
)
f.write("\n")
if __name__ == "__main__":
uvicorn.run("main:app", workers=1, port=8096)