-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.py
91 lines (67 loc) · 2.75 KB
/
api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import json
from fastapi import FastAPI, Request
from fastapi.responses import HTMLResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from fastapi.middleware.cors import CORSMiddleware
from fastapi.exceptions import HTTPException
from pydantic import BaseModel
from db import load_db, games
from main import run, Capturing
database = load_db()
app = FastAPI()
origins = [
"*",
]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
app.mount("/static", StaticFiles(directory="static"), name="static")
templates = Jinja2Templates(directory="templates")
@app.on_event("startup")
async def startup():
await database.connect()
@app.on_event("shutdown")
async def shutdown():
await database.disconnect()
@app.get("/", response_class=HTMLResponse)
async def root(request: Request):
return templates.TemplateResponse("index.html", {"request": request})
@app.get('/question/{question_name}/', response_class=HTMLResponse)
async def question(request: Request, question_name):
try:
with open(f"./questions/{question_name}.json", 'r') as f:
question = json.loads(f.read())
del question['large_question']
return templates.TemplateResponse("question.html", {"request": request, "question": question})
except FileNotFoundError:
raise HTTPException(status_code=404, detail="Item not found")
class Answer(BaseModel):
username: str
language: str
code: str
@app.post('/question/{question_name}/test/')
async def test_result(question_name, answer: Answer):
output = run(question_name, answer.code, answer.language)
return {"STDOUT": output}
@app.post('/question/{question_name}/submit/')
async def submit_result(question_name, answer: Answer):
output = run(question_name, answer.code, answer.language)
passed = output[0] == "SUCCESS"
if passed:
time = output[1]
query = games.insert().values(game=question_name, time=time, code=answer.code, language=answer.language, user=answer.username, success=passed)
else:
query = games.insert().values(game=question_name, time='99', code=answer.code, language=answer.language, user=answer.username, success=passed)
last_record_id = await database.execute(query)
return {**answer.dict(), "id": last_record_id}
@app.get('/question/{question_name}/leaderboard/', response_class=HTMLResponse)
async def leaderboard(request: Request, question_name):
query = games.select().filter_by(game=question_name).order_by("time")
resp = await database.fetch_all(query)
return templates.TemplateResponse("leaderboard.html",
{"request": request, "results": resp})