|
| 1 | +#! ../env/bin/python |
| 2 | + |
| 3 | +from flask import Flask, request, g, redirect |
| 4 | +from logging.handlers import RotatingFileHandler |
| 5 | +import logging |
| 6 | + |
| 7 | +# Controllers |
| 8 | +from app.models.database import db |
| 9 | +from app.controllers.main import main |
| 10 | +from app.controllers.profile import profile |
| 11 | +from app.controllers.auth import auth |
| 12 | +from app.controllers.authorize import authorize |
| 13 | + |
| 14 | +# Api controllers |
| 15 | + |
| 16 | +# External controllers |
| 17 | + |
| 18 | +# Misc |
| 19 | +from app.tpl_filter import tpl_filter |
| 20 | +from app.extensions import ( |
| 21 | + cache, |
| 22 | + debug_toolbar, |
| 23 | + login_manager, |
| 24 | + csrf, |
| 25 | + oauth, |
| 26 | + mailgun, |
| 27 | + babel |
| 28 | +) |
| 29 | + |
| 30 | +def create_app(object_name, env='prod'): |
| 31 | + """ |
| 32 | + An flask application factory, as explained here: |
| 33 | + http://flask.pocoo.org/docs/patterns/appfactories/ |
| 34 | +
|
| 35 | + Arguments: |
| 36 | + object_name: the python path of the config object, e.g. app.settings.ProdConfig |
| 37 | +
|
| 38 | + env: The name of the current environment, e.g. prod or dev |
| 39 | + """ |
| 40 | + app = Flask(__name__, static_url_path='') |
| 41 | + |
| 42 | + app.config.from_object(object_name) |
| 43 | + app.config['ENV'] = env |
| 44 | + |
| 45 | + # templates and statics |
| 46 | + app.template_folder = app.config['TEMPLATE_FOLDER'] |
| 47 | + app.static_folder = app.config['STATIC_FOLDER'] |
| 48 | + |
| 49 | + # initialize the cache |
| 50 | + cache.init_app(app) |
| 51 | + |
| 52 | + # initialize the debug tool bar |
| 53 | + debug_toolbar.init_app(app) |
| 54 | + |
| 55 | + # CSRF protection |
| 56 | + csrf.init_app(app) |
| 57 | + |
| 58 | + # Oauthlib |
| 59 | + oauth.init_app(app) |
| 60 | + |
| 61 | + # initialize SQLAlchemy |
| 62 | + db.init_app(app) |
| 63 | + |
| 64 | + # Authentication |
| 65 | + login_manager.init_app(app) |
| 66 | + |
| 67 | + # Mailgun |
| 68 | + mailgun.init_app(app) |
| 69 | + |
| 70 | + # Languages |
| 71 | + babel.init_app(app) |
| 72 | + |
| 73 | + # Register blueprints |
| 74 | + app.register_blueprint(main) |
| 75 | + app.register_blueprint(auth) |
| 76 | + app.register_blueprint(profile) |
| 77 | + app.register_blueprint(authorize) |
| 78 | + |
| 79 | + # Api |
| 80 | + |
| 81 | + # Extneral |
| 82 | + |
| 83 | + # Import custom template filters |
| 84 | + app.register_blueprint(tpl_filter) |
| 85 | + |
| 86 | + # @babel.localeselector |
| 87 | + # def get_locale(): |
| 88 | + # return g.locale |
| 89 | + |
| 90 | + # # Set lang |
| 91 | + # @app.before_request |
| 92 | + # def before_request(): |
| 93 | + # lang_cookie = request.cookies.get('locale') |
| 94 | + # locale = lang_cookie if lang_cookie else 'en' |
| 95 | + |
| 96 | + # if not lang_cookie: |
| 97 | + # for lang in request.accept_languages.values(): |
| 98 | + # if lang[:2] in ['de', 'en']: |
| 99 | + # locale = lang[:2] |
| 100 | + # break |
| 101 | + |
| 102 | + # g.locale = locale |
| 103 | + |
| 104 | + # if app.config['ENV'] == 'prod': |
| 105 | + # file_handler = RotatingFileHandler('app.log', maxBytes=1024 * 1024 * 100, backupCount=20) |
| 106 | + # formatter = logging.Formatter( "%(asctime)s | %(pathname)s:%(lineno)d | %(funcName)s | %(levelname)s | %(message)s ") |
| 107 | + # file_handler.setFormatter(formatter) |
| 108 | + # app.logger.addHandler(file_handler) |
| 109 | + |
| 110 | + return app |
0 commit comments