generated from ministryofjustice/template-repository
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
♻️ Refactor app configuration into modular files (#164)
- Loading branch information
1 parent
cf6e3f8
commit bdfc932
Showing
15 changed files
with
144 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from flask import Flask | ||
from flask_cors import CORS | ||
|
||
|
||
def configure_cors(app: Flask) -> None: | ||
CORS(app, resources={r"/*": {"origins": "*", "send_wildcard": "False"}}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from flask import Flask | ||
|
||
from app.main.middleware.error_handler import ( | ||
client_error, | ||
page_not_found, | ||
server_forbidden, | ||
unknown_server_error, | ||
) | ||
|
||
|
||
def configure_error_handlers(app: Flask) -> None: | ||
app.register_error_handler(400, client_error) | ||
app.register_error_handler(403, server_forbidden) | ||
app.register_error_handler(404, page_not_found) | ||
app.register_error_handler(500, unknown_server_error) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from flask import Flask | ||
from jinja2 import ChoiceLoader, PackageLoader, PrefixLoader | ||
|
||
from app.main.config.app_config import app_config | ||
|
||
|
||
def configure_jinja(app: Flask) -> None: | ||
app.jinja_loader = ChoiceLoader( | ||
[ | ||
PackageLoader("app"), | ||
PrefixLoader( | ||
{"govuk_frontend_jinja": PackageLoader("govuk_frontend_jinja")} | ||
), | ||
] | ||
) | ||
app.jinja_env.trim_blocks = True | ||
app.jinja_env.lstrip_blocks = True | ||
app.jinja_env.globals["phase_banner_text"] = app_config.phase_banner_text |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from flask import Flask | ||
from flask_limiter import Limiter | ||
from flask_limiter.util import get_remote_address | ||
|
||
|
||
def configure_limiter(app: Flask, is_rate_limit_enabled: bool = True) -> None: | ||
Limiter( | ||
get_remote_address, | ||
app=app, | ||
default_limits=["10 per minute", "2 per second"], | ||
storage_uri="memory://", | ||
strategy="moving-window", | ||
enabled=is_rate_limit_enabled, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import logging | ||
|
||
|
||
def configure_logging(logging_level: str) -> None: | ||
logging.basicConfig( | ||
format="{asctime:s} | {levelname:>8s} | {filename:s}:{lineno:d} | {message:s}", | ||
datefmt="%Y-%m-%dT%H:%M:%S", | ||
style="{", | ||
level=logging_level.upper() if logging_level else "INFO", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from flask import Flask | ||
|
||
from app.main.routes.auth import auth_route | ||
from app.main.routes.join import join_route | ||
from app.main.routes.main import main | ||
from app.main.routes.robots import robot_route | ||
|
||
|
||
def configure_routes(app: Flask) -> None: | ||
app.register_blueprint(auth_route, url_prefix="/auth") | ||
app.register_blueprint(join_route, url_prefix="/join") | ||
app.register_blueprint(main) | ||
app.register_blueprint(robot_route) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import logging | ||
|
||
import sentry_sdk | ||
from sentry_sdk.integrations.flask import FlaskIntegration | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def configure_sentry(dsn_key: str, environment: str) -> None: | ||
if not dsn_key: | ||
logger.warning("Missing Sentry DSN Key") | ||
|
||
if not environment: | ||
logger.warning("Missing Sentry Environment") | ||
|
||
if dsn_key and environment: | ||
logger.info(f"Configuring Sentry for environment [ {environment} ]") | ||
sentry_sdk.init( | ||
dsn=dsn_key, | ||
environment=environment, | ||
integrations=[FlaskIntegration()], | ||
enable_tracing=True, | ||
traces_sample_rate=0.1, | ||
) | ||
logger.info("Sentry configured successfully") | ||
else: | ||
logger.warning( | ||
"Sentry not configured due to either missing DSN Key or Environment" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters