-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapp.py
More file actions
78 lines (62 loc) · 2.15 KB
/
Copy pathapp.py
File metadata and controls
78 lines (62 loc) · 2.15 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
66
67
68
69
70
71
72
73
74
75
76
77
78
from __future__ import annotations
import logging
import os
import fastapi
from azure.monitor.opentelemetry import configure_azure_monitor
from fastapi.middleware.cors import CORSMiddleware
from opentelemetry import trace
from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor
from opentelemetry.instrumentation.httpx import HTTPXClientInstrumentor
from opentelemetry.trace import get_tracer_provider
from acidwatch_api.database import lifespan
from acidwatch_api.settings import SETTINGS
from acidwatch_api.authentication import (
swagger_ui_init_oauth_config,
)
from acidwatch_api.routes import router
logging.basicConfig(
format="%(asctime)s %(levelname)s %(name)s: %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
level=logging.INFO,
)
tracer = trace.get_tracer(__name__, tracer_provider=get_tracer_provider())
fastapi_app = fastapi.FastAPI(
title=f"AcidWatch API ({SETTINGS.acidwatch_env})",
swagger_ui_init_oauth=swagger_ui_init_oauth_config,
debug=not SETTINGS.is_production,
lifespan=lifespan,
)
if SETTINGS.applicationinsights_connection_string:
configure_azure_monitor(
connection_string=SETTINGS.applicationinsights_connection_string
)
HTTPXClientInstrumentor().instrument()
FastAPIInstrumentor.instrument_app(fastapi_app)
origins = [
SETTINGS.frontend_uri,
"https://acidwatch.radix.equinor.com",
]
# Allow CORS for GitHub Codespaces if running in that environment
if codespace_name := os.environ.get("CODESPACE_NAME"):
domain = os.environ.get(
"GITHUB_CODESPACES_PORT_FORWARDING_DOMAIN", "app.github.dev"
)
origins.append(f"https://{codespace_name}-5173.{domain}")
fastapi_app.include_router(router)
# Fail if origin misconfigured
if any(origin == "*" for origin in origins):
raise RuntimeError("Wildcard CORS origin '*' is not allowed with credentials")
app = CORSMiddleware(
fastapi_app,
allow_origins=origins,
allow_credentials=True,
allow_methods=["GET", "POST", "PUT", "DELETE", "OPTIONS"],
allow_headers=[
"Authorization",
"Content-Type",
"traceparent",
"tracestate",
"Request-Id",
"Request-Context",
],
)