From 8153d8476fb4e24d08d9d4b561a6dede446c0008 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Sun, 24 Feb 2019 12:35:26 +0100 Subject: [PATCH] Added uWSGI optional dependency for HTTP backend --- platypush/backend/http/__init__.py | 31 ++++++++++++++++++----- platypush/backend/http/{app => }/uwsgi.py | 4 +-- requirements.txt | 1 + setup.py | 1 + 4 files changed, 28 insertions(+), 9 deletions(-) rename platypush/backend/http/{app => }/uwsgi.py (51%) diff --git a/platypush/backend/http/__init__.py b/platypush/backend/http/__init__.py index 00be2bc95..e31fef132 100644 --- a/platypush/backend/http/__init__.py +++ b/platypush/backend/http/__init__.py @@ -48,6 +48,16 @@ class HttpBackend(Backend): * **python-dateutil** (``pip install python-dateutil``) * **magic** (``pip install python-magic``), optional, for MIME type support if you want to enable media streaming + * **uwsgi** (``pip install uwsgi`` plus uwsgi server installed on your + system if required) - optional but recommended. By default the + Platypush web server will run in a process spawned on the fly by + the HTTP backend. However, being a Flask app, it will serve clients + in a single thread and won't support many features of a full-blown + web server. + + Base command to run the web server over uwsgi:: + + uwsgi --http :8008 --module platypush.backend.http.uwsgi --master --processes 4 --threads 4 """ _DEFAULT_HTTP_PORT = 8008 @@ -57,7 +67,7 @@ class HttpBackend(Backend): websocket_port=_DEFAULT_WEBSOCKET_PORT, disable_websocket=False, dashboard={}, resource_dirs={}, ssl_cert=None, ssl_key=None, ssl_cafile=None, ssl_capath=None, - maps={}, **kwargs): + maps={}, run_externally=False, **kwargs): """ :param port: Listen port for the web server (default: 8008) :type port: int @@ -115,6 +125,11 @@ class HttpBackend(Backend): db: "sqlite:////home/blacklight/.local/share/platypush/feeds/rss.db" :type dashboard: dict + + :param run_externally: If set, then the HTTP backend will not directly + spawn the web server. Set this option if you plan to run the webapp + in a separate web server (recommended), like uwsgi or uwsgi+nginx. + :type run_externally: bool """ super().__init__(**kwargs) @@ -129,6 +144,7 @@ class HttpBackend(Backend): self.resource_dirs = { name: os.path.abspath( os.path.expanduser(d)) for name, d in resource_dirs.items() } self.active_websockets = set() + self.run_externally = run_externally self.ssl_context = get_ssl_server_context(ssl_cert=ssl_cert, ssl_key=ssl_key, ssl_cafile=ssl_cafile, @@ -215,17 +231,18 @@ class HttpBackend(Backend): def run(self): super().run() - - self.server_proc = Process(target=self._start_web_server(), - name='WebServer') - self.server_proc.start() + self.logger.info('Initializing HTTP backend on port {}'.format(self.port)) if not self.disable_websocket: + self.logger.info('Initializing websocket interface') self.websocket_thread = threading.Thread(target=self.websocket) self.websocket_thread.start() - self.logger.info('Initialized HTTP backend on port {}'.format(self.port)) - self.server_proc.join() + if not self.run_externally: + self.server_proc = Process(target=self._start_web_server(), + name='WebServer') + self.server_proc.start() + self.server_proc.join() # vim:sw=4:ts=4:et: diff --git a/platypush/backend/http/app/uwsgi.py b/platypush/backend/http/uwsgi.py similarity index 51% rename from platypush/backend/http/app/uwsgi.py rename to platypush/backend/http/uwsgi.py index df2ccfcff..6312c2e92 100644 --- a/platypush/backend/http/app/uwsgi.py +++ b/platypush/backend/http/uwsgi.py @@ -1,8 +1,8 @@ """ -uWSGI webapp startup script +uWSGI webapp entry point """ -from . import application +from platypush.backend.http.app import application if __name__ == '__main__': application.run() diff --git a/requirements.txt b/requirements.txt index 3cb238375..95bf5cb07 100644 --- a/requirements.txt +++ b/requirements.txt @@ -22,6 +22,7 @@ flask websockets redis python-dateutil +#uwsgi # HTTP poll backend support frozendict diff --git a/setup.py b/setup.py index a3cd1ae23..586184496 100755 --- a/setup.py +++ b/setup.py @@ -70,6 +70,7 @@ setup( 'Support for Pushbullet backend': ['requests', 'pushbullet.py'], 'Support for HTTP backend': ['flask','websockets', 'python-dateutil'], 'Support for HTTP poll backend': ['frozendict'], + 'Support for external web server': ['uwsgi'], 'Support for database plugin': ['sqlalchemy'], 'Support for RSS feeds': ['feedparser'], 'Support for PDF generation': ['weasyprint'],