|
@ -1,16 +1,15 @@ |
|
|
#!/usr/bin/env python3 |
|
|
#!/usr/bin/env python3 |
|
|
# -*- coding: utf-8 -*- |
|
|
# -*- coding: utf-8 -*- |
|
|
|
|
|
|
|
|
|
|
|
import os |
|
|
import json |
|
|
import json |
|
|
from gevent import pywsgi |
|
|
from gevent import pywsgi |
|
|
from Checker import formatCheck |
|
|
from Checker import formatCheck |
|
|
from Basis.Logger import logging |
|
|
from Basis.Logger import logging |
|
|
from Basis.Manager import Manager |
|
|
from Basis.Manager import Manager |
|
|
from Basis.Constant import Version |
|
|
|
|
|
from flask import Flask, Response, request |
|
|
from flask import Flask, Response, request |
|
|
|
|
|
from Basis.Constant import ApiPort, ApiPath, ApiToken, Version |
|
|
|
|
|
|
|
|
token = '' |
|
|
|
|
|
webPath = '/' # root of api server |
|
|
|
|
|
webApi = Flask(__name__) # init flask server |
|
|
webApi = Flask(__name__) # init flask server |
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -43,16 +42,16 @@ def genError(message: str) -> Response: |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def tokenCheck() -> bool: |
|
|
def tokenCheck() -> bool: |
|
|
if token == '': return True # without token check |
|
|
if ApiToken == '': return True # without token check |
|
|
if request.method == 'GET': |
|
|
if request.method == 'GET': |
|
|
return request.args.get('token') == token |
|
|
return request.args.get('token') == ApiToken |
|
|
elif request.method == 'POST': |
|
|
elif request.method == 'POST': |
|
|
return request.json.get('token') == token |
|
|
return request.json.get('token') == ApiToken |
|
|
else: |
|
|
else: |
|
|
return False # polyfill |
|
|
return False # polyfill |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@webApi.route('/task', methods = ['GET']) |
|
|
@webApi.route(os.path.join(ApiPath, 'task'), methods = ['GET']) |
|
|
def getTaskList() -> Response: |
|
|
def getTaskList() -> Response: |
|
|
if not tokenCheck(): # token check |
|
|
if not tokenCheck(): # token check |
|
|
return genError('Invalid token') |
|
|
return genError('Invalid token') |
|
@ -64,7 +63,7 @@ def getTaskList() -> Response: |
|
|
}) |
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@webApi.route('/task', methods = ['POST']) |
|
|
@webApi.route(os.path.join(ApiPath, 'task'), methods = ['POST']) |
|
|
def createTask() -> Response: |
|
|
def createTask() -> Response: |
|
|
if not tokenCheck(): # token check |
|
|
if not tokenCheck(): # token check |
|
|
return genError('Invalid token') |
|
|
return genError('Invalid token') |
|
@ -93,7 +92,7 @@ def createTask() -> Response: |
|
|
}) |
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@webApi.route('/task/<taskId>', methods = ['GET']) |
|
|
@webApi.route(os.path.join(ApiPath, 'task/<taskId>'), methods = ['GET']) |
|
|
def getTaskInfo(taskId: str) -> Response: |
|
|
def getTaskInfo(taskId: str) -> Response: |
|
|
if not tokenCheck(): # token check |
|
|
if not tokenCheck(): # token check |
|
|
return genError('Invalid token') |
|
|
return genError('Invalid token') |
|
@ -106,7 +105,7 @@ def getTaskInfo(taskId: str) -> Response: |
|
|
}) |
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@webApi.route('/version', methods = ['GET']) |
|
|
@webApi.route(os.path.join(ApiPath, 'version'), methods = ['GET']) |
|
|
def getVersion() -> Response: |
|
|
def getVersion() -> Response: |
|
|
logging.debug('API get version -> %s' + Version) |
|
|
logging.debug('API get version -> %s' + Version) |
|
|
return jsonResponse({ |
|
|
return jsonResponse({ |
|
@ -115,9 +114,7 @@ def getVersion() -> Response: |
|
|
}) |
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def startServer(apiToken: str = '', apiPort: int = 7839) -> None: |
|
|
def startServer() -> None: |
|
|
global token |
|
|
logging.warning('API server at http://:%i/' % ApiPort) |
|
|
token = apiToken # api token (default empty) |
|
|
logging.warning('API ' + ('without token' if ApiToken == '' else 'token -> %s' % ApiToken)) |
|
|
logging.warning('API server at http://:%i/' % apiPort) |
|
|
pywsgi.WSGIServer(('0.0.0.0', ApiPort), webApi).serve_forever() # powered by gevent |
|
|
logging.warning('API ' + ('without token' if apiToken == '' else 'token -> %s' % apiToken)) |
|
|
|
|
|
pywsgi.WSGIServer(('0.0.0.0', apiPort), webApi).serve_forever() # powered by gevent |
|
|
|
|
|