def initialize_app(flask_app): blueprint = Blueprint('api', __name__, url_prefix='/api') api.init_app(blueprint) api.add_namespace(routes.ns) flask_app.config['SWAGGER_UI_DOC_EXPANSION'] = 'list' flask_app.config['RESTPLUS_VALIDATE'] = True flask_app.config['RESTPLUS_MASK_SWAGGER'] = False flask_app.register_blueprint(blueprint)
def create_app(app_name='HOMEWORK_API'): app = Flask(app_name) app.config.from_object('v1.config') api.init_app(app) app.register_blueprint(rc) CORS(app, supports_credentials=True) app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024 # 初始化app return app
def create_app(): app = Flask(__name__, static_url_path='/static') app.config['DEBUG'] = True app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///database.db' app.secret_key = 'thisissecrett' api.init_app(app) db.init_app(app) return app
from flask import Flask from routes import api app = Flask(__name__) api.init_app(app) if __name__ == '__main__': app.run(debug=True)
def create_flask_app(environment): app = Flask(__name__, instance_relative_config=True, static_folder=None, template_folder='./api/emails/templates') # enabe CORS cors = CORS(app, resources={r"/*": {"origins": "*"}}) app.config.from_object(app_configuration[environment]) app.config['BUNDLE_ERRORS'] = True # initialize SQLAlchemy db.init_app(app) # initialize migration commands migrate = Migrate(app, db) # initialize flask script manager manager = Manager(app) # Add database migration command manager.add_command('db', MigrateCommand) # landing route @app.route('/') def index(): return "API'S ARE LIVE" # initialize api resources api.init_app(app) # handle default 404 exceptions with a custom response @app.errorhandler(404) def resource_not_found(exception): response = jsonify( dict(status='fail', data={ 'error': 'Not found', 'message': 'The requested URL was' ' not found on the server. If you entered the URL ' 'manually please check and try again' })) response.status_code = 404 return response # both error handlers below handle default 500 exceptions with a custom # response @app.errorhandler(500) def internal_server_error(error): response = jsonify( dict(status=error, error='Internal Server Error', message='The server encountered an internal error and was' ' unable to complete your request. Either the server is' ' overloaded or there is an error in the application')) response.status_code = 500 return response environment = os.getenv('FLASK_CONFIG') return app
from flask import Flask from config import VERSION, SQLALCHEMY_DATABASE_URI, SECRET_KEY from models import db from routes import api app = Flask(__name__) app.config['SECRET_KEY'] = SECRET_KEY app.config['SQLALCHEMY_DATABASE_URI'] = SQLALCHEMY_DATABASE_URI app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True # https://flask-sqlalchemy.palletsprojects.com/en/2.x/api/#flask_sqlalchemy.SQLAlchemy.init_app db.init_app(app) # https://flask-restx.readthedocs.io/en/latest/api.html#flask_restx.Api.init_app api.init_app(app, version=VERSION, title='Company API', description='A company name & tag API') if __name__ == '__main__': app.run(host='0.0.0.0', debug=True)