主要作路由,限流,鑑權等接入放行性質的工做python
class App: action_map = {} def dispatch(self, data): try: assert data["action"] in self.action_map, ErrorCode.sys_action_not_exist action = self.action_map[data["action"]] return { "code": 0, "msg": "", "data": action.run(data["params"]) } except AssertionError as e: msg, code = e return { "code": code, "msg": msg, "data": None } except Exception: traceback.print_exc() msg, code = ErrorCode.sys_unknown return { "code": code, "msg": msg, "data": None }
名稱到動做類的映射數據庫
action_map = { 'register': RegisterAction }
表達一個接口的實際業務邏輯操做,權限,數據schema都在這裏進行配置數據結構
class RegisterAction(Action): schema = Schema({ "verify_code": And(str, len), "phone": And(str, len), "password": And(str, len) }) auth = False def run(self, data): assert check_verify_code(data["verify_code"]), MyErrorCode.verify_code_wrong assert User.get(data["phone"]), MyErrorCode.already_register return {"token": "token"}
操做順序spa