backend: database and file upload test

This commit is contained in:
OleSTEEP 2025-10-02 22:44:29 +03:00
parent b62f6d87c7
commit d786d7f7af
6 changed files with 198 additions and 34 deletions

48
backend/api.py Normal file
View file

@ -0,0 +1,48 @@
from typing import Annotated
from fastapi import FastAPI, File, UploadFile
from fastapi.middleware.cors import CORSMiddleware
from pathlib import Path
from vntypes import Novel, Mark
from db import VNDB
app = FastAPI()
database = VNDB()
origins = [
"http://localhost",
"http://localhost:5173",
]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.get("/api/posts/{post_id}")
def get_post(post_id: int):
return "NOT IMPLEMENTED!"
@app.post("/api/new_post")
def new_post(novel: Novel):
print(novel)
return "yay!"
@app.post("/api/new_mark")
def new_mark(mark: Mark):
database.insert_mark(mark.type, mark.value)
return "yay!"
@app.post("/api/novel_thumb")
async def novel_thumb(thumb: Annotated[bytes, File()], filename: str):
return {"file_size": len(thumb)}
@app.post("/api/screenshot")
async def screenshot(scrshot: Annotated[bytes, File()], filename: str):
Path("screens/").mkdir(exist_ok=True)
with open(Path("screens", filename), "wb") as file:
file.write(scrshot)
return {"file_size": len(scrshot)}