forked from wuranxu/pity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·73 lines (53 loc) · 2 KB
/
main.py
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
from mimetypes import guess_type
from os.path import isfile
import uvicorn
from fastapi import Request
from fastapi.middleware.cors import CORSMiddleware
from starlette.responses import Response
from starlette.staticfiles import StaticFiles
from starlette.templating import Jinja2Templates
from app import pity
from app.routers.auth import user
from app.routers.config import router as config_router
from app.routers.online import router as online_router
from app.routers.project import project
from app.routers.request import http
from app.routers.testcase import testcase
pity.include_router(user.router)
pity.include_router(project.router)
pity.include_router(http.router)
pity.include_router(testcase.router)
pity.include_router(config_router)
pity.include_router(online_router)
pity.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
pity.mount("/statics", StaticFiles(directory="statics"), name="statics")
templates = Jinja2Templates(directory="statics")
@pity.get("/")
async def serve_spa(request: Request):
return templates.TemplateResponse("index.html", {"request": request})
@pity.get("/{filename}")
async def get_site(filename):
filename = './statics/' + filename
if not isfile(filename):
return Response(status_code=404)
with open(filename, mode='rb') as f:
content = f.read()
content_type, _ = guess_type(filename)
return Response(content, media_type=content_type)
@pity.get("/static/{filename}")
async def get_site(filename):
filename = './statics/static/' + filename
if not isfile(filename):
return Response(status_code=404)
with open(filename, mode='rb') as f:
content = f.read()
content_type, _ = guess_type(filename)
return Response(content, media_type=content_type)
if __name__ == "__main__":
uvicorn.run(app='main:pity', host='0.0.0.0', port=7777, reload=False)