2022-06-16 10:12:37 +09:00
|
|
|
# SPDX-FileCopyrightText: 2022 n9k <https://git.076.ne.jp/ninya9k>
|
2022-03-07 23:51:59 +09:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
|
2022-02-13 13:00:10 +09:00
|
|
|
from collections import OrderedDict
|
|
|
|
|
2022-03-10 22:58:17 +09:00
|
|
|
from quart_compress import Compress
|
2022-02-13 13:00:10 +09:00
|
|
|
|
2022-06-12 17:10:24 +09:00
|
|
|
from anonstream.config import update_flask_from_toml
|
2022-02-20 13:23:32 +09:00
|
|
|
from anonstream.utils.captcha import create_captcha_factory, create_captcha_signer
|
2022-06-12 17:10:24 +09:00
|
|
|
from anonstream.quart import Quart
|
2022-02-13 13:00:10 +09:00
|
|
|
|
2022-06-16 12:15:21 +09:00
|
|
|
def create_app(toml_config):
|
2022-06-19 17:26:38 +09:00
|
|
|
app = Quart('anonstream', static_folder=None)
|
2022-06-12 17:10:24 +09:00
|
|
|
app.jinja_options['trim_blocks'] = True
|
|
|
|
app.jinja_options['lstrip_blocks'] = True
|
|
|
|
|
|
|
|
auth_password = update_flask_from_toml(toml_config, app.config)
|
|
|
|
print('Broadcaster username:', app.config['AUTH_USERNAME'])
|
2022-02-13 13:00:10 +09:00
|
|
|
print('Broadcaster password:', auth_password)
|
|
|
|
|
2022-06-12 17:10:24 +09:00
|
|
|
# Compress some responses
|
2022-06-29 13:48:49 +09:00
|
|
|
Compress(app)
|
2022-02-16 18:55:30 +09:00
|
|
|
app.config.update({
|
2022-06-19 16:53:31 +09:00
|
|
|
'COMPRESS_MIN_SIZE': 2048,
|
|
|
|
'COMPRESS_LEVEL': 9,
|
2022-02-16 18:55:30 +09:00
|
|
|
})
|
|
|
|
|
2022-06-12 17:10:24 +09:00
|
|
|
# Global state: messages, users, captchas
|
2022-02-18 10:32:34 +09:00
|
|
|
app.messages_by_id = OrderedDict()
|
|
|
|
app.messages = app.messages_by_id.values()
|
2022-02-20 16:20:43 +09:00
|
|
|
|
|
|
|
app.users_by_token = {}
|
2022-02-18 10:32:34 +09:00
|
|
|
app.users = app.users_by_token.values()
|
2022-02-20 16:20:43 +09:00
|
|
|
|
|
|
|
app.captchas = OrderedDict()
|
2022-02-20 13:23:32 +09:00
|
|
|
app.captcha_factory = create_captcha_factory(app.config['CAPTCHA_FONTS'])
|
|
|
|
app.captcha_signer = create_captcha_signer(app.config['SECRET_KEY'])
|
2022-02-20 16:20:43 +09:00
|
|
|
|
2022-06-22 17:35:35 +09:00
|
|
|
app.failures = OrderedDict()
|
2022-06-22 14:00:43 +09:00
|
|
|
|
2022-02-23 07:51:29 +09:00
|
|
|
# State for tasks
|
2022-02-20 16:20:43 +09:00
|
|
|
app.users_update_buffer = set()
|
2022-02-23 07:51:29 +09:00
|
|
|
app.stream_title = None
|
2022-02-26 08:06:36 +09:00
|
|
|
app.stream_uptime = None
|
|
|
|
app.stream_viewership = None
|
2022-06-16 05:50:43 +09:00
|
|
|
app.last_info_task = None
|
2022-02-13 13:00:10 +09:00
|
|
|
|
2022-02-23 07:51:29 +09:00
|
|
|
# Background tasks' asyncio.sleep tasks, cancelled on shutdown
|
2022-02-20 10:06:13 +09:00
|
|
|
app.background_sleep = set()
|
|
|
|
|
2022-06-14 20:40:16 +09:00
|
|
|
# Queues for event socket clients
|
|
|
|
app.event_queues = set()
|
|
|
|
|
2022-02-20 10:06:13 +09:00
|
|
|
@app.after_serving
|
2022-02-19 12:14:37 +09:00
|
|
|
async def shutdown():
|
2022-06-12 17:10:24 +09:00
|
|
|
# Force all background tasks to finish
|
2022-02-20 10:06:13 +09:00
|
|
|
for task in app.background_sleep:
|
|
|
|
task.cancel()
|
2022-02-19 12:14:37 +09:00
|
|
|
|
|
|
|
@app.before_serving
|
|
|
|
async def startup():
|
2022-06-13 07:23:17 +09:00
|
|
|
# Start control server
|
2022-06-14 20:40:16 +09:00
|
|
|
if app.config['SOCKET_CONTROL_ENABLED']:
|
|
|
|
from anonstream.control.server import start_control_server_at
|
|
|
|
async def start_control_server():
|
|
|
|
return await start_control_server_at(
|
|
|
|
app.config['SOCKET_CONTROL_ADDRESS']
|
|
|
|
)
|
|
|
|
app.add_background_task(start_control_server)
|
|
|
|
|
|
|
|
# Start event server
|
|
|
|
if app.config['SOCKET_EVENT_ENABLED']:
|
|
|
|
from anonstream.events import start_event_server_at
|
|
|
|
async def start_event_server():
|
|
|
|
return await start_event_server_at(
|
2022-06-25 18:39:40 +09:00
|
|
|
app.config['SOCKET_EVENT_ADDRESS']
|
2022-06-14 20:40:16 +09:00
|
|
|
)
|
|
|
|
app.add_background_task(start_event_server)
|
|
|
|
|
2022-06-13 07:23:17 +09:00
|
|
|
# Create routes and background tasks
|
2022-02-13 13:00:10 +09:00
|
|
|
import anonstream.routes
|
2022-02-19 12:14:37 +09:00
|
|
|
import anonstream.tasks
|
2022-02-13 13:00:10 +09:00
|
|
|
|
|
|
|
return app
|