REST API, bugs
This commit is contained in:
Pavel Loginov
2019-10-31 22:51:43 +03:00
parent b5faaede1b
commit 1b728e72aa
14 changed files with 256 additions and 35 deletions

View File

@@ -5,7 +5,7 @@ sys.path.append(os.path.dirname(__file__))
sys.path.append(os.path.join(sys.path[0], '/var/www/haproxy-wi/app/'))
os.chdir(os.path.dirname(__file__))
from bottle import route, run, template, hook, response, request
from bottle import route, run, template, hook, response, request, error
import sql
import funct
import api_funct
@@ -46,6 +46,11 @@ def enable_cors():
response.headers['Access-Control-Allow-Headers'] = _allow_headers
@error(500)
def error_handler_500(error):
return json.dumps({"status": "error", "message": str(error.exception)})
@route('/', method=['GET', 'POST'])
@route('/help', method=['GET', 'POST'])
def index():
@@ -53,14 +58,19 @@ def index():
return dict(error=_error_auth)
data = {
'help': 'show all available endpoints',
'servers':'show info about all servers',
'server/<id,hostname,ip>':'show info about server by id or hostname or ip',
'servers/status':'show status all servers',
'server/<id,hostname,ip>':'show info about the server by id or hostname or ip',
'server/<id,hostname,ip>/status':'show HAProxy status by id or hostname or ip',
'server/<id,hostname,ip>/runtime':'exec HAProxy runtime commands by id or hostname or ip',
'server/<id,hostname,ip>/backends':'show backends by id or hostname or ip',
'server/<id,hostname,ip>/action/start':'start HAProxy service by id or hostname or ip',
'server/<id,hostname,ip>/action/stop':'stop HAProxy service by id or hostname or ip',
'server/<id,hostname,ip>/action/restart':'restart HAProxy service by id or hostname or ip'
'server/<id,hostname,ip>/action/restart':'restart HAProxy service by id or hostname or ip',
'server/<id,hostname,ip>/config/get':'get HAProxy config from the server by id or hostname or ip',
'server/<id,hostname,ip>/config/send':'send HAProxy config to the server by id or hostname or ip. Has to have config header with config and action header for action after upload. Action header accepts next value: save, test, reload and restart. May be empty for just save',
'server/<id,hostname,ip>/config/add':'add section to the HAProxy config by id or hostname or ip. Has to have config header with section and action header for action after upload. Action header accepts next value: save, test, reload and restart. May be empty for just save'
}
return dict(help=data)
@@ -91,6 +101,12 @@ def get_servers():
return dict(servers=data)
@route('/servers/status', method=['GET', 'POST'])
def callback():
if not check_login():
return dict(error=_error_auth)
return api_funct.get_all_statuses()
@route('/server/<id>', method=['GET', 'POST'])
@route('/server/<id:int>', method=['GET', 'POST'])
def callback(id):
@@ -129,4 +145,28 @@ def callback(id):
if not check_login():
return dict(error=_error_auth)
return api_funct.show_backends(id)
@route('/server/<id>/config/get', method=['GET', 'POST'])
@route('/server/<id:int>/config/get', method=['GET', 'POST'])
def callback(id):
if not check_login():
return dict(error=_error_auth)
return api_funct.get_config(id)
@route('/server/<id>/config/send', method=['GET', 'POST'])
@route('/server/<id:int>/config/send', method=['GET', 'POST'])
def callback(id):
if not check_login():
return dict(error=_error_auth)
return api_funct.upload_config(id)
@route('/server/<id>/config/add', method=['GET', 'POST'])
@route('/server/<id:int>/config/add', method=['GET', 'POST'])
def callback(id):
if not check_login():
return dict(error=_error_auth)
return api_funct.add_to_config(id)