Spaces:
Running
Running
File size: 2,351 Bytes
88be5d0 9d3cbac 58a7ac0 88be5d0 58a7ac0 359afc2 9d3cbac 359afc2 58a7ac0 359afc2 58a7ac0 9d3cbac c79d28c 58a7ac0 88be5d0 fc7a233 88be5d0 fc7a233 88be5d0 fc7a233 88be5d0 58a7ac0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
from fastapi import FastAPI, Body, Header, Request, Query
from fastapi.middleware.cors import CORSMiddleware
from utils import beijing, parse_token
from logging_helper import LoggingHelper
from fastapi.templating import Jinja2Templates
import datetime
app = FastAPI(
title=f"Log Displayer",
description=f"Updated at {beijing()}",
)
print("Adding middlewares...", end="")
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
print("Done\n")
LOGS_REPO_ID = "pgsoft/logs"
local_dir = "data/logs"
logger = LoggingHelper(
repo_id=LOGS_REPO_ID,
local_dir=local_dir,
)
@app.post("/{end}")
async def add_log(
end: str,
message: str = Body(..., embed=True),
token: str | None = Header(None),
source: str | None = Header("web"),
):
print("Type: ", end)
print("From: ", source)
print("Token: ", token)
uid, username = parse_token(token)
timestamp = beijing().isoformat()
print("Timestamp: ", timestamp)
print("Message: ", message)
log = {
"type": end,
"source": source,
"uid": uid,
"username": username,
"token": token,
"content": message,
"timestamp": timestamp,
}
logger.addlog(log)
return True
@app.get("/healthcheck")
async def health_check():
return True
templates = Jinja2Templates(directory="static")
@app.get("")
@app.get("/")
async def root(
request: Request,
from_date: str | None = Query(None),
to_date: str | None = Query(None),
):
"""
首页端点,支持日期范围查询
查询参数:
- from_date: 开始日期(格式:YYYY-MM-DD),不指定时默认加载今天
- to_date: 结束日期(格式:YYYY-MM-DD),不指定时默认为今天
"""
# 如果没有指定日期范围,默认加载今天的日志
if from_date is None and to_date is None:
today = beijing().date().strftime("%Y-%m-%d")
from_date = today # 最近30天(包括今天)
to_date = today
print(f"[root] No date range specified, using today: {from_date} to {to_date}")
data = logger.refresh(from_date=from_date, to_date=to_date)
return templates.TemplateResponse(
"index.html",
{"request": request, "data": data},
)
|