mirror of https://github.com/mitsuhiko/flask.git
Kyle Lawlor
8 years ago
10 changed files with 95 additions and 62 deletions
@ -0,0 +1,3 @@
|
||||
from flaskr.factory import create_app |
||||
|
||||
app = create_app() |
@ -0,0 +1,52 @@
|
||||
import os |
||||
from flask import Flask, g |
||||
from werkzeug.utils import find_modules, import_string |
||||
from flaskr.blueprints.flaskr import init_db |
||||
|
||||
|
||||
def create_app(config=None): |
||||
app = Flask(__name__) |
||||
|
||||
app.config.update(dict( |
||||
DATABASE=os.path.join(app.root_path, 'flaskr.db'), |
||||
DEBUG=True, |
||||
SECRET_KEY='development key', |
||||
USERNAME='admin', |
||||
PASSWORD='default' |
||||
)) |
||||
app.config.update(config or {}) |
||||
app.config.from_envvar('FLASKR_SETTINGS', silent=True) |
||||
|
||||
register_blueprints(app) |
||||
register_cli(app) |
||||
register_teardowns(app) |
||||
|
||||
return app |
||||
|
||||
|
||||
def register_blueprints(app): |
||||
"""Register all blueprint modules |
||||
|
||||
Reference: Armin Ronacher, "Flask for Fun and for Profit" PyBay 2016. |
||||
""" |
||||
for name in find_modules('flaskr.blueprints'): |
||||
mod = import_string(name) |
||||
if hasattr(mod, 'bp'): |
||||
app.register_blueprint(mod.bp) |
||||
return None |
||||
|
||||
|
||||
def register_cli(app): |
||||
@app.cli.command('initdb') |
||||
def initdb_command(): |
||||
"""Creates the database tables.""" |
||||
init_db() |
||||
print('Initialized the database.') |
||||
|
||||
|
||||
def register_teardowns(app): |
||||
@app.teardown_appcontext |
||||
def close_db(error): |
||||
"""Closes the database again at the end of the request.""" |
||||
if hasattr(g, 'sqlite_db'): |
||||
g.sqlite_db.close() |
Loading…
Reference in new issue