File size: 1,614 Bytes
58a7ac0
9d3cbac
58a7ac0
 
 
 
359afc2
9d3cbac
 
 
 
 
 
 
 
 
 
 
 
 
 
359afc2
58a7ac0
 
 
 
 
 
359afc2
 
58a7ac0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9d3cbac
c79d28c
 
 
 
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
from fastapi import FastAPI, Body, Header, Request
from fastapi.middleware.cors import CORSMiddleware
from utils import beijing, parse_token
from logging_helper import LoggingHelper
from fastapi.templating import Jinja2Templates


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):
    data = logger.refresh()
    return templates.TemplateResponse(
        "index.html",
        {"request": request, "data": data},
    )