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.finish()
class StatusHandler(tornado.web.RequestHandler):
class ServiceHandler(tornado.web.RequestHandler):
def get(self, action):
def handle(self, action):
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['online'] = state.online
response.update(state.nodes.info())
else:
response['error'] = 'unknown action'
self.set_status(500)
response = {'error': 'unknown action'}
return render_json(self, response)
class ServiceHandler(tornado.web.RequestHandler):
def get(self, action):
return self.handle(action)
def post(self, action):
data = json.loads(self.request.body)
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()
return self.handle(action)
class RequestHandler(ProxyHandler):
@ -85,8 +86,9 @@ class RequestHandler(ProxyHandler):
render_json(self, {'status': 'offline'})
return None
def run():
root_dir = os.path.normpath(os.path.join(os.path.abspath(os.path.dirname(__file__)), '..'))
def run(root_dir=None):
if not root_dir:
root_dir = os.path.normpath(os.path.join(os.path.abspath(os.path.dirname(__file__)), '..'))
os.chdir(root_dir)
PID = sys.argv[1] if len(sys.argv) > 1 else None
@ -97,8 +99,7 @@ def run():
'debug': False,
}
handlers = [
(r'/(info)', StatusHandler),
(r'/(add|remove)', ServiceHandler),
(r'/(add|remove|info)', ServiceHandler),
(r".*", RequestHandler),
]

View file

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