-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
103 lines (87 loc) · 3.59 KB
/
Copy pathapp.py
File metadata and controls
103 lines (87 loc) · 3.59 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import uvicorn
from azure.monitor.opentelemetry import configure_azure_monitor
from fastapi import APIRouter, FastAPI, Security
from fastapi.exceptions import RequestValidationError
from fastapi.middleware.cors import CORSMiddleware
from fastapi.security import OAuth2AuthorizationCodeBearer
from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor
from starlette.middleware import Middleware
from starlette.responses import RedirectResponse
from config import config
from features.jobs import jobs
from job_handler_plugins.azure_container_instances import (
AzureHandlerAuthError,
AzureHandlerConfigError,
AzureHandlerProvisionError,
)
from middleware.store_headers import StoreHeadersMiddleware
from restful.responses import responses
from utils.exception_handlers import (
azure_auth_exception_handler,
azure_config_exception_handler,
azure_provision_exception_handler,
validation_exception_handler,
)
from utils.logging import logger
oauth2_scheme = OAuth2AuthorizationCodeBearer(authorizationUrl="", tokenUrl="", auto_error=False)
def auth_with_jwt(jwt_token: str = Security(oauth2_scheme)):
# Authentication is handled by DMSS. Adding a security dependenciy is
# necessary to generate correct JSON schema for openapi generator.
pass
def create_app():
all_routes = APIRouter(tags=["DMJobs"])
authenticated_routes = APIRouter()
authenticated_routes.include_router(jobs.router)
app = FastAPI(
title="Data Modelling Job API",
responses=responses,
version="1.8.0", # x-release-please-version
description="REST API used with the Data Modelling framework to schedule jobs",
exception_handlers={
RequestValidationError: validation_exception_handler,
AzureHandlerConfigError: azure_config_exception_handler,
AzureHandlerAuthError: azure_auth_exception_handler,
AzureHandlerProvisionError: azure_provision_exception_handler,
},
middleware=[Middleware(StoreHeadersMiddleware)],
swagger_ui_init_oauth={
"clientId": config.OAUTH_CLIENT_ID,
"appName": "DM JOB",
"usePkceWithAuthorizationCodeGrant": True,
"scopes": config.OAUTH_AUTH_SCOPE,
"useBasicAuthenticationWithAccessCodeGrant": True,
},
)
if config.ENVIRONMENT == "local":
logger.warning("CORS has been turned off. This should only occur in in development.")
# Turn off CORS when running locally. allow_origins argument can be replaced with a list of URLs.
app.add_middleware(
CORSMiddleware,
allow_origins="*",
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
all_routes.include_router(authenticated_routes, dependencies=[Security(auth_with_jwt)])
app.include_router(all_routes)
if config.APPINSIGHTS_BE_CONNECTION_STRING:
configure_azure_monitor(connection_string=config.APPINSIGHTS_BE_CONNECTION_STRING, logger_name="API")
FastAPIInstrumentor.instrument_app(app)
@app.get("/", operation_id="redirect_to_docs", response_class=RedirectResponse, include_in_schema=False)
def redirect_to_docs():
"""
Redirects any requests to the servers root ('/') to '/docs'
"""
return RedirectResponse(url="/docs")
return app
def run():
uvicorn.run(
"app:create_app",
host="0.0.0.0", # nosec
port=5000,
reload=config.ENVIRONMENT == "local",
factory=True,
log_level=config.LOGGER_LEVEL.lower(),
)
if __name__ == "__main__":
run()