one get/post handler for add/remove/info

This commit is contained in:
j 2014-09-01 14:30:26 +02:00
parent cc47d33c5e
commit df7b5acf91
2 changed files with 30 additions and 29 deletions

View file

@ -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,8 +86,9 @@ class RequestHandler(ProxyHandler):
render_json(self, {'status': 'offline'}) render_json(self, {'status': 'offline'})
return None return None
def run(): def run(root_dir=None):
root_dir = os.path.normpath(os.path.join(os.path.abspath(os.path.dirname(__file__)), '..')) if not root_dir:
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),
] ]

View file

@ -20,13 +20,13 @@ tls_key_path = os.path.join(config_path, 'node.tls.key')
defaults = { defaults = {
"address": "::1", "address": "::1",
"port": 8842, "port": 8842,
"node_address": "", "node_address": "",
"node_port": 8851, "node_port": 8851,
"cert": "", "cert": "",
"directory_service": "http://[2a01:4f8:120:3201::3]:25519", "directory_service": "http://[2a01:4f8:120:3201::3]:25519",
"localnode_discovery": True "localnode_discovery": True
} }
server = pdict(os.path.join(config_path, 'settings.json'), defaults) server = pdict(os.path.join(config_path, 'settings.json'), defaults)
services = pdict(os.path.join(config_path, 'services.json'), {}) services = pdict(os.path.join(config_path, 'services.json'), {})