|
1 | | -import click |
2 | | -from fastapi import APIRouter, FastAPI, Security |
3 | | -from fastapi.exceptions import RequestValidationError |
4 | | -from starlette.middleware import Middleware |
5 | | - |
6 | | -from authentication.authentication import auth_with_jwt |
7 | | -from common.exception_handlers import validation_exception_handler |
8 | | -from common.middleware import TimerHeaderMiddleware |
9 | | -from common.responses import responses |
10 | | -from config import config |
11 | | -from features.health_check import health_check_feature |
12 | | -from features.todo import todo_feature |
13 | | -from features.whoami import whoami_feature |
14 | | - |
15 | | -description_md = """ |
16 | | -### Description |
17 | | -A RESTful API for handling todo items. |
18 | | -
|
19 | | -Anyone in Equinor are authorized to use the API. |
20 | | - * Click **Authorize** to login and start testing. |
21 | | -
|
22 | | -### Resources |
23 | | - * [Docs](https://equinor.github.io/template-fastapi-react/) |
24 | | - * [Github](https://github.com/equinor/template-fastapi-react) |
25 | | -
|
26 | | - For questions about usage or expanding the API, create issue on Github or see docs. |
27 | | -""" |
28 | | - |
29 | | - |
30 | | -def create_app() -> FastAPI: |
31 | | - public_routes = APIRouter() |
32 | | - public_routes.include_router(health_check_feature.router) |
33 | | - |
34 | | - authenticated_routes = APIRouter() |
35 | | - authenticated_routes.include_router(todo_feature.router) |
36 | | - authenticated_routes.include_router(whoami_feature.router) |
37 | | - |
38 | | - middleware = [Middleware(TimerHeaderMiddleware)] |
39 | | - |
40 | | - exception_handlers = {RequestValidationError: validation_exception_handler} |
41 | | - |
42 | | - app = FastAPI( |
43 | | - root_path="/api", |
44 | | - title="Template FastAPI React", |
45 | | - version="1.2.1", # x-release-please-version |
46 | | - description=description_md, |
47 | | - responses=responses, |
48 | | - middleware=middleware, |
49 | | - license_info={"name": "MIT", "url": "https://github.com/equinor/template-fastapi-react/blob/main/LICENSE.md"}, |
50 | | - exception_handlers=exception_handlers, # type: ignore |
51 | | - swagger_ui_init_oauth={ |
52 | | - "clientId": config.OAUTH_CLIENT_ID, |
53 | | - "appName": "TemplateFastAPIReact", |
54 | | - "usePkceWithAuthorizationCodeGrant": True, |
55 | | - "scopes": config.OAUTH_AUTH_SCOPE, |
56 | | - "useBasicAuthenticationWithAccessCodeGrant": True, |
57 | | - }, |
58 | | - ) |
59 | | - |
60 | | - app.include_router(authenticated_routes, dependencies=[Security(auth_with_jwt)]) |
61 | | - app.include_router(public_routes) |
62 | | - |
63 | | - return app |
64 | | - |
65 | | - |
66 | | -@click.group() |
67 | | -def cli(): |
68 | | - pass |
69 | | - |
70 | | - |
71 | | -@cli.command() |
72 | | -def run(): |
73 | | - import uvicorn |
74 | | - |
75 | | - uvicorn.run( |
76 | | - "app:create_app", |
77 | | - host="0.0.0.0", # nosec |
78 | | - port=5000, |
79 | | - factory=True, |
80 | | - reload=config.ENVIRONMENT == "local", |
81 | | - log_level=config.LOGGER_LEVEL.lower(), |
82 | | - ) |
83 | | - |
84 | | - |
85 | | -if __name__ == "__main__": |
86 | | - cli() # run commands in cli() group |
| 1 | +import click |
| 2 | +from fastapi import APIRouter, FastAPI, Security |
| 3 | +from fastapi.exceptions import RequestValidationError |
| 4 | +from starlette.middleware import Middleware |
| 5 | + |
| 6 | +from authentication.authentication import auth_with_jwt |
| 7 | +from common.exception_handlers import validation_exception_handler |
| 8 | +from common.middleware import TimerHeaderMiddleware |
| 9 | +from common.responses import responses |
| 10 | +from config import config |
| 11 | +from features.health_check import health_check_feature |
| 12 | +from features.todo import todo_feature |
| 13 | +from features.whoami import whoami_feature |
| 14 | + |
| 15 | +description_md = """ |
| 16 | +### Description |
| 17 | +A RESTful API for handling todo items. |
| 18 | +
|
| 19 | +Anyone in Equinor are authorized to use the API. |
| 20 | + * Click **Authorize** to login and start testing. |
| 21 | +
|
| 22 | +### Resources |
| 23 | + * [Docs](https://equinor.github.io/template-fastapi-react/) |
| 24 | + * [Github](https://github.com/equinor/template-fastapi-react) |
| 25 | +
|
| 26 | + For questions about usage or expanding the API, create issue on Github or see docs. |
| 27 | +""" |
| 28 | + |
| 29 | + |
| 30 | +def create_app() -> FastAPI: |
| 31 | + public_routes = APIRouter() |
| 32 | + public_routes.include_router(health_check_feature.router) |
| 33 | + |
| 34 | + authenticated_routes = APIRouter() |
| 35 | + authenticated_routes.include_router(todo_feature.router) |
| 36 | + authenticated_routes.include_router(whoami_feature.router) |
| 37 | + |
| 38 | + middleware = [Middleware(TimerHeaderMiddleware)] |
| 39 | + |
| 40 | + exception_handlers = {RequestValidationError: validation_exception_handler} |
| 41 | + |
| 42 | + app = FastAPI( |
| 43 | + root_path="/api", |
| 44 | + title="Template FastAPI React", |
| 45 | + version="1.3.0", # x-release-please-version |
| 46 | + description=description_md, |
| 47 | + responses=responses, |
| 48 | + middleware=middleware, |
| 49 | + license_info={"name": "MIT", "url": "https://github.com/equinor/template-fastapi-react/blob/main/LICENSE.md"}, |
| 50 | + exception_handlers=exception_handlers, # type: ignore |
| 51 | + swagger_ui_init_oauth={ |
| 52 | + "clientId": config.OAUTH_CLIENT_ID, |
| 53 | + "appName": "TemplateFastAPIReact", |
| 54 | + "usePkceWithAuthorizationCodeGrant": True, |
| 55 | + "scopes": config.OAUTH_AUTH_SCOPE, |
| 56 | + "useBasicAuthenticationWithAccessCodeGrant": True, |
| 57 | + }, |
| 58 | + ) |
| 59 | + |
| 60 | + app.include_router(authenticated_routes, dependencies=[Security(auth_with_jwt)]) |
| 61 | + app.include_router(public_routes) |
| 62 | + |
| 63 | + return app |
| 64 | + |
| 65 | + |
| 66 | +@click.group() |
| 67 | +def cli(): |
| 68 | + pass |
| 69 | + |
| 70 | + |
| 71 | +@cli.command() |
| 72 | +def run(): |
| 73 | + import uvicorn |
| 74 | + |
| 75 | + uvicorn.run( |
| 76 | + "app:create_app", |
| 77 | + host="0.0.0.0", # nosec |
| 78 | + port=5000, |
| 79 | + factory=True, |
| 80 | + reload=config.ENVIRONMENT == "local", |
| 81 | + log_level=config.LOGGER_LEVEL.lower(), |
| 82 | + ) |
| 83 | + |
| 84 | + |
| 85 | +if __name__ == "__main__": |
| 86 | + cli() # run commands in cli() group |
0 commit comments