43 lines
1.3 KiB
Python
43 lines
1.3 KiB
Python
import logging
|
|
import os
|
|
import ssl
|
|
|
|
import uvicorn
|
|
from uvicorn.protocols.http.h11_impl import H11Protocol
|
|
|
|
|
|
class IngressProtocol(H11Protocol):
|
|
def connection_made(self, transport):
|
|
tls = transport.get_extra_info("ssl_object")
|
|
peer = tls.getpeercert() if tls else {}
|
|
if ("DNS", "lb-ingress") not in peer.get("subjectAltName", ()):
|
|
transport.close()
|
|
return
|
|
super().connection_made(transport)
|
|
|
|
def connection_lost(self, exc):
|
|
if self.transport is not None:
|
|
super().connection_lost(exc)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
logging.basicConfig(level=logging.INFO, format="%(levelname)s %(message)s")
|
|
logging.getLogger("messenger").info("environment=%s", os.environ.get("APP_ENV", "lab"))
|
|
port = int(os.environ.get("APP_PORT", "8080"))
|
|
if not 1 <= port <= 65535:
|
|
raise SystemExit("APP_PORT должен быть от 1 до 65535")
|
|
uvicorn.run(
|
|
"src.app:create_app",
|
|
factory=True,
|
|
host=os.environ.get("APP_HOST", "0.0.0.0"),
|
|
port=port,
|
|
workers=1,
|
|
http=IngressProtocol,
|
|
ssl_certfile="/run/tls/api.crt",
|
|
ssl_keyfile="/run/tls/api.key",
|
|
ssl_ca_certs="/run/tls/ca.crt",
|
|
ssl_cert_reqs=ssl.CERT_REQUIRED,
|
|
access_log=False,
|
|
timeout_graceful_shutdown=2,
|
|
)
|