# Path: libforget/cachebust.py
# def cachebust(app):
#     # pylint: disable=unused-variable
# 
#     @app.route('/static-cb/<int:timestamp>/<path:filename>')
#     def static_cachebust(timestamp, filename):
#         path = os.path.join(app.static_folder, filename)
#         try:
#             mtime = os.stat(path).st_mtime
#         except Exception:
#             return abort(404)
#         if abs(mtime - timestamp) > 1:
#             abort(404)
#         else:
#             resp = app.view_functions['static'](filename=filename)
#             resp.headers.set(
#                 'cache-control',
#                 'public, immutable, max-age={}'.format(60*60*24*365))
#             if 'expires' in resp.headers:
#                 resp.headers.remove('expires')
#             return resp
# 
#     @app.context_processor
#     def replace_url_for():
#         return dict(url_for=cachebust_url_for)
# 
#     def cachebust_url_for(endpoint, **kwargs):
#         if endpoint == 'static':
#             endpoint = 'static_cachebust'
#             path = os.path.join(app.static_folder, kwargs.get('filename'))
#             kwargs['timestamp'] = int(os.stat(path).st_mtime)
#         return url_for(endpoint, **kwargs)
# 
#     return cachebust_url_for


from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import MetaData
from flask_migrate import Migrate
from libforget.cachebust import cachebust
from werkzeug.middleware.proxy_fix import ProxyFix
    from raven.contrib.flask import Sentry
import version
import mimetypes
import libforget.brotli
import libforget.img_proxy
        "CHANGELOG_URL": "https://github.com/codl/forget/blob/{hash}/CHANGELOG.markdown",
        "REDIS_URI": "redis://",
}

app.config.update(default_config)

app.config.from_pyfile('config.py', True)

metadata = MetaData(naming_convention={
    "ix": 'ix_%(column_0_label)s',
    "uq": "uq_%(table_name)s_%(column_0_name)s",
    "ck": "ck_%(table_name)s_%(constraint_name)s",
    "fk": "fk_%(table_name)s_%(column_0_name)s_%(referred_table_name)s",
    "pk": "pk_%(table_name)s"
})

db = SQLAlchemy(app, metadata=metadata)
migrate = Migrate(app, db)

if 'CELERY_BROKER' not in app.config:
    uri = app.config['REDIS_URI']
    if uri.startswith('unix://'):
        uri = uri.replace('unix', 'redis+socket', 1)
    app.config['CELERY_BROKER'] = uri

sentry = None
if 'SENTRY_DSN' in app.config:
    app.config['SENTRY_CONFIG']['release'] = version.get_versions()['version']
    sentry = Sentry(app, dsn=app.config['SENTRY_DSN'])


# Path: libforget/cachebust.py
# def cachebust(app):
#     # pylint: disable=unused-variable
# 
#     @app.route('/static-cb/<int:timestamp>/<path:filename>')
#     def static_cachebust(timestamp, filename):
#         path = os.path.join(app.static_folder, filename)
#         try:
#             mtime = os.stat(path).st_mtime
#         except Exception:
#             return abort(404)
#         if abs(mtime - timestamp) > 1:
#             abort(404)
#         else:
#             resp = app.view_functions['static'](filename=filename)
#             resp.headers.set(
#                 'cache-control',
#                 'public, immutable, max-age={}'.format(60*60*24*365))
#             if 'expires' in resp.headers:
#                 resp.headers.remove('expires')
#             return resp
# 
#     @app.context_processor
#     def replace_url_for():
#         return dict(url_for=cachebust_url_for)
# 
#     def cachebust_url_for(endpoint, **kwargs):
#         if endpoint == 'static':
#             endpoint = 'static_cachebust'
#             path = os.path.join(app.static_folder, kwargs.get('filename'))
#             kwargs['timestamp'] = int(os.stat(path).st_mtime)
#         return url_for(endpoint, **kwargs)
# 
#     return cachebust_url_for


# Path: app.py
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import MetaData
from flask_migrate import Migrate
from libforget.cachebust import cachebust
from werkzeug.middleware.proxy_fix import ProxyFix
    from raven.contrib.flask import Sentry
import version
import mimetypes
import libforget.brotli
import libforget.img_proxy

        "CHANGELOG_URL": "https://github.com/codl/forget/blob/{hash}/CHANGELOG.markdown",
        "REDIS_URI": "redis://",
}

app.config.update(default_config)

app.config.from_pyfile('config.py', True)

metadata = MetaData(naming_convention={
    "ix": 'ix_%(column_0_label)s',
    "uq": "uq_%(table_name)s_%(column_0_name)s",
    "ck": "ck_%(table_name)s_%(constraint_name)s",
    "fk": "fk_%(table_name)s_%(column_0_name)s_%(referred_table_name)s",
    "pk": "pk_%(table_name)s"
})

db = SQLAlchemy(app, metadata=metadata)
migrate = Migrate(app, db)

if 'CELERY_BROKER' not in app.config:
    uri = app.config['REDIS_URI']
    if uri.startswith('unix://'):
        uri = uri.replace('unix', 'redis+socket', 1)
    app.config['CELERY_BROKER'] = uri

sentry = None
if 'SENTRY_DSN' in app.config:
    app.config['SENTRY_CONFIG']['release'] = version.get_versions()['version']
    sentry = Sentry(app, dsn=app.config['SENTRY_DSN'])


url_for = cachebust(app)