Skip to content

Commit

Permalink
Extract and test server configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
florimondmanca committed Jun 28, 2022
1 parent b95c1ff commit 2e34e11
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 29 deletions.
58 changes: 58 additions & 0 deletions server/infrastructure/server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import uvicorn
import uvicorn.supervisors

from server.config.di import resolve
from server.config.settings import Settings


def get_server_config(app: str, settings: Settings = None) -> uvicorn.Config:
if settings is None:
settings = resolve(Settings)

kwargs = dict(
host=settings.host,
port=settings.port,
)

if settings.server_mode == "local":
kwargs.update(
# Enable hot reload.
reload=True,
reload_dirs=["server"],
)
elif settings.server_mode == "live":
kwargs.update(
# Pass any proxy headers, so that Uvicorn sees information about the
# connecting client, rather than the connecting Nginx proxy.
# See: https://www.uvicorn.org/deployment/#running-behind-nginx
proxy_headers=True,
# Match Nginx mount path.
root_path="/api",
)

return uvicorn.Config(app, **kwargs)


def run(app: str) -> int:
"""
Run the API server.
This is a simplified version of `uvicorn.run()`.
"""
config = get_server_config(app)
server = uvicorn.Server(config=config)

if config.should_reload:
sock = config.bind_socket()
reloader = uvicorn.supervisors.ChangeReload(
config, target=server.run, sockets=[sock]
)
reloader.run()
return 0

server.run()

if not server.started:
return 3

return 0
33 changes: 4 additions & 29 deletions server/main.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,12 @@
import sys

from .api.app import create_app
from .config.di import bootstrap
from .infrastructure.server import run

bootstrap()

app = create_app()

if __name__ == "__main__":
import uvicorn

from .config.di import resolve
from .config.settings import Settings

settings = resolve(Settings)

kwargs: dict = {
"host": settings.host,
"port": settings.port,
}

if settings.server_mode == "local":
kwargs.update(
# Enable hot reload.
reload=True,
reload_dirs=["server"],
)
elif settings.server_mode == "live":
kwargs.update(
# Pass any proxy headers, so that Uvicorn sees information about the
# connecting client, rather than the connecting Nginx proxy.
# See: https://www.uvicorn.org/deployment/#running-behind-nginx
proxy_headers=True,
# Match Nginx mount path.
root_path="/api",
)

uvicorn.run("server.main:app", **kwargs)
sys.exit(run("server.main:app"))
25 changes: 25 additions & 0 deletions tests/api/test_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from server.config import Settings
from server.config.di import resolve
from server.infrastructure.server import get_server_config


def test_server_config_local() -> None:
settings = resolve(Settings).copy(update={"server_mode": "local"})

config = get_server_config("server.main:app", settings)

assert config.host == "localhost"
assert config.port == 3579
assert config.should_reload
assert config.root_path == ""


def test_server_config_live() -> None:
settings = resolve(Settings).copy(update={"server_mode": "live"})

config = get_server_config("server.main:app", settings)

assert config.host == "localhost"
assert config.port == 3579
assert config.proxy_headers
assert config.root_path == "/api"

0 comments on commit 2e34e11

Please sign in to comment.