forked from shenxianmq/Auto_Symlink
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweb.py
More file actions
54 lines (41 loc) · 1.36 KB
/
web.py
File metadata and controls
54 lines (41 loc) · 1.36 KB
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
import os
from uvicorn.config import Config
import multiprocessing
from fastapi import FastAPI, Request
from fastapi.staticfiles import StaticFiles
from fastapi.responses import HTMLResponse, PlainTextResponse
from fastapi.templating import Jinja2Templates
working_directory = os.path.dirname(os.path.abspath(__file__))
os.chdir(working_directory)
app = FastAPI()
# Mounting the static files directory
app.mount("/static", StaticFiles(directory="static"), name="static")
templates = Jinja2Templates(directory="templates")
def read_log_file():
file_path = "./config/auto_symlink.log"
try:
with open(file_path, "r", encoding="utf-8") as file:
content = file.read()
return content
except FileNotFoundError:
return f"File not found:{file_path}."
@app.get("/", response_class=HTMLResponse)
async def read_root(request: Request):
return templates.TemplateResponse("index.html", {"request": request})
@app.get("/api/get_log", response_class=PlainTextResponse)
async def get_log():
return read_log_file()
if __name__ == "__main__":
import uvicorn
# Run the application using uvicorn
Server = uvicorn.Server(
Config(
app,
host="0.0.0.0",
port=8095,
reload=False,
workers=multiprocessing.cpu_count(),
)
)
# 启动服务
Server.run()