diff options
-rwxr-xr-x | bin/fg21sim-webui | 4 | ||||
-rw-r--r-- | fg21sim/webui/__init__.py | 2 | ||||
-rw-r--r-- | fg21sim/webui/app.py | 35 | ||||
-rw-r--r-- | fg21sim/webui/websocket.py | 3 |
4 files changed, 21 insertions, 23 deletions
diff --git a/bin/fg21sim-webui b/bin/fg21sim-webui index 9da61a3..642039e 100755 --- a/bin/fg21sim-webui +++ b/bin/fg21sim-webui @@ -26,7 +26,7 @@ from tornado.options import define, options, parse_command_line from fg21sim.configs import configs from fg21sim.utils import setup_logging -from fg21sim.webui import make_application +from fg21sim.webui import Application # Define options in the global namespace @@ -60,7 +60,7 @@ def main(): logger = logging.getLogger(tool) logger.info("COMMAND: {0}".format(" ".join(sys.argv))) - application = make_application(debug=options.debug) + application = Application(debug=options.debug) application.listen(options.port) url = "http://localhost:{port}".format(port=options.port) logger.info("Tornado started") diff --git a/fg21sim/webui/__init__.py b/fg21sim/webui/__init__.py index 637e653..745c947 100644 --- a/fg21sim/webui/__init__.py +++ b/fg21sim/webui/__init__.py @@ -1,4 +1,4 @@ # Copyright (c) 2016 Weitian LI <liweitianux@live.com> # MIT license -from .app import make_application +from .app import Application diff --git a/fg21sim/webui/app.py b/fg21sim/webui/app.py index 8199aa7..004be8d 100644 --- a/fg21sim/webui/app.py +++ b/fg21sim/webui/app.py @@ -16,6 +16,7 @@ import os import tornado.web from .websocket import FG21simWSHandler +from ..configs import ConfigManager class IndexHandler(tornado.web.RequestHandler): @@ -23,23 +24,21 @@ class IndexHandler(tornado.web.RequestHandler): self.render("index.html") -_settings = { - # The static files will be served from the default "/static/" URI. - # Recommend to use `{{ static_url(filepath) }}` in the templates. - "static_path": os.path.join(os.path.dirname(__file__), "static"), - "template_path": os.path.join(os.path.dirname(__file__), "templates"), -} +class Application(tornado.web.Application): + configmanager = ConfigManager() - -# FIXME: -# * Subclass on `tornado.web.Application` -# * hold the attributes (e.g., configs, console) ?? -def make_application(**kwargs): - settings = _settings - settings.update(kwargs) - appplication = tornado.web.Application( - handlers=[ - (r"/", IndexHandler), + def __init__(self, **kwargs): + handlers = [ + (r"/", IndexHandler), (r"/ws", FG21simWSHandler), - ], **settings) - return appplication + ] + settings = { + # The static files will be served from the default "/static/" URI. + # Recommend to use `{{ static_url(filepath) }}` in the templates. + "static_path": os.path.join(os.path.dirname(__file__), + "static"), + "template_path": os.path.join(os.path.dirname(__file__), + "templates"), + } + settings.update(kwargs) + super().__init__(handlers, **settings) diff --git a/fg21sim/webui/websocket.py b/fg21sim/webui/websocket.py index 53d2da4..dffd864 100644 --- a/fg21sim/webui/websocket.py +++ b/fg21sim/webui/websocket.py @@ -26,7 +26,6 @@ from tornado.options import options from .consolehandler import ConsoleHandler from .utils import get_host_ip, ip_in_network -from ..configs import ConfigManager from ..errors import ConfigError @@ -101,7 +100,7 @@ class FG21simWSHandler(tornado.websocket.WebSocketHandler): # FIXME: # * better to move to the `Application` class ?? # * or create a ``ConfigsHandler`` similar to the ``ConsoleHandler`` - self.configs = ConfigManager() + self.configs = self.application.configmanager self.console_handler = ConsoleHandler(websocket=self) # logger.info("WebSocket: {0}: opened".format(self.name)) |