one get/post handler for add/remove/info
This commit is contained in:
parent
cc47d33c5e
commit
df7b5acf91
2 changed files with 30 additions and 29 deletions
|
@ -29,34 +29,35 @@ def render_json(handler, response):
|
||||||
handler.write(response)
|
handler.write(response)
|
||||||
handler.finish()
|
handler.finish()
|
||||||
|
|
||||||
class StatusHandler(tornado.web.RequestHandler):
|
class ServiceHandler(tornado.web.RequestHandler):
|
||||||
|
|
||||||
def get(self, action):
|
def handle(self, action):
|
||||||
response = {}
|
response = {}
|
||||||
if action == 'info':
|
if self.request.body:
|
||||||
|
data = json.loads(self.request.body)
|
||||||
|
else:
|
||||||
|
data = {}
|
||||||
|
if action == 'add':
|
||||||
|
settings.services[data['name']] = data['url']
|
||||||
|
response = {'status': 200}
|
||||||
|
elif action == 'remove':
|
||||||
|
if data['name'] in settings.services:
|
||||||
|
del settings.services[data['name']]
|
||||||
|
response = {'status': 200}
|
||||||
|
elif action == 'info':
|
||||||
response['id'] = settings.USER_ID
|
response['id'] = settings.USER_ID
|
||||||
response['online'] = state.online
|
response['online'] = state.online
|
||||||
response.update(state.nodes.info())
|
response.update(state.nodes.info())
|
||||||
else:
|
else:
|
||||||
response['error'] = 'unknown action'
|
self.set_status(500)
|
||||||
|
response = {'error': 'unknown action'}
|
||||||
return render_json(self, response)
|
return render_json(self, response)
|
||||||
|
|
||||||
class ServiceHandler(tornado.web.RequestHandler):
|
def get(self, action):
|
||||||
|
return self.handle(action)
|
||||||
|
|
||||||
def post(self, action):
|
def post(self, action):
|
||||||
data = json.loads(self.request.body)
|
return self.handle(action)
|
||||||
if action == 'add':
|
|
||||||
settings.services[data['name']] = data['url']
|
|
||||||
response = json.dumps({'status': 200})
|
|
||||||
elif action == 'remove':
|
|
||||||
if data['name'] in settings.services:
|
|
||||||
del settings.services[data['name']]
|
|
||||||
response = json.dumps({'status': 200})
|
|
||||||
else:
|
|
||||||
self.set_status(500)
|
|
||||||
response = json.dumps({'error': 'unknown action'})
|
|
||||||
self.write(response)
|
|
||||||
self.finish()
|
|
||||||
|
|
||||||
class RequestHandler(ProxyHandler):
|
class RequestHandler(ProxyHandler):
|
||||||
|
|
||||||
|
@ -85,7 +86,8 @@ class RequestHandler(ProxyHandler):
|
||||||
render_json(self, {'status': 'offline'})
|
render_json(self, {'status': 'offline'})
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def run():
|
def run(root_dir=None):
|
||||||
|
if not root_dir:
|
||||||
root_dir = os.path.normpath(os.path.join(os.path.abspath(os.path.dirname(__file__)), '..'))
|
root_dir = os.path.normpath(os.path.join(os.path.abspath(os.path.dirname(__file__)), '..'))
|
||||||
os.chdir(root_dir)
|
os.chdir(root_dir)
|
||||||
PID = sys.argv[1] if len(sys.argv) > 1 else None
|
PID = sys.argv[1] if len(sys.argv) > 1 else None
|
||||||
|
@ -97,8 +99,7 @@ def run():
|
||||||
'debug': False,
|
'debug': False,
|
||||||
}
|
}
|
||||||
handlers = [
|
handlers = [
|
||||||
(r'/(info)', StatusHandler),
|
(r'/(add|remove|info)', ServiceHandler),
|
||||||
(r'/(add|remove)', ServiceHandler),
|
|
||||||
(r".*", RequestHandler),
|
(r".*", RequestHandler),
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue