Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _init_application(directory, **kwargs):
def custom_headers(headers, path, url):
if url.endswith(".css"):
headers["X-Is-Css-File"] = "True"
return WhiteNoise(
demo_app,
root=directory,
max_age=1000,
mimetypes={".foobar": "application/x-foo-bar"},
add_headers_function=custom_headers,
index_file=True,
**kwargs
)
def init_app(self, app):
"""Initializes the Flask application with Common."""
if not hasattr(app, 'extensions'):
app.extensions = {}
if 'common' in app.extensions:
raise RuntimeError("Flask-Common extension already initialized")
app.extensions['common'] = self
self.app = app
if 'COMMON_FILESERVER_DISABLED' not in app.config:
with app.test_request_context():
# Configure WhiteNoise.
app.wsgi_app = WhiteNoise(app.wsgi_app, root=url_for('static', filename='')[1:])
self.cache = Cache(app, config={'CACHE_TYPE': app.config.get("COMMON_CACHE_TYPE", 'simple')})
@app.before_request
def before_request_callback():
request.start_time = maya.now()
@app.after_request
def after_request_callback(response):
if 'COMMON_POWERED_BY_DISABLED' not in current_app.config:
response.headers['X-Powered-By'] = 'Flask'
if 'COMMON_PROCESSED_TIME_DISABLED' not in current_app.config:
response.headers['X-Processed-Time'] = maya.now().epoch - request.start_time.epoch
return response
@app.route('/favicon.ico')
def __init__(self, root_dir: str=None) -> None:
assert whitenoise is not None, 'whitenoise must be installed.'
from whitenoise import WhiteNoise
self.whitenoise = WhiteNoise(application=None, root=root_dir)
self.whitenoise.add_files(PACKAGE_STATICS, prefix='apistar')
def _create_whitenoise(app, config):
wh_config = config.registry.settings.get("whitenoise", {}).copy()
if wh_config:
# Create our Manifest immutable file checker.
manifest = ImmutableManifestFiles()
for manifest_spec, prefix in config.registry.settings.get(
"whitenoise.manifests", []
):
manifest.add_manifest(manifest_spec, prefix)
# Wrap our WSGI application with WhiteNoise
app = WhiteNoise(app, **wh_config, immutable_file_test=manifest)
# Go through and add all of the files we were meant to add.
for path, kwargs in config.registry.settings.get("whitenoise.files", []):
app.add_files(resolver.resolve(path).abspath(), **kwargs)
return app
integrations=[FlaskIntegration()],
**sentry_extras
)
if cf_app:
with sentry_sdk.configure_scope() as scope:
scope.set_tag('cf_app', cf_app)
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'app', 'static')
STATIC_URL = 'static/'
app = Flask('app')
create_app(app)
application = WhiteNoise(app, STATIC_ROOT, STATIC_URL)
def create_app():
# Initialize Flask instance and enable static file serving
app = Flask(__name__)
app.config.from_object(config[ENV_CONFIG]()) # instance is for __init__
app.wsgi_app = WhiteNoise(app.wsgi_app, root='static/')
# Initialize Sentry for error tracking
sentry.init_app(app)
# Custom Json
app.json_encoder = CustomJSONEncoder
compress.init_app(app)
# Inintialize webpack support
webpack.init_app(app)
# Initialize Mail by Flask-Mail
mail.init_app(app)
# Initialize Database and Migration by Flask-Sqlalchey and Flask-Migrate
db.init_app(app)
[config-attrs]: http://whitenoise.evans.io/en/stable/base.html#configuration-attributes
# Parameters
root (str):
the path to a directory from where static files should be served.
If the directory does not exist, no files will be served.
**kwargs (any):
keyword arguments passed to the WhiteNoise constructor. See also [Configuration attributes (WhiteNoise docs)][config-attrs].
# Returns
app (callable): a WhiteNoise WSGI app.
"""
if exists(root):
kwargs["root"] = root
return WhiteNoise(empty_wsgi_app(), **kwargs)
middleware here, or combine a Django application with an application of another
framework.
"""
import os
from django.core.wsgi import get_wsgi_application
from whitenoise import WhiteNoise
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "eggtimer.settings")
# This application object is used by any WSGI server configured to use this
# file. This includes Django's development server, if the WSGI_APPLICATION
# setting points here.
application = WhiteNoise(get_wsgi_application())