Example #1
0
def create_app():
    app = Flask(__name__)
    app.config.from_object("config")

    mongodb_database = MongoDBConn(host=app.config.get("MONGODB_HOST"), port=app.config.get("MONGODB_PORT"))

    mongodb_conn = mongodb_database[app.config.get("MONGODB_DATABASE")]

    from .models import User, Task, Project

    mongodb_database.register([User, Task, Project])

    app.mongodb_database = mongodb_database
    app.mongodb_conn = mongodb_conn

    from .api_v1 import api as api_blueprint
    from .open_api import open_api as open_api_blueprint

    app.register_blueprint(api_blueprint, url_prefix="/api/v1")
    app.register_blueprint(open_api_blueprint, url_prefix="/open_api")

    @app.before_request
    def app_before_request():
        if request.method == "OPTIONS":
            resp = current_app.make_default_options_response()
            cors_headers = {
                "Access-Control-Allow-Headers": "Origin, Accept, Content-Type, Authorization",
                "Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, OPTIONS, HEAD",
                "Access-Control-Allow-Origin": "*",
            }
            resp.headers.extend(cors_headers)
            return resp
        return

    @app.after_request
    def after_request(rv):
        headers = getattr(g, "headers", {})
        rv.headers.extend(headers)
        return rv

    return app
Example #2
0
def create_app(config_name="development"):
    config_name = CONFIG_NAME or config_name

    app = Flask(__name__)

    app.version = __version__
    app.artisan = __artisan__

    # config
    app.config.from_object(config[config_name])
    app.json_encoder = Encoder
    app.debug = app.config.get("DEBUG")

    # logging
    if app.config.get("TESTING") is True:
        app.logger.setLevel(logging.FATAL)
    else:
        error_file_handler = RotatingFileHandler(
            app.config.get("LOGGING")["error"]["file"],
            maxBytes=app.config.get("LOGGING_ROTATING_MAX_BYTES"),
            backupCount=app.config.get("LOGGING_ROTATING_BACKUP_COUNT"),
        )

        error_file_handler.setLevel(logging.WARNING)
        error_file_handler.setFormatter(logging.Formatter(app.config.get("LOGGING")["error"]["format"]))

        app.logger.addHandler(error_file_handler)

    # database connections
    app.mongodb_database = MongodbConn(host=app.config.get("DB_HOST"), port=app.config.get("DB_PORT"))
    app.mongodb_conn = app.mongodb_database[app.config.get("DB_DBNAME")]

    app.sup_oauth = SupOAuth(
        ext_key=app.config.get("EXT_KEY"),
        ext_secret=app.config.get("EXT_SECRET"),
        secret_key=app.config.get("SECRET_KEY"),
        expires_in=app.config.get("EXPIRES_IN"),
        api_uri=app.config.get("OAUTH_API_URI"),
        token_uri=app.config.get("OAUTH_TOKEN_API_URI"),
        redirect_uri=app.config.get("OAUTH_REDIRECT_URI"),
    )

    from blueprints.user.models import ExtUser

    app.mongodb_database.register([ExtUser])

    # register blueprints
    from blueprints.user import blueprint as user_module

    app.register_blueprint(user_module, url_prefix="/user")

    from blueprints.comment import blueprint as comment_module

    app.register_blueprint(comment_module, url_prefix="/comment")

    # register error handlers
    @app.errorhandler(404)
    def app_error_404(error):
        current_app.logger.warn("Error: 404\n{}".format(traceback.format_exc()))
        return make_json_response(NotFound())

    @app.errorhandler(405)
    def app_error_405(error):
        current_app.logger.warn("Error: 405\n{}".format(traceback.format_exc()))
        return make_json_response(MethodNotAllowed())

    @app.errorhandler(Exception)
    def app_error_uncaught(error):
        current_app.logger.warn("Error: Uncaught\n{}".format(traceback.format_exc()))
        return make_json_response(UncaughtException(repr(error)))

    @app.before_request
    def app_before_request():
        # cors response
        if request.method == "OPTIONS":
            resp = current_app.make_default_options_response()
            cors_headers = make_cors_headers()
            resp.headers.extend(cors_headers)
            return resp

    print "-------------------------------------------------------"
    print "Comment Extension: {}".format(app.version)
    print "Developers: {}".format(", ".join(app.artisan))
    print "-------------------------------------------------------"

    return app