-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract and test server configuration
- Loading branch information
1 parent
b95c1ff
commit e694057
Showing
7 changed files
with
120 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
from typing import Callable, Union | ||
|
||
import uvicorn | ||
import uvicorn.supervisors | ||
|
||
from server.config.di import resolve | ||
from server.config.settings import Settings | ||
|
||
|
||
def get_server_config( | ||
app: Union[str, Callable], 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) | ||
|
||
|
||
class Server(uvicorn.Server): | ||
pass | ||
|
||
|
||
def run(app: Union[str, Callable]) -> int: | ||
""" | ||
Run the API server. | ||
This is a simplified version of `uvicorn.run()`. | ||
""" | ||
config = get_server_config(app) | ||
server = Server(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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters