working statistics api route

This commit is contained in:
kevdagoat 2020-09-06 12:58:17 +08:00
parent 1f184d3e94
commit e6bf64a41f
3 changed files with 32 additions and 8 deletions

View File

@ -171,14 +171,17 @@ class db_builder:
username = default_data.get("username", 'admin') username = default_data.get("username", 'admin')
password = default_data.get("password", 'crafty') password = default_data.get("password", 'crafty')
api_token = helper.random_string_generator(32)
Users.insert({ Users.insert({
Users.username: username, Users.username: username,
Users.password: helper.encode_pass(password), Users.password: helper.encode_pass(password),
Users.api_token: helper.random_string_generator(32), Users.api_token: api_token,
Users.enabled: True Users.enabled: True
}).execute() }).execute()
console.info("API token is {}".format(api_token))
@staticmethod @staticmethod
def is_fresh_install(): def is_fresh_install():
if helper.check_file_exists(helper.db_path): if helper.check_file_exists(helper.db_path):

View File

@ -6,6 +6,7 @@ import tornado.escape
import logging import logging
from app.classes.shared.models import Users from app.classes.shared.models import Users
from app.classes.minecraft.stats import stats
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@ -21,20 +22,37 @@ class BaseHandler(tornado.web.RequestHandler):
self.set_status(403) self.set_status(403)
self.finish(self.return_response(403, {'error':'ACCESS_DENIED', 'info':'You were denied access to the requested resource'})) self.finish(self.return_response(403, {'error':'ACCESS_DENIED', 'info':'You were denied access to the requested resource'}))
def authenticate_user(self, token): def authenticate_user(self):
try: try:
log.debug("Searching for specified token") log.debug("Searching for specified token")
user_data = Users.get(api_token=token) user_data = Users.get(api_token=self.get_argument('token'))
log.debug("Checking results") log.debug("Checking results")
if user_data: if user_data:
# Login successful! Return the username # Login successful! Check perms
log.info("User {} has authenticated to API".format(user_data.username)) log.info("User {} has authenticated to API".format(user_data.username))
return user_data.username # TODO: Role check
else: else:
logging.debug("Auth unsuccessful") logging.debug("Auth unsuccessful")
return None return self.access_denied("unknown")
except: except:
log.warning("Traceback occurred when authenticating user to API. Most likely wrong token") log.warning("Traceback occurred when authenticating user to API. Most likely wrong token")
return None return self.access_denied("unknown")
pass pass
class ServersStats(BaseHandler):
def get(self):
"""Get details about all servers"""
self.authenticate_user()
# Get server stats
self.finish(self.write({"servers": stats.get_servers_stats()}))
class NodeStats(BaseHandler):
def get(self):
"""Get stats for particular node"""
self.authenticate_user()
# Get node stats
node_stats = stats.get_node_stats()
node_stats.pop("servers")
self.finish(self.write(node_stats))

View File

@ -23,6 +23,7 @@ try:
from app.classes.web.default_handler import DefaultHandler from app.classes.web.default_handler import DefaultHandler
from app.classes.web.server_handler import ServerHandler from app.classes.web.server_handler import ServerHandler
from app.classes.web.ajax_handler import AjaxHandler from app.classes.web.ajax_handler import AjaxHandler
from app.classes.web.api_handler import ServersStats, NodeStats
except ModuleNotFoundError as e: except ModuleNotFoundError as e:
logger.critical("Import Error: Unable to load {} module".format(e, e.name)) logger.critical("Import Error: Unable to load {} module".format(e, e.name))
@ -122,6 +123,8 @@ class webserver:
(r'/panel/(.*)', PanelHandler), (r'/panel/(.*)', PanelHandler),
(r'/server/(.*)', ServerHandler), (r'/server/(.*)', ServerHandler),
(r'/ajax/(.*)', AjaxHandler), (r'/ajax/(.*)', AjaxHandler),
(r'/api/stats/servers', ServersStats),
(r'/api/stats/node', NodeStats),
] ]
app = tornado.web.Application( app = tornado.web.Application(