Dan Albert 9e6b1cf716 Move logging config to a file.
By default logging configuration is defined by
resources/default_logging.yaml. Very noisy loggers (like the
uvicorn.access logger) are kept out of the console and UI logs by
default. Developers (or weird users) can customize their default logging
config by copying the file to resources/logging.yaml and editing as
needed. It would be preferable to load this file form the Liberation
user directory, but because first-time initialization requires the UI,
we want to configure logging before we necessarily know where to find
that.
2022-02-20 14:17:44 -08:00

38 lines
1.1 KiB
Python

import time
from collections.abc import Iterator
from contextlib import contextmanager
from threading import Thread
import uvicorn
from uvicorn import Config
from game.server.app import app
from game.server.settings import ServerSettings
class Server(uvicorn.Server):
def __init__(self) -> None:
super().__init__(
Config(
app=app,
host=ServerSettings.get().server_bind_address,
port=ServerSettings.get().server_port,
# Configured explicitly with default_logging.yaml or logging.yaml.
log_config=None,
)
)
@contextmanager
def run_in_thread(self) -> Iterator[None]:
# This relies on undocumented behavior, but it is what the developer recommends:
# https://github.com/encode/uvicorn/issues/742
thread = Thread(target=self.run)
thread.start()
try:
while not self.started:
time.sleep(1e-3)
yield
finally:
self.should_exit = True
thread.join()