-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
67 lines (56 loc) · 1.77 KB
/
main.py
File metadata and controls
67 lines (56 loc) · 1.77 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
55
56
57
58
59
60
61
62
63
64
65
66
67
import os
import json
import argparse
import logging
from fastapi import FastAPI, Request
from fastapi.responses import FileResponse
from apis.update import router as app_update_router
from apis.notice import router as app_notice_router
from apis.info import router as app_info_router
CONFIG_FILE = 'config.json'
DEFAULT_HOST = '0.0.0.0'
DEFAULT_PORT = 8080
def create_app() -> FastAPI:
app = FastAPI()
app.include_router(app_update_router)
app.include_router(app_notice_router)
app.include_router(app_info_router)
@app.exception_handler(404)
async def not_found_handler(request: Request, exc):
logging.warning(f"404 Not Found: {request.url}")
return FileResponse(
"static/1.mp3",
media_type="audio/mpeg",
filename="audio.mp3"
)
return app
def load_config() -> dict:
config = {
'host': DEFAULT_HOST,
'port': DEFAULT_PORT
}
# 从配置文件加载
try:
with open(CONFIG_FILE) as f:
config.update(json.load(f))
except FileNotFoundError:
pass
except Exception as e:
logging.error(f"加载配置文件出错: {e}")
# 环境变量覆盖
config['host'] = os.getenv('HOST', config['host'])
config['port'] = int(os.getenv('PORT', config['port']))
return config
app = create_app()
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--host', help='监听地址')
parser.add_argument('--port', type=int, help='监听端口')
args = parser.parse_args()
config = load_config()
if args.host:
config['host'] = args.host
if args.port:
config['port'] = args.port
import uvicorn
uvicorn.run(app=app, host=config['host'], port=config['port'], workers=1)