sanic-api 0.2.5__py3-none-any.whl → 0.2.7__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- example/__main__.py +19 -19
- example/api/__init__.py +1 -1
- example/api/t1/__init__.py +2 -1
- example/api/t1/{t1.py → t1_1.py} +22 -22
- example/api/t1/t1_2.py +9 -0
- example/api/t2/__init__.py +1 -1
- example/api/t2/t2.py +9 -9
- example/api/t2/t3/__init__.py +1 -1
- example/api/t2/t3/t3.py +9 -9
- example/api/user.py +89 -89
- example/api/validator.py +97 -97
- example/app.py +62 -62
- example/settings.py +10 -10
- sanic_api/__init__.py +21 -21
- sanic_api/api/__init__.py +2 -2
- sanic_api/api/api.py +177 -177
- sanic_api/api/exception.py +46 -46
- sanic_api/api/extend.py +25 -25
- sanic_api/api/handle_exception.py +61 -61
- sanic_api/api/model.py +45 -45
- sanic_api/api/validators.py +76 -76
- sanic_api/config/__init__.py +2 -2
- sanic_api/config/base.py +110 -110
- sanic_api/config/sanic.py +19 -21
- sanic_api/config/sanic_api.py +11 -13
- sanic_api/config/setting.py +30 -30
- sanic_api/enum.py +88 -88
- sanic_api/logger/__init__.py +1 -1
- sanic_api/logger/config.py +77 -77
- sanic_api/logger/extend.py +71 -71
- sanic_api/logger/sanic_http.py +68 -68
- sanic_api/openapi/__init__.py +7 -7
- sanic_api/openapi/openapi.py +102 -102
- sanic_api/utils.py +112 -97
- {sanic_api-0.2.5.dist-info → sanic_api-0.2.7.dist-info}/METADATA +4 -3
- sanic_api-0.2.7.dist-info/RECORD +39 -0
- sanic_api-0.2.7.dist-info/WHEEL +4 -0
- sanic_api-0.2.5.dist-info/RECORD +0 -38
- sanic_api-0.2.5.dist-info/WHEEL +0 -4
- {sanic_api-0.2.5.dist-info → sanic_api-0.2.7.dist-info}/licenses/LICENSE.txt +0 -0
example/__main__.py
CHANGED
@@ -1,19 +1,19 @@
|
|
1
|
-
from sanic import Sanic
|
2
|
-
from sanic.worker.loader import AppLoader
|
3
|
-
|
4
|
-
from example.app import app_factory
|
5
|
-
from example.settings import settings
|
6
|
-
from sanic_api.enum import RunModeEnum
|
7
|
-
|
8
|
-
if __name__ == "__main__":
|
9
|
-
loader = AppLoader(factory=app_factory)
|
10
|
-
app = loader.load()
|
11
|
-
app.prepare(
|
12
|
-
host=settings.host,
|
13
|
-
port=settings.port,
|
14
|
-
debug=settings.mode == RunModeEnum.DEBUG,
|
15
|
-
dev=settings.mode == RunModeEnum.DEV,
|
16
|
-
workers=settings.workers,
|
17
|
-
fast=settings.mode == RunModeEnum.PRODUCTION and settings.workers == 1,
|
18
|
-
)
|
19
|
-
Sanic.serve(app, app_loader=loader)
|
1
|
+
from sanic import Sanic
|
2
|
+
from sanic.worker.loader import AppLoader
|
3
|
+
|
4
|
+
from example.app import app_factory
|
5
|
+
from example.settings import settings
|
6
|
+
from sanic_api.enum import RunModeEnum
|
7
|
+
|
8
|
+
if __name__ == "__main__":
|
9
|
+
loader = AppLoader(factory=app_factory)
|
10
|
+
app = loader.load()
|
11
|
+
app.prepare(
|
12
|
+
host=settings.host,
|
13
|
+
port=settings.port,
|
14
|
+
debug=settings.mode == RunModeEnum.DEBUG,
|
15
|
+
dev=settings.mode == RunModeEnum.DEV,
|
16
|
+
workers=settings.workers,
|
17
|
+
fast=settings.mode == RunModeEnum.PRODUCTION and settings.workers == 1,
|
18
|
+
)
|
19
|
+
Sanic.serve(app, app_loader=loader)
|
example/api/__init__.py
CHANGED
@@ -1 +1 @@
|
|
1
|
-
from .user import users_blueprint
|
1
|
+
from .user import users_blueprint
|
example/api/t1/__init__.py
CHANGED
@@ -1 +1,2 @@
|
|
1
|
-
from .
|
1
|
+
from .t1_1 import t1_1_blueprint
|
2
|
+
from .t1_2 import t1_2_blueprint
|
example/api/t1/{t1.py → t1_1.py}
RENAMED
@@ -1,22 +1,22 @@
|
|
1
|
-
from sanic import Blueprint, Request, text
|
2
|
-
|
3
|
-
from sanic_api.api.exception import ServerException
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
@
|
10
|
-
async def hello(request):
|
11
|
-
return text("Hello world!")
|
12
|
-
|
13
|
-
|
14
|
-
@
|
15
|
-
async def error(request):
|
16
|
-
raise ServerException(message="Error")
|
17
|
-
|
18
|
-
|
19
|
-
@
|
20
|
-
async def restart(request: Request):
|
21
|
-
request.app.m.restart(all_workers=True, zero_downtime=True)
|
22
|
-
return text("ok")
|
1
|
+
from sanic import Blueprint, Request, text
|
2
|
+
|
3
|
+
from sanic_api.api.exception import ServerException
|
4
|
+
|
5
|
+
t1_1_blueprint = Blueprint("t1_1_blueprint", url_prefix="/t1_1")
|
6
|
+
t1_1_blueprint.ctx.desc = "测试蓝图1_1"
|
7
|
+
|
8
|
+
|
9
|
+
@t1_1_blueprint.route("/test", methods=["GET", "POST"])
|
10
|
+
async def hello(request):
|
11
|
+
return text("Hello world!")
|
12
|
+
|
13
|
+
|
14
|
+
@t1_1_blueprint.route("/error", methods=["GET", "POST"])
|
15
|
+
async def error(request):
|
16
|
+
raise ServerException(message="Error")
|
17
|
+
|
18
|
+
|
19
|
+
@t1_1_blueprint.route("/restart", methods=["GET", "POST"])
|
20
|
+
async def restart(request: Request):
|
21
|
+
request.app.m.restart(all_workers=True, zero_downtime=True)
|
22
|
+
return text("ok")
|
example/api/t1/t1_2.py
ADDED
example/api/t2/__init__.py
CHANGED
@@ -1 +1 @@
|
|
1
|
-
from .t2 import t2_blueprint
|
1
|
+
from .t2 import t2_blueprint
|
example/api/t2/t2.py
CHANGED
@@ -1,9 +1,9 @@
|
|
1
|
-
from sanic import Blueprint, text
|
2
|
-
|
3
|
-
t2_blueprint = Blueprint("t2_blueprint", url_prefix="/t2")
|
4
|
-
t2_blueprint.ctx.desc = "测试蓝图2"
|
5
|
-
|
6
|
-
|
7
|
-
@t2_blueprint.route("/test", methods=["GET", "POST"])
|
8
|
-
async def hello(request):
|
9
|
-
return text("Hello world!")
|
1
|
+
from sanic import Blueprint, text
|
2
|
+
|
3
|
+
t2_blueprint = Blueprint("t2_blueprint", url_prefix="/t2")
|
4
|
+
t2_blueprint.ctx.desc = "测试蓝图2"
|
5
|
+
|
6
|
+
|
7
|
+
@t2_blueprint.route("/test", methods=["GET", "POST"])
|
8
|
+
async def hello(request):
|
9
|
+
return text("Hello world!")
|
example/api/t2/t3/__init__.py
CHANGED
@@ -1 +1 @@
|
|
1
|
-
from .t3 import t3_blueprint
|
1
|
+
from .t3 import t3_blueprint
|
example/api/t2/t3/t3.py
CHANGED
@@ -1,9 +1,9 @@
|
|
1
|
-
from sanic import Blueprint, text
|
2
|
-
|
3
|
-
t3_blueprint = Blueprint("t3_blueprint", url_prefix="/t3")
|
4
|
-
t3_blueprint.ctx.desc = "测试蓝图3"
|
5
|
-
|
6
|
-
|
7
|
-
@t3_blueprint.route("/test", methods=["GET", "POST"])
|
8
|
-
async def hello(request):
|
9
|
-
return text("Hello world!")
|
1
|
+
from sanic import Blueprint, text
|
2
|
+
|
3
|
+
t3_blueprint = Blueprint("t3_blueprint", url_prefix="/t3")
|
4
|
+
t3_blueprint.ctx.desc = "测试蓝图3"
|
5
|
+
|
6
|
+
|
7
|
+
@t3_blueprint.route("/test", methods=["GET", "POST"])
|
8
|
+
async def hello(request):
|
9
|
+
return text("Hello world!")
|
example/api/user.py
CHANGED
@@ -1,89 +1,89 @@
|
|
1
|
-
from typing import Any, Dict, List
|
2
|
-
from uuid import uuid4
|
3
|
-
|
4
|
-
from sanic import Blueprint, text
|
5
|
-
from sanic.request import Request
|
6
|
-
|
7
|
-
from .validator import (
|
8
|
-
UserAddApi,
|
9
|
-
UserDelApi,
|
10
|
-
UserFindOneApi,
|
11
|
-
UserListApi,
|
12
|
-
UserUpdateApi,
|
13
|
-
)
|
14
|
-
|
15
|
-
users_blueprint = Blueprint("users_blueprint", url_prefix="/users")
|
16
|
-
users_blueprint.ctx.desc = "用户"
|
17
|
-
|
18
|
-
|
19
|
-
@users_blueprint.route("/test", methods=["GET"])
|
20
|
-
async def hello(request):
|
21
|
-
return text("Hello world!")
|
22
|
-
|
23
|
-
|
24
|
-
@users_blueprint.route("/", methods=["POST"])
|
25
|
-
async def user_add(request: Request, api: UserAddApi):
|
26
|
-
"""
|
27
|
-
添加用户
|
28
|
-
"""
|
29
|
-
api.resp.uid = 1
|
30
|
-
return api.json_resp()
|
31
|
-
|
32
|
-
|
33
|
-
@users_blueprint.route("/<uid>", methods=["DELETE"])
|
34
|
-
async def user_del(request: Request, uid: int, api: UserDelApi):
|
35
|
-
"""
|
36
|
-
刪除一个用户
|
37
|
-
"""
|
38
|
-
|
39
|
-
api.resp.uid = uid
|
40
|
-
|
41
|
-
return api.json_resp()
|
42
|
-
|
43
|
-
|
44
|
-
@users_blueprint.route("/<uid>", methods=["PUT"])
|
45
|
-
async def user_update(request: Request, uid: int, api: UserUpdateApi):
|
46
|
-
"""
|
47
|
-
更新一个用户
|
48
|
-
"""
|
49
|
-
|
50
|
-
api.resp.uid = uid
|
51
|
-
|
52
|
-
return api.json_resp()
|
53
|
-
|
54
|
-
|
55
|
-
@users_blueprint.route("/<uid>", methods=["GET"])
|
56
|
-
async def user_one(request: Request, uid: int, api: UserFindOneApi):
|
57
|
-
"""
|
58
|
-
获取一个用户
|
59
|
-
"""
|
60
|
-
|
61
|
-
api.resp.uid = 1
|
62
|
-
api.resp.username = "username"
|
63
|
-
api.resp.password = uuid4().hex
|
64
|
-
|
65
|
-
return api.json_resp()
|
66
|
-
|
67
|
-
|
68
|
-
@users_blueprint.route("/", methods=["GET"])
|
69
|
-
async def user_list(request: Request, api: UserListApi):
|
70
|
-
"""
|
71
|
-
获取一些用户
|
72
|
-
"""
|
73
|
-
|
74
|
-
users: List[Dict[str, Any]] = [
|
75
|
-
{
|
76
|
-
"uid": 1,
|
77
|
-
"username": "user1",
|
78
|
-
},
|
79
|
-
{
|
80
|
-
"uid": 2,
|
81
|
-
"username": "user2",
|
82
|
-
},
|
83
|
-
]
|
84
|
-
for user in users:
|
85
|
-
api.resp.username = user["username"]
|
86
|
-
api.resp.uid = user["uid"]
|
87
|
-
api.resp.add_data()
|
88
|
-
|
89
|
-
return api.json_resp()
|
1
|
+
from typing import Any, Dict, List
|
2
|
+
from uuid import uuid4
|
3
|
+
|
4
|
+
from sanic import Blueprint, text
|
5
|
+
from sanic.request import Request
|
6
|
+
|
7
|
+
from .validator import (
|
8
|
+
UserAddApi,
|
9
|
+
UserDelApi,
|
10
|
+
UserFindOneApi,
|
11
|
+
UserListApi,
|
12
|
+
UserUpdateApi,
|
13
|
+
)
|
14
|
+
|
15
|
+
users_blueprint = Blueprint("users_blueprint", url_prefix="/users")
|
16
|
+
users_blueprint.ctx.desc = "用户"
|
17
|
+
|
18
|
+
|
19
|
+
@users_blueprint.route("/test", methods=["GET"])
|
20
|
+
async def hello(request):
|
21
|
+
return text("Hello world!")
|
22
|
+
|
23
|
+
|
24
|
+
@users_blueprint.route("/", methods=["POST"])
|
25
|
+
async def user_add(request: Request, api: UserAddApi):
|
26
|
+
"""
|
27
|
+
添加用户
|
28
|
+
"""
|
29
|
+
api.resp.uid = 1
|
30
|
+
return api.json_resp()
|
31
|
+
|
32
|
+
|
33
|
+
@users_blueprint.route("/<uid>", methods=["DELETE"])
|
34
|
+
async def user_del(request: Request, uid: int, api: UserDelApi):
|
35
|
+
"""
|
36
|
+
刪除一个用户
|
37
|
+
"""
|
38
|
+
|
39
|
+
api.resp.uid = uid
|
40
|
+
|
41
|
+
return api.json_resp()
|
42
|
+
|
43
|
+
|
44
|
+
@users_blueprint.route("/<uid>", methods=["PUT"])
|
45
|
+
async def user_update(request: Request, uid: int, api: UserUpdateApi):
|
46
|
+
"""
|
47
|
+
更新一个用户
|
48
|
+
"""
|
49
|
+
|
50
|
+
api.resp.uid = uid
|
51
|
+
|
52
|
+
return api.json_resp()
|
53
|
+
|
54
|
+
|
55
|
+
@users_blueprint.route("/<uid>", methods=["GET"])
|
56
|
+
async def user_one(request: Request, uid: int, api: UserFindOneApi):
|
57
|
+
"""
|
58
|
+
获取一个用户
|
59
|
+
"""
|
60
|
+
|
61
|
+
api.resp.uid = 1
|
62
|
+
api.resp.username = "username"
|
63
|
+
api.resp.password = uuid4().hex
|
64
|
+
|
65
|
+
return api.json_resp()
|
66
|
+
|
67
|
+
|
68
|
+
@users_blueprint.route("/", methods=["GET"])
|
69
|
+
async def user_list(request: Request, api: UserListApi):
|
70
|
+
"""
|
71
|
+
获取一些用户
|
72
|
+
"""
|
73
|
+
|
74
|
+
users: List[Dict[str, Any]] = [
|
75
|
+
{
|
76
|
+
"uid": 1,
|
77
|
+
"username": "user1",
|
78
|
+
},
|
79
|
+
{
|
80
|
+
"uid": 2,
|
81
|
+
"username": "user2",
|
82
|
+
},
|
83
|
+
]
|
84
|
+
for user in users:
|
85
|
+
api.resp.username = user["username"]
|
86
|
+
api.resp.uid = user["uid"]
|
87
|
+
api.resp.add_data()
|
88
|
+
|
89
|
+
return api.json_resp()
|
example/api/validator.py
CHANGED
@@ -1,97 +1,97 @@
|
|
1
|
-
from pydantic import BaseModel, Field
|
2
|
-
|
3
|
-
from sanic_api.api import API
|
4
|
-
from sanic_api.api.model import ListRespModel, ResponseModel
|
5
|
-
|
6
|
-
|
7
|
-
class Pagination(BaseModel):
|
8
|
-
"""
|
9
|
-
分页
|
10
|
-
"""
|
11
|
-
|
12
|
-
page: int = Field(1, gt=0, title="页数")
|
13
|
-
per_page: int = Field(20, title="每页数量", gt=0, lt=100)
|
14
|
-
|
15
|
-
|
16
|
-
class ListParam(Pagination):
|
17
|
-
"""
|
18
|
-
列表
|
19
|
-
"""
|
20
|
-
|
21
|
-
orderings: str = Field(None, title="排序字段", description='排序字段,多个字段 ”,“ 分割,倒序使用 "-", 如:"id, -name"')
|
22
|
-
|
23
|
-
|
24
|
-
class UserModel(BaseModel):
|
25
|
-
username: str = Field(title="用户名")
|
26
|
-
password: str = Field(title="密码", description="密码,经过md5加密的")
|
27
|
-
|
28
|
-
|
29
|
-
class AddUserReqModel(BaseModel):
|
30
|
-
"""
|
31
|
-
添加请求参数
|
32
|
-
"""
|
33
|
-
|
34
|
-
user: UserModel = Field(title="用户信息")
|
35
|
-
|
36
|
-
|
37
|
-
class AddUserRespModel(ResponseModel):
|
38
|
-
"""
|
39
|
-
响应参数
|
40
|
-
"""
|
41
|
-
|
42
|
-
uid: int = Field(default=None, title="用户ID", description="创建用户的用户ID")
|
43
|
-
|
44
|
-
|
45
|
-
class FindOneUserRespModel(UserModel, AddUserRespModel):
|
46
|
-
"""
|
47
|
-
响应参数
|
48
|
-
"""
|
49
|
-
|
50
|
-
|
51
|
-
class UserListRespModel(ListRespModel, FindOneUserRespModel):
|
52
|
-
"""
|
53
|
-
用户列表响应参数
|
54
|
-
"""
|
55
|
-
|
56
|
-
|
57
|
-
class UserAddApi(API):
|
58
|
-
"""
|
59
|
-
添加用户API
|
60
|
-
"""
|
61
|
-
|
62
|
-
json_req: AddUserReqModel
|
63
|
-
resp: AddUserRespModel
|
64
|
-
|
65
|
-
|
66
|
-
class UserDelApi(API):
|
67
|
-
"""
|
68
|
-
删除用户API
|
69
|
-
"""
|
70
|
-
|
71
|
-
resp: AddUserRespModel
|
72
|
-
|
73
|
-
|
74
|
-
class UserUpdateApi(UserAddApi):
|
75
|
-
"""
|
76
|
-
更新用户API
|
77
|
-
"""
|
78
|
-
|
79
|
-
json_req: AddUserReqModel
|
80
|
-
resp: AddUserRespModel
|
81
|
-
|
82
|
-
|
83
|
-
class UserFindOneApi(API):
|
84
|
-
"""
|
85
|
-
查询单个用户API
|
86
|
-
"""
|
87
|
-
|
88
|
-
resp: FindOneUserRespModel
|
89
|
-
|
90
|
-
|
91
|
-
class UserListApi(API):
|
92
|
-
"""
|
93
|
-
用户列表API
|
94
|
-
"""
|
95
|
-
|
96
|
-
query_req: ListParam
|
97
|
-
resp: UserListRespModel
|
1
|
+
from pydantic import BaseModel, Field
|
2
|
+
|
3
|
+
from sanic_api.api import API
|
4
|
+
from sanic_api.api.model import ListRespModel, ResponseModel
|
5
|
+
|
6
|
+
|
7
|
+
class Pagination(BaseModel):
|
8
|
+
"""
|
9
|
+
分页
|
10
|
+
"""
|
11
|
+
|
12
|
+
page: int = Field(1, gt=0, title="页数")
|
13
|
+
per_page: int = Field(20, title="每页数量", gt=0, lt=100)
|
14
|
+
|
15
|
+
|
16
|
+
class ListParam(Pagination):
|
17
|
+
"""
|
18
|
+
列表
|
19
|
+
"""
|
20
|
+
|
21
|
+
orderings: str = Field(None, title="排序字段", description='排序字段,多个字段 ”,“ 分割,倒序使用 "-", 如:"id, -name"')
|
22
|
+
|
23
|
+
|
24
|
+
class UserModel(BaseModel):
|
25
|
+
username: str = Field(title="用户名")
|
26
|
+
password: str = Field(title="密码", description="密码,经过md5加密的")
|
27
|
+
|
28
|
+
|
29
|
+
class AddUserReqModel(BaseModel):
|
30
|
+
"""
|
31
|
+
添加请求参数
|
32
|
+
"""
|
33
|
+
|
34
|
+
user: UserModel = Field(title="用户信息")
|
35
|
+
|
36
|
+
|
37
|
+
class AddUserRespModel(ResponseModel):
|
38
|
+
"""
|
39
|
+
响应参数
|
40
|
+
"""
|
41
|
+
|
42
|
+
uid: int = Field(default=None, title="用户ID", description="创建用户的用户ID")
|
43
|
+
|
44
|
+
|
45
|
+
class FindOneUserRespModel(UserModel, AddUserRespModel):
|
46
|
+
"""
|
47
|
+
响应参数
|
48
|
+
"""
|
49
|
+
|
50
|
+
|
51
|
+
class UserListRespModel(ListRespModel, FindOneUserRespModel):
|
52
|
+
"""
|
53
|
+
用户列表响应参数
|
54
|
+
"""
|
55
|
+
|
56
|
+
|
57
|
+
class UserAddApi(API):
|
58
|
+
"""
|
59
|
+
添加用户API
|
60
|
+
"""
|
61
|
+
|
62
|
+
json_req: AddUserReqModel
|
63
|
+
resp: AddUserRespModel
|
64
|
+
|
65
|
+
|
66
|
+
class UserDelApi(API):
|
67
|
+
"""
|
68
|
+
删除用户API
|
69
|
+
"""
|
70
|
+
|
71
|
+
resp: AddUserRespModel
|
72
|
+
|
73
|
+
|
74
|
+
class UserUpdateApi(UserAddApi):
|
75
|
+
"""
|
76
|
+
更新用户API
|
77
|
+
"""
|
78
|
+
|
79
|
+
json_req: AddUserReqModel
|
80
|
+
resp: AddUserRespModel
|
81
|
+
|
82
|
+
|
83
|
+
class UserFindOneApi(API):
|
84
|
+
"""
|
85
|
+
查询单个用户API
|
86
|
+
"""
|
87
|
+
|
88
|
+
resp: FindOneUserRespModel
|
89
|
+
|
90
|
+
|
91
|
+
class UserListApi(API):
|
92
|
+
"""
|
93
|
+
用户列表API
|
94
|
+
"""
|
95
|
+
|
96
|
+
query_req: ListParam
|
97
|
+
resp: UserListRespModel
|
example/app.py
CHANGED
@@ -1,62 +1,62 @@
|
|
1
|
-
from sanic import Sanic
|
2
|
-
from sanic.log import logger
|
3
|
-
|
4
|
-
from example.settings import settings
|
5
|
-
from sanic_api import init_api
|
6
|
-
from sanic_api.utils import auto_blueprint
|
7
|
-
|
8
|
-
|
9
|
-
async def main_process_start(sanic_app: Sanic):
|
10
|
-
"""
|
11
|
-
主进程启动之前调用
|
12
|
-
Args:
|
13
|
-
sanic_app: application
|
14
|
-
|
15
|
-
Returns:
|
16
|
-
|
17
|
-
"""
|
18
|
-
sanic_cfg = settings.sanic.dict(by_alias=True)
|
19
|
-
sanic_app.config.update(sanic_cfg)
|
20
|
-
logger.info(f"{sanic_app.name} 服务启动")
|
21
|
-
|
22
|
-
|
23
|
-
async def main_process_stop(sanic_app: Sanic):
|
24
|
-
"""
|
25
|
-
主进程停止之后调用
|
26
|
-
Args:
|
27
|
-
sanic_app: application
|
28
|
-
|
29
|
-
Returns:
|
30
|
-
|
31
|
-
"""
|
32
|
-
|
33
|
-
logger.info(f"{sanic_app.name} 服务停止")
|
34
|
-
|
35
|
-
|
36
|
-
async def before_server_start(sanic_app: Sanic):
|
37
|
-
"""
|
38
|
-
worker启动之前调用
|
39
|
-
Args:
|
40
|
-
sanic_app: application
|
41
|
-
|
42
|
-
Returns:
|
43
|
-
|
44
|
-
"""
|
45
|
-
logger.debug(f"Worler {sanic_app.m.pid} 启动")
|
46
|
-
|
47
|
-
|
48
|
-
def app_factory():
|
49
|
-
"""
|
50
|
-
app 工厂方法
|
51
|
-
Returns:
|
52
|
-
|
53
|
-
"""
|
54
|
-
app = Sanic(name="test", configure_logging=False)
|
55
|
-
app.main_process_start(main_process_start)
|
56
|
-
app.main_process_stop(main_process_stop)
|
57
|
-
app.before_server_start(before_server_start)
|
58
|
-
|
59
|
-
auto_blueprint(app, "
|
60
|
-
init_api(app)
|
61
|
-
|
62
|
-
return app
|
1
|
+
from sanic import Sanic
|
2
|
+
from sanic.log import logger
|
3
|
+
|
4
|
+
from example.settings import settings
|
5
|
+
from sanic_api import init_api
|
6
|
+
from sanic_api.utils import auto_blueprint
|
7
|
+
|
8
|
+
|
9
|
+
async def main_process_start(sanic_app: Sanic):
|
10
|
+
"""
|
11
|
+
主进程启动之前调用
|
12
|
+
Args:
|
13
|
+
sanic_app: application
|
14
|
+
|
15
|
+
Returns:
|
16
|
+
|
17
|
+
"""
|
18
|
+
sanic_cfg = settings.sanic.dict(by_alias=True)
|
19
|
+
sanic_app.config.update(sanic_cfg)
|
20
|
+
logger.info(f"{sanic_app.name} 服务启动")
|
21
|
+
|
22
|
+
|
23
|
+
async def main_process_stop(sanic_app: Sanic):
|
24
|
+
"""
|
25
|
+
主进程停止之后调用
|
26
|
+
Args:
|
27
|
+
sanic_app: application
|
28
|
+
|
29
|
+
Returns:
|
30
|
+
|
31
|
+
"""
|
32
|
+
|
33
|
+
logger.info(f"{sanic_app.name} 服务停止")
|
34
|
+
|
35
|
+
|
36
|
+
async def before_server_start(sanic_app: Sanic):
|
37
|
+
"""
|
38
|
+
worker启动之前调用
|
39
|
+
Args:
|
40
|
+
sanic_app: application
|
41
|
+
|
42
|
+
Returns:
|
43
|
+
|
44
|
+
"""
|
45
|
+
logger.debug(f"Worler {sanic_app.m.pid} 启动")
|
46
|
+
|
47
|
+
|
48
|
+
def app_factory():
|
49
|
+
"""
|
50
|
+
app 工厂方法
|
51
|
+
Returns:
|
52
|
+
|
53
|
+
"""
|
54
|
+
app = Sanic(name="test", configure_logging=False)
|
55
|
+
app.main_process_start(main_process_start)
|
56
|
+
app.main_process_stop(main_process_stop)
|
57
|
+
app.before_server_start(before_server_start)
|
58
|
+
|
59
|
+
auto_blueprint(app, "api")
|
60
|
+
init_api(app)
|
61
|
+
|
62
|
+
return app
|
example/settings.py
CHANGED
@@ -1,10 +1,10 @@
|
|
1
|
-
from sanic_api.config import DefaultSettings
|
2
|
-
|
3
|
-
|
4
|
-
class Settings(DefaultSettings):
|
5
|
-
"""
|
6
|
-
设置类
|
7
|
-
"""
|
8
|
-
|
9
|
-
|
10
|
-
settings = Settings()
|
1
|
+
from sanic_api.config import DefaultSettings
|
2
|
+
|
3
|
+
|
4
|
+
class Settings(DefaultSettings):
|
5
|
+
"""
|
6
|
+
设置类
|
7
|
+
"""
|
8
|
+
|
9
|
+
|
10
|
+
settings = Settings()
|