-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.py
More file actions
66 lines (53 loc) · 1.5 KB
/
app.py
File metadata and controls
66 lines (53 loc) · 1.5 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
import sys
sys.path.append("../")
import platform
import signal
import uvicorn
import logging
from rich import print
from .config import AppConfig, get_origins
from fastapi.staticfiles import StaticFiles
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
def create_app(origins: list):
"""创建全局唯一实例"""
#app = FastAPI(on_startup=[on_startup])
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
# allow_origin_regex="*",
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
return app
# uvicorn 模式下,app 必须放在全局作用域中,不能放在 main 函数中!
app = create_app(get_origins(1))
# 挂载静态文件目录,模拟静态资源服务器
# app.mount("/tmp", StaticFiles(directory="tmp"), name="static")
print("挂载路由...")
from .agv_api import router as agv_water
app.include_router(agv_water, prefix="", tags=["agv_water"])
# 启动服务器
def run_uvcorn(cfg: AppConfig):
print(f"服务地址:{cfg.hostname}")
uvicorn.run(
cfg.uvicorn_app_url,
host=cfg.host,
port=cfg.port,
access_log=False, # 取消冗余日志
)
def main(args=[]):
port = 29000
cfg = AppConfig(
"agv_server.app:app",
is_encrypt=0,
port=port,
)
print("启动服务...")
run_uvcorn(cfg)
print("关闭服务...")
if __name__ == '__main__':
args = sys.argv[1:]
main(args)