From 201b256fe55c7f58bd44e0a90c94c5d9ca2bc8ca Mon Sep 17 00:00:00 2001 From: Sunil Mohan Adapa Date: Tue, 8 Jan 2019 17:43:49 -0800 Subject: [PATCH] main: Separate out CherryPy code into a separate module This will help with modularizing the code as well as abstracting out CherryPy for potential later replacement. Signed-off-by: Sunil Mohan Adapa --- plinth/__main__.py | 76 ++------------------------------ plinth/web_server.py | 101 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+), 73 deletions(-) create mode 100644 plinth/web_server.py diff --git a/plinth/__main__.py b/plinth/__main__.py index be50258fc..8cbab88ae 100644 --- a/plinth/__main__.py +++ b/plinth/__main__.py @@ -19,14 +19,12 @@ import argparse import importlib import logging -import os import sys import axes -import cherrypy from . import (cfg, frontpage, log, menu, module_loader, service, setup, - web_framework) + web_framework, web_server) axes.default_app_config = "plinth.axes_app_config.AppConfig" precedence_commandline_arguments = ["server_dir", "develop"] @@ -62,72 +60,6 @@ def parse_arguments(): return parser.parse_args() -def _mount_static_directory(static_dir, static_url): - config = { - '/': { - 'tools.staticdir.root': static_dir, - 'tools.staticdir.on': True, - 'tools.staticdir.dir': '.' - } - } - app = cherrypy.tree.mount(None, static_url, config) - log.setup_cherrypy_static_directory(app) - logger.debug('Serving static directory %s on %s', static_dir, static_url) - - -def setup_server(): - """Setup CherryPy server""" - logger.info('Setting up CherryPy server') - - # Configure default server - cherrypy.config.update({ - 'server.max_request_body_size': 0, - 'server.socket_host': cfg.host, - 'server.socket_port': cfg.port, - 'server.thread_pool': 10, - # Avoid stating files once per second in production - 'engine.autoreload.on': cfg.develop, - }) - - application = web_framework.get_wsgi_application() - cherrypy.tree.graft(application, cfg.server_dir) - - static_dir = os.path.join(cfg.file_root, 'static') - _mount_static_directory(static_dir, web_framework.get_static_url()) - - custom_static_dir = cfg.custom_static_dir - custom_static_url = '/plinth/custom/static' - if os.path.exists(custom_static_dir): - _mount_static_directory(custom_static_dir, custom_static_url) - else: - logger.debug( - 'Not serving custom static directory %s on %s, ' - 'directory does not exist', custom_static_dir, custom_static_url) - - _mount_static_directory('/usr/share/javascript', '/javascript') - - manual_dir = os.path.join(cfg.doc_dir, 'images') - manual_url = '/'.join([cfg.server_dir, 'help/manual/images']) \ - .replace('//', '/') - _mount_static_directory(manual_dir, manual_url) - - for module_name, module in module_loader.loaded_modules.items(): - module_path = os.path.dirname(module.__file__) - static_dir = os.path.join(module_path, 'static') - if not os.path.isdir(static_dir): - continue - - urlprefix = "%s%s" % (web_framework.get_static_url(), module_name) - _mount_static_directory(static_dir, urlprefix) - - cherrypy.engine.signal_handler.subscribe() - - -def on_server_stop(): - """Stop all other threads since web server is trying to exit.""" - setup.stop() - - def run_setup_and_exit(module_list, allow_install=True): """Run setup on all essential modules and exit.""" error_code = 0 @@ -239,11 +171,9 @@ def main(): run_diagnostics_and_exit() setup.run_setup_in_background() - setup_server() - cherrypy.engine.start() - cherrypy.engine.subscribe('stop', on_server_stop) - cherrypy.engine.block() + web_server.init() + web_server.run() if __name__ == '__main__': diff --git a/plinth/web_server.py b/plinth/web_server.py new file mode 100644 index 000000000..d2b06fc38 --- /dev/null +++ b/plinth/web_server.py @@ -0,0 +1,101 @@ +# +# This file is part of FreedomBox. +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +""" +Setup CherryPy web server. +""" + +import logging +import os + +import cherrypy + +from . import cfg, log, module_loader, setup, web_framework + +logger = logging.getLogger(__name__) + + +def _mount_static_directory(static_dir, static_url): + config = { + '/': { + 'tools.staticdir.root': static_dir, + 'tools.staticdir.on': True, + 'tools.staticdir.dir': '.' + } + } + app = cherrypy.tree.mount(None, static_url, config) + log.setup_cherrypy_static_directory(app) + logger.debug('Serving static directory %s on %s', static_dir, static_url) + + +def init(): + """Setup CherryPy server""" + logger.info('Setting up CherryPy server') + + # Configure default server + cherrypy.config.update({ + 'server.max_request_body_size': 0, + 'server.socket_host': cfg.host, + 'server.socket_port': cfg.port, + 'server.thread_pool': 10, + # Avoid stating files once per second in production + 'engine.autoreload.on': cfg.develop, + }) + + application = web_framework.get_wsgi_application() + cherrypy.tree.graft(application, cfg.server_dir) + + static_dir = os.path.join(cfg.file_root, 'static') + _mount_static_directory(static_dir, web_framework.get_static_url()) + + custom_static_dir = cfg.custom_static_dir + custom_static_url = '/plinth/custom/static' + if os.path.exists(custom_static_dir): + _mount_static_directory(custom_static_dir, custom_static_url) + else: + logger.debug( + 'Not serving custom static directory %s on %s, ' + 'directory does not exist', custom_static_dir, custom_static_url) + + _mount_static_directory('/usr/share/javascript', '/javascript') + + manual_dir = os.path.join(cfg.doc_dir, 'images') + manual_url = '/'.join([cfg.server_dir, 'help/manual/images']) \ + .replace('//', '/') + _mount_static_directory(manual_dir, manual_url) + + for module_name, module in module_loader.loaded_modules.items(): + module_path = os.path.dirname(module.__file__) + static_dir = os.path.join(module_path, 'static') + if not os.path.isdir(static_dir): + continue + + urlprefix = "%s%s" % (web_framework.get_static_url(), module_name) + _mount_static_directory(static_dir, urlprefix) + + cherrypy.engine.signal_handler.subscribe() + + +def on_server_stop(): + """Stop all other threads since web server is trying to exit.""" + setup.stop() + + +def run(): + """Start the web server and block it until exit.""" + cherrypy.engine.start() + cherrypy.engine.subscribe('stop', on_server_stop) + cherrypy.engine.block()