This repository has been archived by the owner on Nov 29, 2024. It is now read-only.
generated from communitiesuk/funding-service-design-TEMPLATE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
91 lines (73 loc) · 2.86 KB
/
app.py
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
from os import getenv
import connexion
from connexion import FlaskApp
from connexion.resolver import MethodResolver
from flask import jsonify
from fsd_utils import init_sentry
from fsd_utils.healthchecks.checkers import DbChecker, FlaskRunningChecker
from fsd_utils.healthchecks.healthcheck import Healthcheck
from fsd_utils.logging import logging
from fsd_utils.services.aws_extended_client import SQSExtendedClient
from api.routes.application.routes import ApplicationsView # noqa
from config import Config
from db.exceptions.application import ApplicationError
from openapi.utils import get_bundled_specs
def create_app() -> FlaskApp:
init_sentry()
connexion_app = connexion.App(
__name__,
)
connexion_app.add_api(
get_bundled_specs("/openapi/api.yml"),
validate_responses=True,
resolver_error=501,
resolver=MethodResolver("api"),
)
# Configure Flask App
flask_app = connexion_app.app
flask_app.config.from_object("config.Config")
# Initialise logging
logging.init_app(flask_app)
# Initialize sqs extended client
create_sqs_extended_client(flask_app)
from db import db, migrate
# Bind SQLAlchemy ORM to Flask app
db.init_app(flask_app)
# Bind Flask-Migrate db utilities to Flask app
migrate.init_app(flask_app, db, directory="db/migrations", render_as_batch=True)
# Add healthchecks to flask_app
health = Healthcheck(flask_app)
health.add_check(FlaskRunningChecker())
health.add_check(DbChecker(db))
@flask_app.errorhandler(ApplicationError)
def handle_application_error(error):
response = jsonify({"detail": str(error)})
response.status_code = 500
return response
return connexion_app
def create_sqs_extended_client(flask_app):
if (
getenv("AWS_ACCESS_KEY_ID", "Access Key Not Available") == "Access Key Not Available"
and getenv("AWS_SECRET_ACCESS_KEY", "Secret Key Not Available") == "Secret Key Not Available"
):
flask_app.extensions["sqs_extended_client"] = SQSExtendedClient(
region_name=Config.AWS_REGION,
endpoint_url=getenv("AWS_ENDPOINT_OVERRIDE", None),
large_payload_support=Config.AWS_MSG_BUCKET_NAME,
always_through_s3=True,
delete_payload_from_s3=True,
logger=flask_app.logger,
)
else:
flask_app.extensions["sqs_extended_client"] = SQSExtendedClient(
aws_access_key_id=Config.AWS_ACCESS_KEY_ID,
aws_secret_access_key=Config.AWS_SECRET_ACCESS_KEY,
region_name=Config.AWS_REGION,
endpoint_url=getenv("AWS_ENDPOINT_OVERRIDE", None),
large_payload_support=Config.AWS_MSG_BUCKET_NAME,
always_through_s3=True,
delete_payload_from_s3=True,
logger=flask_app.logger,
)
app = create_app()
application = app.app