From 6f4b2e0f6900661aa7416abe73f2816bbc21fc57 Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Thu, 12 Sep 2019 16:19:21 +0800 Subject: [PATCH] 1 --- f7/app.py | 45 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 39 insertions(+), 6 deletions(-) diff --git a/f7/app.py b/f7/app.py index 4941bd8..23d2df3 100644 --- a/f7/app.py +++ b/f7/app.py @@ -1,18 +1,17 @@ # -*- coding: utf-8 -*- #!/usr/bin/python +import f7 + +import json +import traceback import tornado.web import tornado.ioloop class _mainHandler(tornado.web.RequestHandler): def get(self): - self.write(json.dumps({ - 'errcode': 0, - 'errmsg': '', - 'healthy': 1, - 'max_rundelay': 10 - })) + instance()._dispatchRequest(self) class _App: _instance = None @@ -24,9 +23,15 @@ class _App: return cls._instance def __init__(self): + self._requestHandler = {} self._app = tornado.web.Application([ (r"/webapp/index[\.]php", _mainHandler), ]) + self.registerHandler('Ops', 'selfChecking', self.__selfChecking) + + def init(self, log_dir): + f7.udplog.init() + f7.udplog.setLogDirAndCreate(log_dir) def start(self): tornado.ioloop.IOLoop.current().start() @@ -37,5 +42,33 @@ class _App: def callAt(self, when, callback): tornado.ioloop.IOLoop.current().call_at(when, callback) + def registerHandler(self, c, a, callback): + self._requestHandler[c + '$' + a] = callback + + def _dispatchRequest(self, request): + c = request.get_argument('c', '') + a = request.get_argument('a', '') + handler = self._requestHandler.get(c + '$' + a, None) + if not handler: + request.write('') + return + try: + response = handler(lambda param, def_val = '': request.get_argument(param, def_val)) + request.write(response) + except Exception as e: + f7.udplog.error('dispatch request ' + str(e)) + request.write(json.dumps({ + 'errcode': 200, + 'errmsg': '服务内部错误', + })) + + def __selfChecking(self, request): + return json.dumps({ + 'errcode': 0, + 'errmsg': '', + 'healthy': 1, + 'max_rundelay': 10 + }) + def instance(): return _App.instance()