-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
80 lines (64 loc) · 2.32 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
74
75
76
77
78
79
80
# encoding:utf-8
import os
import json
from fastapi import FastAPI, Request
from fastapi.responses import HTMLResponse, Response, JSONResponse
from fastapi.staticfiles import StaticFiles
from fastapi.exceptions import RequestValidationError
from starlette.exceptions import HTTPException as StarletteHTTPException
from f_ai.common import config, res_error
from f_ai.routers.index import index_router
tags_metadata = [
{
"name": "image",
"description": "图像处理",
"externalDocs": {
"description": "Items external docs",
"url": "https://fastapi.tiangolo.com/",
},
},
{
"name": "text",
"description": "文本处理",
},
]
app = FastAPI(
debug=False,
title='f-ai', version="2022.09.30", description="基于Paddle的模型接口",
terms_of_service="https://github.com/2720851545/f-ai",
contact={"name": "llyke", "url": "https://github.com/2720851545",
"email": "[email protected]", },
license_info={"name": "Apache 2.0",
"url": "https://www.apache.org/licenses/LICENSE-2.0.html"},
openapi_tags=tags_metadata, )
@app.middleware("http")
async def global_middleware(request: Request, call_next):
if request.url.path.startswith("/api/") and os.getenv('F_AI_ENV') == 'test':
return Response(
content=json.dumps(
res_error(message='服务器顶不住, 请本地运行测试😁'), indent=2),
status_code=200,
)
response: Response = await call_next(request)
return response
@app.exception_handler(StarletteHTTPException)
async def validation_exception_handler(request, err):
return JSONResponse(res_error(message=err.detail))
@app.exception_handler(RequestValidationError)
async def validation_exception_handler(request, err: RequestValidationError):
return JSONResponse(res_error(err.errors()))
@app.get("/", summary="首页", response_class=HTMLResponse)
def read_root():
return """
<html>
<h3>基于百度飞浆的人工智障服务</h3>
<a href="https://f-ai-production.up.railway.r/docs">跳转官方文档</a>
</html>
"""
app.include_router(
index_router,
prefix="/api/v1",
)
if __name__ == '__main__':
app.mount(
"/static", StaticFiles(directory=config.image_result_path), name="static")