allow configuring REST API Host, have default public and local ports

This commit is contained in:
jesopo 2019-09-15 11:37:32 +01:00
parent 769d4e1a28
commit 4fb538f486

View file

@ -5,6 +5,9 @@
import http.server, json, socket, ssl, threading, uuid, urllib.parse import http.server, json, socket, ssl, threading, uuid, urllib.parse
from src import ModuleManager, utils from src import ModuleManager, utils
DEFAULT_PORT = 5001
DEFAULT_PUBLIC_PORT = 5000
class Response(object): class Response(object):
def __init__(self, compact=False): def __init__(self, compact=False):
self._compact = compact self._compact = compact
@ -75,16 +78,22 @@ class Handler(http.server.BaseHTTPRequestHandler):
return _bot.get_setting("rest-api-minify", False) return _bot.get_setting("rest-api-minify", False)
def url_for(self, headers, route, endpoint, get_params={}): def url_for(self, headers, route, endpoint, get_params={}):
if "Host" in headers: config_host = _bot.get_setting("rest-api-host", None)
host, _, port = headers["Host"].partition(":")
if not port:
port = _bot.config.get("api-port", "5001")
host = None
if not config_host == None:
host = config_host
elif "Host" in headers:
header_host, _, port = headers["Host"].partition(":")
if not port:
port = _bot.config.get("api-port", DEFAULT_PUBLIC_PORT)
host = "%s:%s" % (header_host, port)
if host:
get_params_str = "" get_params_str = ""
if get_params: if get_params:
get_params_str = "?%s" % urllib.parse.urlencode(get_params) get_params_str = "?%s" % urllib.parse.urlencode(get_params)
return "%s:%s/%s/%s%s" % (host, port, route, endpoint, return "%s/%s/%s%s" % (host, route, endpoint, get_params_str)
get_params_str)
else: else:
return None return None
def _url_for(self, headers): def _url_for(self, headers):
@ -161,6 +170,8 @@ class BitBotIPv6HTTPd(http.server.HTTPServer):
utils.BoolSetting("rest-api", "Enable/disable REST API")) utils.BoolSetting("rest-api", "Enable/disable REST API"))
@utils.export("botset", @utils.export("botset",
utils.BoolSetting("rest-api-minify", "Enable/disable REST API minifying")) utils.BoolSetting("rest-api-minify", "Enable/disable REST API minifying"))
@utils.export("botset",
utils.Setting("rest-api-host", "Public hostname:port for the REST API"))
class Module(ModuleManager.BaseModule): class Module(ModuleManager.BaseModule):
def on_load(self): def on_load(self):
global _bot global _bot
@ -174,7 +185,7 @@ class Module(ModuleManager.BaseModule):
self.httpd = None self.httpd = None
if self.bot.get_setting("rest-api", False): if self.bot.get_setting("rest-api", False):
port = int(self.bot.config.get("api-port", "5000")) port = int(self.bot.config.get("api-port", str(DEFAULT_PORT)))
self.httpd = BitBotIPv6HTTPd(("::1", port), Handler) self.httpd = BitBotIPv6HTTPd(("::1", port), Handler)
self.thread = threading.Thread(target=self.httpd.serve_forever) self.thread = threading.Thread(target=self.httpd.serve_forever)