Use query parameters for year, day and mode. Handle file stream message.

This commit is contained in:
Mikaël Capelle
2024-12-10 16:26:04 +00:00
parent cb69fea7ac
commit eb64edb22f
10 changed files with 351 additions and 153 deletions

View File

@@ -6,7 +6,8 @@ import subprocess
from pathlib import Path
from typing import Any, Literal, TypeAlias
from fastapi import FastAPI, Form
from fastapi import FastAPI, Form, HTTPException
from fastapi.responses import FileResponse
from sse_starlette import EventSourceResponse
DataMode: TypeAlias = Literal["holt59", "tests"]
@@ -14,6 +15,7 @@ DataMode: TypeAlias = Literal["holt59", "tests"]
LOGGER = logging.getLogger("uvicorn.info")
GIT_AOC_PATH = Path("/code")
AOC_FILES_PATH = Path("/files")
def update_repository():
@@ -54,6 +56,16 @@ async def data(year: int, day: int, mode: DataMode = "tests"):
return {"content": read_test_file(year, day, mode)}
@app.get("/file")
async def file(name: str):
path = AOC_FILES_PATH.joinpath(name).resolve()
if path.parent != AOC_FILES_PATH:
raise HTTPException(403)
return FileResponse(path)
@app.post("/submit-sse")
async def submit_sse(year: int = Form(), day: int = Form(), input: str = Form()):
data = input.rstrip().replace("\r\n", "\n")
@@ -62,6 +74,8 @@ async def submit_sse(year: int = Form(), day: int = Form(), input: str = Form())
"holt59-aoc",
"--stdin",
"--api",
"--output",
AOC_FILES_PATH,
"--year",
str(year),
str(day),