Compare commits

..

2 commits

Author SHA1 Message Date
yuuki
fc01f4aa8a キャッシュとレートリミットの調整 2025-01-31 15:54:26 +09:00
yuuki
9164a4fc48 fastapiとpymongoの更新 2025-01-31 15:53:47 +09:00
3 changed files with 15 additions and 12 deletions

18
app.py
View file

@ -166,11 +166,11 @@ async def cors_handler(req: Request, call_next: Callable[[Request], Awaitable[Re
@app.get("/api/litey/get") @app.get("/api/litey/get")
async def api_get(id: str = None): async def api_get(id: str = None):
res = JSONResponse(get_litey_notes(id)) res = JSONResponse(get_litey_notes(id))
res.headers["Cache-Control"] = f"public, max-age=0, s-maxage=0" res.headers["Cache-Control"] = f"public, max-age=15, s-maxage=15"
res.headers["CDN-Cache-Control"] = f"max-age=0" res.headers["CDN-Cache-Control"] = f"max-age=15"
return res return res
@app.post("/api/litey/post") @app.post("/api/litey/post", dependencies=[Depends(RateLimiter(times=4, seconds=3600))])
async def api_post(item: LiteYItem, req: Request): async def api_post(item: LiteYItem, req: Request):
ctx["mongo_client"].litey.notes.insert_one({ ctx["mongo_client"].litey.notes.insert_one({
"id": str(uuid4()), "id": str(uuid4()),
@ -206,11 +206,11 @@ async def api_image_proxy(url: str):
@app.get("/api/ng/get") @app.get("/api/ng/get")
async def api_ng_get(): async def api_ng_get():
res = PlainTextResponse("\n".join(get_ng_words())) res = PlainTextResponse("\n".join(get_ng_words()))
res.headers["Cache-Control"] = f"public, max-age=0, s-maxage=0" res.headers["Cache-Control"] = f"public, max-age=15, s-maxage=15"
res.headers["CDN-Cache-Control"] = f"max-age=0" res.headers["CDN-Cache-Control"] = f"max-age=15"
return res return res
@app.post("/api/ng/post") @app.post("/api/ng/post", dependencies=[Depends(RateLimiter(times=4, seconds=3600))])
async def api_ng_post(item: NGItem): async def api_ng_post(item: NGItem):
ctx["mongo_client"].litey.ngs.insert_one({ ctx["mongo_client"].litey.ngs.insert_one({
"word": item.word "word": item.word
@ -218,7 +218,7 @@ async def api_ng_post(item: NGItem):
return PlainTextResponse("OK") return PlainTextResponse("OK")
@app.post("/api/ng/delete") @app.post("/api/ng/delete", dependencies=[Depends(RateLimiter(times=1, seconds=86400))])
async def api_ng_delete(item: NGItem): async def api_ng_delete(item: NGItem):
ctx["mongo_client"].litey.ngs.delete_one({ ctx["mongo_client"].litey.ngs.delete_one({
"word": item.word "word": item.word
@ -232,8 +232,8 @@ async def home(req: Request):
"notes": get_litey_notes(), "notes": get_litey_notes(),
"ng_words": get_ng_words() "ng_words": get_ng_words()
}) })
res.headers["Cache-Control"] = f"public, max-age=0, s-maxage=0" res.headers["Cache-Control"] = f"public, max-age=15, s-maxage=15"
res.headers["CDN-Cache-Control"] = f"max-age=0" res.headers["CDN-Cache-Control"] = f"max-age=15"
return res return res
@app.get("/{ref:path}") @app.get("/{ref:path}")

View file

@ -1,5 +1,5 @@
requests==2.32.3 requests==2.32.3
fastapi[standard]==0.115.7 fastapi[standard]==0.115.8
pymongo==4.10.1 pymongo==4.11
Jinja2==3.1.5 Jinja2==3.1.5
fastapi-limiter==0.1.6 fastapi-limiter==0.1.6

View file

@ -15,7 +15,10 @@ function notePost() {
msg.value = ""; msg.value = "";
alert("投稿に成功しました!"); alert("投稿に成功しました!");
} else { } else {
alert("投稿に失敗しました。"); (async () => {
const text = await res.text();
alert(`投稿に失敗しました。\n${text}`);
})();
} }
}); });
} }