refactor: 主入口 main.py 上移到 source/,端口重排为 9008/9007/9006
- main.py: source/openapi/main.py -> source/main.py - 端口: 聚合层 9888->9008, pixiv 后端 9088->9007, bilibili 后端 19990->9006 - nginx/openapi.rrrosmontis.icu.conf: 三处 proxy_pass 指向 127.0.0.1:9008 - systemd/openapi.service: WorkingDirectory/ExecStart 同步为 source/
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
from aiohttp import web, ClientSession, ClientTimeout
|
||||
|
||||
|
||||
PIXIV_BACKEND = "http://127.0.0.1:9088"
|
||||
BILIBILI_BACKEND = "http://127.0.0.1:19990"
|
||||
|
||||
|
||||
async def handle_pixiv(request):
|
||||
pid = request.query.get("id", "")
|
||||
if not pid:
|
||||
return web.json_response({"error": "missing id parameter"}, status=400)
|
||||
target = f"{PIXIV_BACKEND}/{pid}"
|
||||
return await proxy(request, target)
|
||||
|
||||
|
||||
async def handle_bilibili(request):
|
||||
qs = request.query_string
|
||||
target = f"{BILIBILI_BACKEND}/bilibili/get_video"
|
||||
if qs:
|
||||
target += "?" + qs
|
||||
return await proxy(request, target)
|
||||
|
||||
|
||||
async def proxy(request, target):
|
||||
timeout = ClientTimeout(total=300)
|
||||
async with ClientSession(timeout=timeout) as session:
|
||||
try:
|
||||
async with session.get(target) as resp:
|
||||
body = await resp.read()
|
||||
headers = {}
|
||||
for key in ("Content-Type", "Content-Disposition", "Cache-Control"):
|
||||
if key in resp.headers:
|
||||
headers[key] = resp.headers[key]
|
||||
return web.Response(
|
||||
body=body,
|
||||
status=resp.status,
|
||||
headers=headers,
|
||||
)
|
||||
except Exception as e:
|
||||
return web.json_response(
|
||||
{"error": "backend unreachable", "detail": str(e)}, status=502
|
||||
)
|
||||
|
||||
|
||||
async def handle_root(request):
|
||||
raise web.HTTPFound("https://rrrosmontis.icu/articles/index.html?id=00003")
|
||||
|
||||
|
||||
app = web.Application()
|
||||
app.router.add_get("/", handle_root)
|
||||
app.router.add_get("/pixiv/get_image", handle_pixiv)
|
||||
app.router.add_get("/bilibili/get_video", handle_bilibili)
|
||||
|
||||
if __name__ == "__main__":
|
||||
web.run_app(app, host="127.0.0.1", port=9888)
|
||||
Reference in New Issue
Block a user