2023-04-20 13:10:16 +00:00
|
|
|
import os
|
|
|
|
|
|
|
|
from flask import Flask, render_template
|
|
|
|
from flask_sqlalchemy import SQLAlchemy
|
|
|
|
from flask_login import LoginManager
|
|
|
|
from werkzeug.exceptions import HTTPException
|
|
|
|
from flask_migrate import Migrate
|
|
|
|
|
|
|
|
from app.logger import log
|
|
|
|
|
|
|
|
# instantiate extensions
|
|
|
|
login_manager = LoginManager()
|
|
|
|
db = SQLAlchemy()
|
|
|
|
migration = Migrate()
|
|
|
|
|
|
|
|
|
|
|
|
def create_app(environment="development"):
|
|
|
|
|
|
|
|
from config import config
|
|
|
|
from app.views import (
|
|
|
|
main_blueprint,
|
|
|
|
auth_blueprint,
|
|
|
|
user_blueprint,
|
2023-04-24 08:31:54 +00:00
|
|
|
book_blueprint,
|
2023-04-21 11:40:20 +00:00
|
|
|
home_blueprint,
|
2023-04-20 13:10:16 +00:00
|
|
|
)
|
|
|
|
from app.models import (
|
|
|
|
User,
|
|
|
|
AnonymousUser,
|
|
|
|
)
|
|
|
|
|
|
|
|
# Instantiate app.
|
|
|
|
app = Flask(__name__)
|
|
|
|
|
|
|
|
# Set app config.
|
|
|
|
env = os.environ.get("APP_ENV", environment)
|
|
|
|
configuration = config(env)
|
|
|
|
app.config.from_object(configuration)
|
|
|
|
configuration.configure(app)
|
|
|
|
log(log.INFO, "Configuration: [%s]", configuration.ENV)
|
|
|
|
|
|
|
|
# Set up extensions.
|
|
|
|
db.init_app(app)
|
|
|
|
migration.init_app(app, db)
|
|
|
|
login_manager.init_app(app)
|
|
|
|
|
|
|
|
# Register blueprints.
|
|
|
|
app.register_blueprint(auth_blueprint)
|
|
|
|
app.register_blueprint(main_blueprint)
|
|
|
|
app.register_blueprint(user_blueprint)
|
2023-04-24 08:31:54 +00:00
|
|
|
app.register_blueprint(book_blueprint)
|
2023-04-21 11:40:20 +00:00
|
|
|
app.register_blueprint(home_blueprint)
|
2023-04-20 13:10:16 +00:00
|
|
|
|
|
|
|
# Set up flask login.
|
|
|
|
@login_manager.user_loader
|
|
|
|
def get_user(id):
|
|
|
|
return User.query.get(int(id))
|
|
|
|
|
|
|
|
login_manager.login_view = "auth.login"
|
|
|
|
login_manager.login_message_category = "info"
|
|
|
|
login_manager.anonymous_user = AnonymousUser
|
|
|
|
|
|
|
|
# Error handlers.
|
|
|
|
@app.errorhandler(HTTPException)
|
|
|
|
def handle_http_error(exc):
|
|
|
|
return render_template("error.html", error=exc), exc.code
|
|
|
|
|
|
|
|
return app
|