コード例 #1
1
ファイル: main.py プロジェクト: olavgg/py-sth
    def start():
        """
        Start the application
        """
        config_type = "DevelopmentConfig"
        if len(sys.argv) == 2:
            if sys.argv[1] == "dev":
                config_type = "DevelopmentConfig"
            elif sys.argv[1] == "test":
                config_type = "TestConfig"
            elif sys.argv[1] == "prod":
                config_type = "ProductionConfig"
        app = Flask(__name__)
        app.config.from_object("conf.config.%s" % config_type)
        app.secret_key = getRandomInteger(128)
        app.permanent_session_lifetime = timedelta(seconds=10)
        FORMAT = "%(asctime)s %(levelname)s: " "%(message)s [in %(pathname)s:%(lineno)d]"
        logging.basicConfig(filename=app.config["LOGFILE"], level=logging.DEBUG, format=FORMAT)
        PYSTHClient.init_controllers(app)

        @app.errorhandler(400)
        def bad_request(exception):
            """Bad Request"""
            data = dict(status=exception.code, error=str(exception), description=bad_request.__doc__)
            return jsonify(data), 400

        @app.errorhandler(404)
        def page_not_found(exception):
            """Page Not Found"""
            data = dict(status=exception.code, error=str(exception), description=page_not_found.__doc__)
            return jsonify(data), 404

        if app.config["DEBUG"] is True:

            @app.errorhandler(500)
            def error(exception):
                """Internal Server Error"""
                data = dict(status=exception.code, error=str(exception), description=error.__doc__)
                return jsonify(data), 500

        @app.errorhandler(403)
        def forbidden(exception):
            """Forbidden"""
            data = dict(status=exception.code, error=str(exception), description=forbidden.__doc__)
            return jsonify(data), 403

        # noinspection PyUnusedLocal
        @app.before_first_request
        def bootstrap():
            """
            Call the bootstrap function
            """
            Bootstrap()

        with app.app_context():
            Base.do_first_request()
        app.run(app.config["HOST"], app.config["PORT"])
コード例 #2
0
ファイル: wsgi.py プロジェクト: olavgg/py-sth
    @APP.errorhandler(500)
    def error(exception):
        """Internal Server Error"""
        data = dict(
            status=exception.code,
            error=str(exception),
            description=error.__doc__
        )
        return jsonify(data), 500


@APP.errorhandler(403)
def forbidden(exception):
    """Forbidden"""
    data = dict(
        status=exception.code,
        error=str(exception),
        description=forbidden.__doc__
    )
    return jsonify(data), 403


@APP.before_first_request
def bootstrap():
    Bootstrap()


with APP.app_context():
    Base.do_first_request()
APP.wsgi_app = ProxyFix(APP.wsgi_app)