リファクタリング
This commit is contained in:
parent
d5ca0a68da
commit
7a3db8c48c
3 changed files with 65 additions and 24 deletions
27
README.md
Normal file
27
README.md
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
# LiteY
|
||||||
|
|
||||||
|
軽量な掲示板です
|
||||||
|
|
||||||
|
## デバッグの開始
|
||||||
|
|
||||||
|
データベースを起動します。
|
||||||
|
|
||||||
|
```bash
|
||||||
|
docker run --detach \
|
||||||
|
--name litey-mongo-debug \
|
||||||
|
--volume lzitey-mongo-debug:/data/db \
|
||||||
|
--publish 27017:27017 \
|
||||||
|
mongo
|
||||||
|
```
|
||||||
|
|
||||||
|
サーバーを起動します
|
||||||
|
|
||||||
|
```bash
|
||||||
|
fastapi dev
|
||||||
|
```
|
||||||
|
|
||||||
|
## デプロイ
|
||||||
|
|
||||||
|
今すぐデプロイ!
|
||||||
|
|
||||||
|
- https://litey.trade/
|
60
app.py
60
app.py
|
@ -6,6 +6,7 @@ from os import environ
|
||||||
from urllib.parse import urlparse
|
from urllib.parse import urlparse
|
||||||
from base64 import b64encode
|
from base64 import b64encode
|
||||||
from re import escape, compile, IGNORECASE
|
from re import escape, compile, IGNORECASE
|
||||||
|
from pprint import pprint
|
||||||
|
|
||||||
from requests import get
|
from requests import get
|
||||||
from fastapi import FastAPI, Request, Response, status
|
from fastapi import FastAPI, Request, Response, status
|
||||||
|
@ -13,6 +14,7 @@ from fastapi.responses import JSONResponse, Response, PlainTextResponse, FileRes
|
||||||
from fastapi.templating import Jinja2Templates
|
from fastapi.templating import Jinja2Templates
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
from pymongo import MongoClient, DESCENDING
|
from pymongo import MongoClient, DESCENDING
|
||||||
|
from contextlib import asynccontextmanager
|
||||||
|
|
||||||
# Jinja2
|
# Jinja2
|
||||||
|
|
||||||
|
@ -45,23 +47,33 @@ def is_over_n_hours(src: datetime, hours: int) -> bool:
|
||||||
|
|
||||||
# 初期化
|
# 初期化
|
||||||
|
|
||||||
templates = Jinja2Templates("templates")
|
ctx = {}
|
||||||
|
|
||||||
templates.env.filters["ip_to_uid"] = ip_to_uid
|
@asynccontextmanager
|
||||||
templates.env.filters["replace_ng_words"] = replace_ng_words
|
async def lifespan(app: FastAPI):
|
||||||
templates.env.filters["content_to_linksets"] = content_to_linksets
|
ctx["templates"] = Jinja2Templates("templates")
|
||||||
templates.env.filters["fromisoformat"] = datetime.fromisoformat
|
|
||||||
templates.env.filters["is_over_n_hours"] = is_over_n_hours
|
|
||||||
|
|
||||||
mongo_client = MongoClient(
|
ctx["templates"].env.filters["ip_to_uid"] = ip_to_uid
|
||||||
environ.get("MONGO_URI", "mongodb://127.0.0.1:27017/"),
|
ctx["templates"].env.filters["replace_ng_words"] = replace_ng_words
|
||||||
username=environ.get("MONGO_USER"),
|
ctx["templates"].env.filters["content_to_linksets"] = content_to_linksets
|
||||||
password=environ.get("MONGO_PASSWORD")
|
ctx["templates"].env.filters["fromisoformat"] = datetime.fromisoformat
|
||||||
)
|
ctx["templates"].env.filters["is_over_n_hours"] = is_over_n_hours
|
||||||
|
|
||||||
#uuid重複する?考えすぎ?
|
ctx["mongo_client"] = MongoClient(
|
||||||
#mongo_client.litey.notes.create_index("id", unique=True)
|
environ.get("MONGO_URI", "mongodb://127.0.0.1:27017/"),
|
||||||
mongo_client.litey.ngs.create_index("word", unique=True)
|
username=environ.get("MONGO_USER"),
|
||||||
|
password=environ.get("MONGO_PASSWORD")
|
||||||
|
)
|
||||||
|
|
||||||
|
#uuid重複する?考えすぎ?
|
||||||
|
ctx["mongo_client"].litey.notes.create_index("id", unique=True)
|
||||||
|
ctx["mongo_client"].litey.ngs.create_index("word", unique=True)
|
||||||
|
|
||||||
|
pprint(ctx)
|
||||||
|
yield
|
||||||
|
ctx["mongo_client"].close()
|
||||||
|
|
||||||
|
ctx.clear()
|
||||||
|
|
||||||
# スニペット
|
# スニペット
|
||||||
|
|
||||||
|
@ -103,18 +115,18 @@ def get_ip(req: Request) -> str:
|
||||||
|
|
||||||
def get_litey_notes(id: str = None) -> List[dict]:
|
def get_litey_notes(id: str = None) -> List[dict]:
|
||||||
if not id:
|
if not id:
|
||||||
cursor = mongo_client.litey.notes.find({}, { "_id": False }).sort("date", DESCENDING)
|
cursor = ctx["mongo_client"].litey.notes.find({}, { "_id": False }).sort("date", DESCENDING)
|
||||||
return list(cursor)
|
return list(cursor)
|
||||||
|
|
||||||
return mongo_client.litey.notes.find_one({ "id": id }, { "_id": False })
|
return ctx["mongo_client"].litey.notes.find_one({ "id": id }, { "_id": False })
|
||||||
|
|
||||||
def get_ng_words() -> List[str]:
|
def get_ng_words() -> List[str]:
|
||||||
cursor = mongo_client.litey.ngs.find({}, { "_id": False })
|
cursor = ctx["mongo_client"].litey.ngs.find({}, { "_id": False })
|
||||||
return [ng["word"] for ng in list(cursor) if "word" in ng]
|
return [ng["word"] for ng in list(cursor) if "word" in ng]
|
||||||
|
|
||||||
# FastAPI
|
# FastAPI
|
||||||
|
|
||||||
app = FastAPI()
|
app = FastAPI(lifespan=lifespan)
|
||||||
|
|
||||||
@app.middleware("http")
|
@app.middleware("http")
|
||||||
async def cors_handler(req: Request, call_next: Callable[[Request], Awaitable[Response]]):
|
async def cors_handler(req: Request, call_next: Callable[[Request], Awaitable[Response]]):
|
||||||
|
@ -140,7 +152,7 @@ async def api_get(id: str = None):
|
||||||
|
|
||||||
@app.post("/api/litey/post")
|
@app.post("/api/litey/post")
|
||||||
async def api_post(item: LiteYItem, req: Request):
|
async def api_post(item: LiteYItem, req: Request):
|
||||||
mongo_client.litey.notes.insert_one({
|
ctx["mongo_client"].litey.notes.insert_one({
|
||||||
"id": str(uuid4()),
|
"id": str(uuid4()),
|
||||||
"content": item.content,
|
"content": item.content,
|
||||||
"date": datetime.now().astimezone(timezone.utc).isoformat(),
|
"date": datetime.now().astimezone(timezone.utc).isoformat(),
|
||||||
|
@ -151,7 +163,9 @@ async def api_post(item: LiteYItem, req: Request):
|
||||||
|
|
||||||
@app.post("/api/litey/delete")
|
@app.post("/api/litey/delete")
|
||||||
async def api_delete(item: LiteYDeleteItem):
|
async def api_delete(item: LiteYDeleteItem):
|
||||||
mongo_client.litey.notes.delete_one({ "id": item.id })
|
ctx["mongo_client"].litey.notes.delete_one({
|
||||||
|
"id": item.id
|
||||||
|
})
|
||||||
|
|
||||||
return PlainTextResponse("OK")
|
return PlainTextResponse("OK")
|
||||||
|
|
||||||
|
@ -178,7 +192,7 @@ async def api_ng_get():
|
||||||
|
|
||||||
@app.post("/api/ng/post")
|
@app.post("/api/ng/post")
|
||||||
async def api_ng_post(item: NGItem):
|
async def api_ng_post(item: NGItem):
|
||||||
mongo_client.litey.ngs.insert_one({
|
ctx["mongo_client"].litey.ngs.insert_one({
|
||||||
"word": item.word
|
"word": item.word
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -186,7 +200,7 @@ async def api_ng_post(item: NGItem):
|
||||||
|
|
||||||
@app.post("/api/ng/delete")
|
@app.post("/api/ng/delete")
|
||||||
async def api_ng_delete(item: NGItem):
|
async def api_ng_delete(item: NGItem):
|
||||||
mongo_client.litey.ngs.delete_one({
|
ctx["mongo_client"].litey.ngs.delete_one({
|
||||||
"word": item.word
|
"word": item.word
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -194,7 +208,7 @@ async def api_ng_delete(item: NGItem):
|
||||||
|
|
||||||
@app.get("/")
|
@app.get("/")
|
||||||
async def home(req: Request):
|
async def home(req: Request):
|
||||||
res = templates.TemplateResponse(req, "index.html", {
|
res = ctx["templates"].TemplateResponse(req, "index.html", {
|
||||||
"notes": get_litey_notes(),
|
"notes": get_litey_notes(),
|
||||||
"ng_words": get_ng_words()
|
"ng_words": get_ng_words()
|
||||||
})
|
})
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
requests==2.32.3
|
requests==2.32.3
|
||||||
fastapi[standard]==0.115.2
|
fastapi[standard]==0.115.4
|
||||||
pymongo==4.10.1
|
pymongo==4.10.1
|
||||||
Jinja2==3.1.4
|
Jinja2==3.1.4
|
||||||
|
|
Loading…
Add table
Reference in a new issue