mirror of
https://github.com/GSA/notifications-api.git
synced 2026-01-28 13:31:54 -05:00
- NOTE - this does not manage secrets. There is only one URL and there is no functionality implemented - prior to rolling out we need to store secrets properly Uses the JWT libraries in [https://github.com/alphagov/notifications-python-client](https://github.com/alphagov/notifications-python-client) - Tokens are checked on every request and will be rejected if token is invalid as per the rules in the python clients.
74 lines
2.1 KiB
Python
74 lines
2.1 KiB
Python
import os
|
|
|
|
from flask._compat import string_types
|
|
from flask import Flask, _request_ctx_stack
|
|
from werkzeug.local import LocalProxy
|
|
from config import configs
|
|
|
|
api_user = LocalProxy(lambda: _request_ctx_stack.top.api_user)
|
|
|
|
|
|
def create_app(config_name):
|
|
application = Flask(__name__)
|
|
|
|
application.config['NOTIFY_API_ENVIRONMENT'] = config_name
|
|
application.config.from_object(configs[config_name])
|
|
|
|
init_app(application)
|
|
|
|
from .main import main as main_blueprint
|
|
application.register_blueprint(main_blueprint)
|
|
|
|
return application
|
|
|
|
|
|
def init_app(app):
|
|
for key, value in app.config.items():
|
|
if key in os.environ:
|
|
app.config[key] = convert_to_boolean(os.environ[key])
|
|
|
|
|
|
def convert_to_boolean(value):
|
|
"""Turn strings to bools if they look like them
|
|
|
|
Truthy things should be True
|
|
>>> for truthy in ['true', 'on', 'yes', '1']:
|
|
... assert convert_to_boolean(truthy) == True
|
|
|
|
Falsey things should be False
|
|
>>> for falsey in ['false', 'off', 'no', '0']:
|
|
... assert convert_to_boolean(falsey) == False
|
|
|
|
Other things should be unchanged
|
|
>>> for value in ['falsey', 'other', True, 0]:
|
|
... assert convert_to_boolean(value) == value
|
|
"""
|
|
if isinstance(value, string_types):
|
|
if value.lower() in ['t', 'true', 'on', 'yes', '1']:
|
|
return True
|
|
elif value.lower() in ['f', 'false', 'off', 'no', '0']:
|
|
return False
|
|
|
|
return value
|
|
|
|
|
|
def convert_to_number(value):
|
|
"""Turns numeric looking things into floats or ints
|
|
|
|
Integery things should be integers
|
|
>>> for inty in ['0', '1', '2', '99999']:
|
|
... assert isinstance(convert_to_number(inty), int)
|
|
|
|
Floaty things should be floats
|
|
>>> for floaty in ['0.99', '1.1', '1000.0000001']:
|
|
... assert isinstance(convert_to_number(floaty), float)
|
|
|
|
Other things should be unchanged
|
|
>>> for value in [0, 'other', True, 123]:
|
|
... assert convert_to_number(value) == value
|
|
"""
|
|
try:
|
|
return float(value) if "." in value else int(value)
|
|
except (TypeError, ValueError):
|
|
return value
|