-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_server.py
93 lines (78 loc) · 2.73 KB
/
test_server.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
81
82
83
84
85
86
87
88
89
90
91
92
import sys
import logging
from laka import LakaServer, Param, Handler, HandlerFailed, HandlerOK
from laka.errors import ValidateError, HandlerNotFound, InvalidHandler, \
InvalidMessage, MakeCommandError, MakeResponseError, MakeHandlerResponseError
# 定义命令
COMMAND_CREATE_USER = 101
# 返回码定义
SUCCESS = 0 # 成功
COMMAND_NOT_FOUND = 1 # 未找到命令
VALIDATE_PARAM_FAILED = 10 # 参数错误
INTERNAL_SERVER_ERROR = 500 # 服务器内部错误
# 返回码对应的提示信息
RESPONSE_MESSAGE = {
SUCCESS: "",
COMMAND_NOT_FOUND: "Command not found.",
VALIDATE_PARAM_FAILED: "Failed to validate params",
INTERNAL_SERVER_ERROR: "Internal Server Error",
}
HandlerOK.set_success_code(SUCCESS)
# 参数
class CreateUserParam(Param):
def __init__(self):
self.account = None
self.password = None
self.tel = None
def validate(self):
"""
接收到消息之后,会自动调用 validate 验证参数是否合法
"""
if not (self.account and self.password):
return False
return True
# handler,用来处理请求
class CreateUserHandler(Handler):
Param = CreateUserParam
def handle(self):
user = {"password":self.param.password, "account":self.param.account}
return HandlerOK(user)
if __name__ == "__main__":
laka_server = LakaServer(
service_name="lakaTest",
redis_host="localhost",
redis_port=6379,
redis_queue="laka_request",
fofo_host="10.88.190.211",
fofo_port=6379,
response_message=RESPONSE_MESSAGE,
check_health=True,
)
try:
laka_server.router(COMMAND_CREATE_USER, CreateUserHandler)
except InvalidHandler as e:
logging.error(e)
sys.exit(1)
try:
for queue, cmd in laka_server.accept_request():
try:
print("cmd = ", cmd.json())
handler_response = laka_server.handle(cmd)
except ValidateError as e:
logging.error(e)
handler_response = HandlerFailed(VALIDATE_PARAM_FAILED)
except MakeHandlerResponseError as e:
logging.error(e)
handler_response = HandlerFailed(INTERNAL_SERVER_ERROR)
except HandlerNotFound as e:
logging.error(e)
handler_response = HandlerFailed(COMMAND_NOT_FOUND)
try:
laka_server.response(cmd.request_id, handler_response)
except MakeResponseError as e:
logging.error(e)
break
except MakeCommandError as e:
logging.error(e)
except InvalidMessage as e:
logging.error(e)