diff options
author | Jorik Schellekens <joriks@matrix.org> | 2019-08-23 17:52:44 +0100 |
---|---|---|
committer | Jorik Schellekens <joriks@matrix.org> | 2019-08-28 15:59:54 +0100 |
commit | 39f758c07c6c555941a3a621a45eca36f5996e6e (patch) | |
tree | 6f3631f7af3797865ff6b2f9a5ae0ea20461faae | |
parent | When I'm alone I count myslef. (diff) | |
download | synapse-39f758c07c6c555941a3a621a45eca36f5996e6e.tar.xz |
It's an OO language. Use it like one.
-rwxr-xr-x | synapse_topology/__init__.py | 15 | ||||
-rw-r--r-- | synapse_topology/model/__init__.py | 9 | ||||
-rw-r--r-- | synapse_topology/model/errors.py | 3 | ||||
-rw-r--r-- | synapse_topology/server/__init__.py | 8 | ||||
-rw-r--r-- | synapse_topology/server/error_handlers.py | 80 | ||||
-rw-r--r-- | synapse_topology/server/server.py | 170 |
6 files changed, 135 insertions, 150 deletions
diff --git a/synapse_topology/__init__.py b/synapse_topology/__init__.py index 9b8d2d0755..a4164ada33 100755 --- a/synapse_topology/__init__.py +++ b/synapse_topology/__init__.py @@ -3,8 +3,8 @@ import argparse import os.path as path import sys -from synapse_topology.server import app -import synapse_topology.model as model +from synapse_topology.server import Server +from synapse_topology.model import Model from twisted.internet import endpoints, reactor from twisted.web.server import Site @@ -33,13 +33,8 @@ if not path.isdir(args.config_dir): exit(1) -model.set_config_dir(args.config_dir) +model = Model(args.config_dir) +server = Server(model) -backend_endpoint = endpoints.serverFromString( - reactor, "tcp6:port=8888:interface=localhost" -) -backend_endpoint.listen(Site(app.resource())) - - -reactor.run() +server.app.run("localhost", 8888) diff --git a/synapse_topology/model/__init__.py b/synapse_topology/model/__init__.py index 327242feec..2dcca83396 100644 --- a/synapse_topology/model/__init__.py +++ b/synapse_topology/model/__init__.py @@ -10,7 +10,7 @@ from .errors import BaseConfigInUseError, ConfigNotFoundError, ServernameNotSetE from .config import create_config from .util import is_subpath -from synapse.config import find_config_files, read_config_files +# from synapse.config import find_config_files, read_config_files class Model: @@ -70,9 +70,10 @@ class Model: the config is in use. """ try: - return read_config_files(find_config_files(self.config_dir)).get( - CONFIG_LOCK, True - ) + pass + # return read_config_files(find_config_files(self.config_dir)).get( + # CONFIG_LOCK, True + # ) except FileNotFoundError: return False diff --git a/synapse_topology/model/errors.py b/synapse_topology/model/errors.py index cbe9c5d819..f2b68a1564 100644 --- a/synapse_topology/model/errors.py +++ b/synapse_topology/model/errors.py @@ -1,6 +1,3 @@ -from .constants import BASE_CONFIG - - class ConfigNotFoundError(FileNotFoundError): def __init__(self, config_name): self.config_name = config_name diff --git a/synapse_topology/server/__init__.py b/synapse_topology/server/__init__.py index 05de65ead3..25244f4d11 100644 --- a/synapse_topology/server/__init__.py +++ b/synapse_topology/server/__init__.py @@ -1,7 +1,3 @@ -from klein import Klein - -app = Klein() - -from . import server -from . import error_handlers +# from .error_handlers import ErrorHandledServer as Server +from .error_handlers import ErrorHandledServer as Server diff --git a/synapse_topology/server/error_handlers.py b/synapse_topology/server/error_handlers.py index f43083322d..d299796bf6 100644 --- a/synapse_topology/server/error_handlers.py +++ b/synapse_topology/server/error_handlers.py @@ -1,47 +1,47 @@ from jsonschema import ValidationError from simplejson.errors import JSONDecodeError from synapse_topology.model.errors import ( - BasConfigInUseError, + BaseConfigInUseError, ConfigNotFoundError, ServernameNotSetError, ) -from . import app - - -@app.handle_errors(ValidationError) -def validation_error(request, failure): - request.setResponseCode(400) - print("Invalid post schema {}".format(failure.getErrorMessage())) - return "Invalid post schema {}".format(failure.getErrorMessage()) - - -@app.handle_errors(JSONDecodeError) -def json_decode_error(request, failure): - request.setResponseCode(400) - return "Invalid post json" - - -@app.handle_errors(ServernameNotSetError) -def not_initialised(request, failure): - request.setResponseCode(500) - return "Config file not setup, please initialise it using the /servername endpoint" - - -@app.handle_errors(ConfigNotFoundError) -def config_not_found(request, failure): - request.setResponseCode(404) - return "The config does not exist" - - -@app.handle_errors(BasConfigInUseError) -def base_config_in_use(request, failure): - request.setResponseCode(409) - return "Sever name and keys already configured" - - -@app.handle_errors(Exception) -def handle_generic_error(request, failure): - print(failure) - request.setResponseCode(500) - return "Internal server error\n{}".format(failure) +from .server import Server + + +class ErrorHandledServer(Server): + app = Server.app + + @app.handle_errors(ValidationError) + def validation_error(self, request, failure): + request.setResponseCode(400) + print("Invalid post schema {}".format(failure.getErrorMessage())) + return "Invalid post schema {}".format(failure.getErrorMessage()) + + @app.handle_errors(JSONDecodeError) + def json_decode_error(self, request, failure): + request.setResponseCode(400) + return "Invalid post json" + + @app.handle_errors(ServernameNotSetError) + def not_initialised(self, request, failure): + request.setResponseCode(500) + return ( + "Config file not setup, please initialise it using the /servername endpoint" + ) + + @app.handle_errors(ConfigNotFoundError) + def config_not_found(self, request, failure): + request.setResponseCode(404) + return "The config does not exist" + + @app.handle_errors(BaseConfigInUseError) + def base_config_in_use(self, request, failure): + request.setResponseCode(409) + return "Sever name and keys already configured" + + @app.handle_errors(Exception) + def handle_generic_error(self, request, failure): + print(failure) + request.setResponseCode(500) + return "Internal server error\n{}".format(failure) diff --git a/synapse_topology/server/server.py b/synapse_topology/server/server.py index c14e54cbf1..8b31a41cfc 100644 --- a/synapse_topology/server/server.py +++ b/synapse_topology/server/server.py @@ -1,10 +1,12 @@ from os.path import abspath, dirname, join from canonicaljson import json -from synapse_topology import model +from synapse_topology.model import Model from twisted.web.static import File +from klein import Klein + from .utils import port_checker from . import error_handlers @@ -17,94 +19,88 @@ from .schemas import ( ) from .utils import validate_schema, log_body_if_fail -from . import app import subprocess import sys -@app.route("/topology_webui/", branch=True) -def server_webui(request): - client_path = abspath(join(dirname(abspath(__file__)), "../webui/dist/")) - print(client_path) - return File(client_path) - - -@app.route("/setup", methods=["GET"]) -def get_config_setup(request): - return json.dumps( - { - model.constants.CONFIG_LOCK: model.config_in_use(), - "config_dir": model.get_config_dir(), - } - ) - - -@app.route("/servername", methods=["GET"]) -def get_server_name(request): - return model.get_server_name() - - -@app.route("/servername", methods=["POST"]) -@validate_schema(SERVERNAME_SCHEMA) -def set_server_name(request, body): - model.generate_base_config(**body) - - -@app.route("/secretkey", methods=["GET"]) -def get_secret_key(request): - return json.dumps({"secret_key": model.get_secret_key()}) - - -@app.route("/config", methods=["GET"]) -def get_config(request): - return str(model.get_config()) - - -@app.route("/config", methods=["POST"]) -@validate_schema(BASE_CONFIG_SCHEMA) -def set_config(request, body): - model.set_config(body) - - -@app.route("/testcertpaths", methods=["POST"]) -@log_body_if_fail -@validate_schema(CERT_PATHS_SCHEMA) -def test_cert_paths(request, body): - result = {} - config_path = model.get_config_dir() - for name, path in body.items(): - path = abspath(join(config_path, path)) - try: - with open(path, "r"): - result[name] = {"invalid": False, "absolute_path": path} - except: - result[name] = {"invalid": True} - return json.dumps(result) - - -@app.route("/certs", methods=["POST"]) -@validate_schema(CERTS_SCHEMA) -def upload_certs(request, body): - model.add_certs(**body) - - -@app.route("/ports", methods=["POST"]) -@validate_schema(PORTS_SCHEMA) -def check_ports(request, body): - results = [] - for port in body["ports"]: - results.append(port_checker(port)) - return json.dumps({"ports": results}) - - -@app.route("/start", methods=["POST"]) -def start_synapse(request): - print("Starting synapse") - subprocess.Popen(["synctl", "start", model.get_config_dir()]) - sys.exit() - - -@app.route("/favicon.ico") -def noop(request): - return +class Server: + app = Klein() + + def __init__(self, model): + self.model = model + + @app.route("/topology_webui/", branch=True) + def server_webui(self, request): + client_path = abspath(join(dirname(abspath(__file__)), "../webui/dist/")) + print(client_path) + return File(client_path) + + @app.route("/setup", methods=["GET"]) + def get_config_setup(self, request): + return json.dumps( + { + self.model.constants.CONFIG_LOCK: self.model.config_in_use(), + "config_dir": self.model.get_config_dir(), + } + ) + + @app.route("/servername", methods=["GET"]) + def get_server_name(self, request): + return self.model.get_server_name() + + @app.route("/servername", methods=["POST"]) + @validate_schema(SERVERNAME_SCHEMA) + def set_server_name(self, request, body): + self.model.generate_base_config(**body) + + @app.route("/secretkey", methods=["GET"]) + def get_secret_key(self, request): + return json.dumps({"secret_key": self.model.get_secret_key()}) + + @app.route("/config", methods=["GET"]) + def get_config(self, request): + return str(self.model.get_config()) + + @app.route("/config", methods=["POST"]) + @validate_schema(BASE_CONFIG_SCHEMA) + def set_config(self, request, body): + self.model.set_config(body) + + @app.route("/testcertpaths", methods=["POST"]) + @log_body_if_fail + @validate_schema(CERT_PATHS_SCHEMA) + def test_cert_paths(self, request, body): + result = {} + config_path = self.model.get_config_dir() + for name, path in body.items(): + path = abspath(join(config_path, path)) + try: + with open(path, "r"): + result[name] = {"invalid": False, "absolute_path": path} + except: + result[name] = {"invalid": True} + return json.dumps(result) + + @app.route("/certs", methods=["POST"]) + @validate_schema(CERTS_SCHEMA) + def upload_certs(self, request, body): + self.model.add_certs(**body) + + @app.route("/ports", methods=["POST"]) + @validate_schema(PORTS_SCHEMA) + def check_ports(self, request, body): + results = [] + for port in body["ports"]: + results.append(port_checker(port)) + return json.dumps({"ports": results}) + + @app.route("/start", methods=["POST"]) + def start_synapse(self, request): + print("Starting synapse") + subprocess.Popen(["synctl", "start", self.model.get_config_dir()]) + sys.exit() + + @app.route("/favicon.ico") + def noop(self, request): + return |