2016-06-04 10:50:43 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2019-04-13 13:00:22 +00:00
|
|
|
##########################################################################
|
|
|
|
# OpenLP - Open Source Lyrics Projection #
|
|
|
|
# ---------------------------------------------------------------------- #
|
2022-02-01 10:10:57 +00:00
|
|
|
# Copyright (c) 2008-2022 OpenLP Developers #
|
2019-04-13 13:00:22 +00:00
|
|
|
# ---------------------------------------------------------------------- #
|
|
|
|
# This program is free software: you can redistribute it and/or modify #
|
|
|
|
# it under the terms of the GNU 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 General Public License for more details. #
|
|
|
|
# #
|
|
|
|
# You should have received a copy of the GNU General Public License #
|
|
|
|
# along with this program. If not, see <https://www.gnu.org/licenses/>. #
|
|
|
|
##########################################################################
|
2016-06-10 16:16:35 +00:00
|
|
|
"""
|
2016-06-05 21:16:13 +00:00
|
|
|
The :mod:`http` module contains the API web server. This is a lightweight web server used by remotes to interact
|
|
|
|
with OpenLP. It uses JSON to communicate with the remotes.
|
2016-06-10 16:16:35 +00:00
|
|
|
"""
|
2016-06-05 16:20:39 +00:00
|
|
|
import logging
|
2020-01-27 22:57:58 +00:00
|
|
|
from secrets import token_hex
|
2016-06-10 16:16:35 +00:00
|
|
|
|
2018-01-04 21:03:15 +00:00
|
|
|
from waitress.server import create_server
|
2016-06-10 16:16:35 +00:00
|
|
|
|
2017-10-07 07:05:07 +00:00
|
|
|
from openlp.core.api.poll import Poller
|
|
|
|
from openlp.core.common.applocation import AppLocation
|
2017-10-23 22:09:57 +00:00
|
|
|
from openlp.core.common.mixins import LogMixin, RegistryProperties
|
2017-10-07 07:05:07 +00:00
|
|
|
from openlp.core.common.path import create_paths
|
2017-10-23 22:09:57 +00:00
|
|
|
from openlp.core.common.registry import Registry, RegistryBase
|
2017-12-20 14:17:07 +00:00
|
|
|
from openlp.core.threading import ThreadWorker, run_thread
|
2016-06-10 16:16:35 +00:00
|
|
|
|
2020-01-27 22:57:58 +00:00
|
|
|
from openlp.core.api import app as application
|
2018-10-02 04:39:42 +00:00
|
|
|
|
2016-06-05 16:20:39 +00:00
|
|
|
log = logging.getLogger(__name__)
|
2016-06-10 16:16:35 +00:00
|
|
|
|
2016-06-04 10:50:43 +00:00
|
|
|
|
2017-12-20 14:17:07 +00:00
|
|
|
class HttpWorker(ThreadWorker):
|
2016-06-10 16:16:35 +00:00
|
|
|
"""
|
2016-06-05 21:16:13 +00:00
|
|
|
A special Qt thread class to allow the HTTP server to run at the same time as the UI.
|
2016-06-10 16:16:35 +00:00
|
|
|
"""
|
2017-12-20 14:17:07 +00:00
|
|
|
def start(self):
|
2016-06-04 10:50:43 +00:00
|
|
|
"""
|
2016-06-05 21:16:13 +00:00
|
|
|
Run the thread.
|
2016-06-10 16:16:35 +00:00
|
|
|
"""
|
2020-04-02 18:48:27 +00:00
|
|
|
address = Registry().get('settings_thread').value('api/ip address')
|
|
|
|
port = Registry().get('settings_thread').value('api/port')
|
2017-12-02 21:47:11 +00:00
|
|
|
try:
|
2020-01-27 22:57:58 +00:00
|
|
|
application.static_folder = str(AppLocation.get_section_data_path('remotes') / 'static')
|
2018-01-04 21:03:15 +00:00
|
|
|
self.server = create_server(application, host=address, port=port)
|
|
|
|
self.server.run()
|
2017-12-02 21:47:11 +00:00
|
|
|
except OSError:
|
|
|
|
log.exception('An error occurred when serving the application.')
|
2017-12-20 14:17:07 +00:00
|
|
|
self.quit.emit()
|
2016-06-10 16:16:35 +00:00
|
|
|
|
2018-01-04 21:03:15 +00:00
|
|
|
def stop(self):
|
|
|
|
"""
|
|
|
|
A method to stop the worker
|
|
|
|
"""
|
|
|
|
if hasattr(self, 'server'):
|
2018-01-06 07:02:45 +00:00
|
|
|
# Loop through all the channels and close them to stop the server
|
|
|
|
for channel in self.server._map.values():
|
|
|
|
if hasattr(channel, 'close'):
|
|
|
|
channel.close()
|
2018-01-04 21:03:15 +00:00
|
|
|
|
2016-06-10 16:16:35 +00:00
|
|
|
|
2017-10-23 22:09:57 +00:00
|
|
|
class HttpServer(RegistryBase, RegistryProperties, LogMixin):
|
2016-06-10 16:16:35 +00:00
|
|
|
"""
|
2016-06-05 21:16:13 +00:00
|
|
|
Wrapper round a server instance
|
2016-06-10 16:16:35 +00:00
|
|
|
"""
|
2016-08-13 05:18:46 +00:00
|
|
|
def __init__(self, parent=None):
|
2016-06-10 16:16:35 +00:00
|
|
|
"""
|
2016-06-13 19:51:46 +00:00
|
|
|
Initialise the http server, and start the http server
|
2016-06-10 16:16:35 +00:00
|
|
|
"""
|
2016-08-13 05:18:46 +00:00
|
|
|
super(HttpServer, self).__init__(parent)
|
2020-01-27 22:57:58 +00:00
|
|
|
Registry().register('authentication_token', token_hex())
|
2018-01-07 04:36:45 +00:00
|
|
|
if not Registry().get_flag('no_web_server'):
|
2017-12-20 14:17:07 +00:00
|
|
|
worker = HttpWorker()
|
|
|
|
run_thread(worker, 'http_server')
|
2016-08-13 05:18:46 +00:00
|
|
|
|
|
|
|
def bootstrap_post_set_up(self):
|
|
|
|
"""
|
|
|
|
Register the poll return service and start the servers.
|
|
|
|
"""
|
2020-01-27 22:57:58 +00:00
|
|
|
create_paths(AppLocation.get_section_data_path('remotes'))
|
2016-08-13 05:18:46 +00:00
|
|
|
self.poller = Poller()
|
|
|
|
Registry().register('poller', self.poller)
|