openlp/openlp/core/api/http/server.py

92 lines
3.8 KiB
Python
Raw Normal View History

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-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-05 16:20:39 +00:00
import logging
from secrets import token_hex
from waitress.server import create_server
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
from openlp.core.threading import ThreadWorker, run_thread
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-04 10:50:43 +00:00
class HttpWorker(ThreadWorker):
"""
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.
"""
def start(self):
2016-06-04 10:50:43 +00:00
"""
2016-06-05 21:16:13 +00:00
Run the thread.
"""
address = Registry().get('settings_thread').value('api/ip address')
port = Registry().get('settings_thread').value('api/port')
try:
application.static_folder = str(AppLocation.get_section_data_path('remotes') / 'static')
self.server = create_server(application, host=address, port=port)
self.server.run()
except OSError:
log.exception('An error occurred when serving the application.')
self.quit.emit()
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()
2017-10-23 22:09:57 +00:00
class HttpServer(RegistryBase, RegistryProperties, LogMixin):
"""
2016-06-05 21:16:13 +00:00
Wrapper round a server instance
"""
2016-08-13 05:18:46 +00:00
def __init__(self, parent=None):
"""
2016-06-13 19:51:46 +00:00
Initialise the http server, and start the http server
"""
2016-08-13 05:18:46 +00:00
super(HttpServer, self).__init__(parent)
Registry().register('authentication_token', token_hex())
if not Registry().get_flag('no_web_server'):
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.
"""
create_paths(AppLocation.get_section_data_path('remotes'))
2016-08-13 05:18:46 +00:00
self.poller = Poller()
Registry().register('poller', self.poller)