forked from bybatkhuu/module-fastapi-logging
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbootstrap.py
More file actions
65 lines (48 loc) · 1.48 KB
/
bootstrap.py
File metadata and controls
65 lines (48 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# Standard libraries
from typing import Any
from collections.abc import Callable
# Third-party libraries
import uvicorn
from uvicorn._types import ASGIApplication
from pydantic import validate_call
from fastapi import FastAPI
from beans_logging_fastapi import add_logger
# Internal modules
from __version__ import __version__
from config import config
from lifespan import lifespan
from router import add_routers
def create_app() -> FastAPI:
"""Create FastAPI application instance.
Returns:
FastAPI: FastAPI application instance.
"""
app = FastAPI(lifespan=lifespan, version=__version__)
# Add logger before any other components:
add_logger(app=app, config=config.logger)
# Add any other components after logger:
add_routers(app=app)
return app
@validate_call(config={"arbitrary_types_allowed": True})
def run_server(app: FastAPI | ASGIApplication | Callable[..., Any] | str) -> None:
"""Run uvicorn server.
Args:
app (FastAPI |
ASGIApplication |
Callable[..., Any] |
str , required): FastAPI application instance or ASGI application or import string.
"""
uvicorn.run(
app=app,
host="0.0.0.0", # nosec B104
port=8000,
access_log=False, # Disable default uvicorn access log
server_header=False,
proxy_headers=False,
forwarded_allow_ips="*",
)
return
__all__ = [
"create_app",
"run_server",
]