def create_app(): app = Flask(__name__, static_folder='../static', static_url_path='') CORS(app) Compress(app) app.app_context().push() # 设置接口响应数据格式 app.config['JSON_AS_ASCII'] = False # 连接数据库 app.permanent_session_lifetime = datetime.timedelta(seconds=720 * 60) app.config[ 'SQLALCHEMY_DATABASE_URI'] = 'postgresql://*****:*****@127.0.0.1:5432/island' app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True app.config['SQLALCHEMY_COMMIT_ON_TEARDOWN'] = False app.config['SQLALCHEMY_ECHO'] = False app.config['JWT_SECRET_KEY'] = 'UbuQgGIdry*H&&I@' app.config['JWT_BLACKLIST_ENABLED'] = False app.config['JWT_BLACKLIST_TOKEN_CHECKS'] = ['access', 'refresh'] db.init_app(app) db.create_all() # 注册蓝图 app.register_blueprint(auth.blueprint) # 注册JWT jwt = JWTManager(app) # Set the secret key to some random bytes. Keep this really secret! app.secret_key = b'_5#y2L"F4Q8z\n\xec]hhfhfj8989jjja/' # 日志 LoggingINFO.logging_setting() return app
def create_app(): ''' This function creates our flask application instance and loads in all environment dependencies ''' app = Flask(__name__, template_folder="templates", static_folder="static") db.get_app(app) login_manager = LoginManager() login_manager.login_view = 'admin.login_admin' login_manager.init_app(app) @login_manager.user_loader def load_user(email): # since the user_id is just the primary key of our user table, use it in the query for the user return User.find_user_by_email(email=email) app.config.from_object(Configuration) db.init_app(app) migrate.init_app(app, db) mail.init_app(app) # major blueprints for routing from main import admin as admin_blue_print app.register_blueprint(admin_blue_print) from auth import email as email_blue_print app.register_blueprint(email_blue_print) return app
def create_app(): app = Flask(__name__) app.config.from_object(config) from app import api_bp app.register_blueprint(api_bp, url_prefix='/api') from Model import db db.app = app db.init_app(app) scheduler = APScheduler() scheduler.init_app(app) scheduler.start() @app.route('/') def index(): return render_template('index.html', token='yes') @app.after_request def add_header(r): r.headers["Cache-Control"] = "no-cache, no-store, must-revalidate" r.headers["Pragma"] = "no-cache" r.headers["Expires"] = "0" r.headers['Cache-Control'] = 'public, max-age=0' return r return app
def create_app(config_filename): app = Flask(__name__) app.config.from_object(config_filename) app.register_blueprint(api_bp, prefix="/api/v1") db.init_app(app) return app
def create_app(config_filename): app = Flask(__name__) app.config.from_object(config_filename) @app.route("/index.html") def index(): return render_template("index.html") @app.route("/about.html") def about(): return render_template("about.html") from app import api_bp, signup_bp, login_bp, confirmation_bp, diffuseur_bp app.register_blueprint(api_bp, url_prefix='/api') app.register_blueprint(login_bp, url_prefix='/login') app.register_blueprint(signup_bp, url_prefix='/signup') app.register_blueprint(confirmation_bp, url_prefix='/confirm_email') app.register_blueprint(diffuseur_bp, url_prefix='/diffuseur') from resources.ConfirmEmail import mail mail.init_app(app) from Model import db db.init_app(app) return app
def create_app(config_filename): app = Flask(__name__) app.config.from_object(config_filename) from app import api_bp app.register_blueprint(api_bp, url_prefix='') from Model import db db.init_app(app) return app
def create_app(config_filename): app = Flask(__name__) app.config.from_object(config_filename) app.register_blueprint(api_bp, url_prefix='/api') with app.app_context(): db.init_app(app) db.create_all() return app
def create_app(config_filename): app = Flask(__name__) app.config.from_object(config_filename) from app import api_bp app.register_blueprint(api_bp, url_prefix='/api') from Model import db from resources.auth import mail mail.init_app(app) db.init_app(app) return app
def create_app(config_filename): app = Flask(__name__, static_folder="./build/") app.config.from_object(config_filename) from app import api_bp app.register_blueprint(api_bp, url_prefix='/api') from Model import db db.init_app(app) return app
def create_app(config): app = Flask(__name__) # Handling CORS (Cross Origin Resource Sharing) easily using thr flask_cors extension CORS(app) app.config.from_object(config) db.init_app(app) return app
def create_app(config_filename): app = Flask(__name__) app.config.from_object(config_filename) # app.config.from_envvar('APPLICATION_SETTINGS') from api import api_bp app.register_blueprint(api_bp) from Model import db db.init_app(app) return app
def create_app(config_filename): app = Flask(__name__) app.config.from_object(config_filename) from blueprints.UserBlueprint import user_blueprint from blueprints.PostBlueprint import post_blueprint app.register_blueprint(user_blueprint, url_prefix='/user') app.register_blueprint(post_blueprint, url_prefix='/api') from Model import db db.init_app(app) return app
def create_app(config_filename): asyncio.set_event_loop(asyncio.new_event_loop()) app = Flask(__name__) app.config.from_object(config_filename) from app import api_bp app.register_blueprint(api_bp, url_prefix='/api') from Model import db db.init_app(app) return app
def create_app(config_filename): app = Flask(__name__) app.config.from_object(config_filename) from app import api_bp app.register_blueprint(api_bp, url_prefix='/api') db.init_app(app) # 解決跨域 CORS(app, supports_credentials=True) return app
def create_app(): app = Flask(__name__) app.config.from_object(os.environ['APP_SETTINGS']) app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False from app import api_bp app.register_blueprint(api_bp, url_prefix='/api') from Model import db db.init_app(app) return app
def api_users(config_filename): app = Flask(__name__) app.config.from_object(config_filename) app.secret_key = "SECRET_KEY" jwt = JWT(app, authenticate, identity) from app import api_bp app.register_blueprint(api_bp, url_prefix='/api') from Model import db db.init_app(app) return app
def create_app(config_filename): app = Flask(__name__) app.config.from_object(config_filename) from app import api_bp # Created blueprint is being registered here app.register_blueprint(api_bp, url_prefix='/api') # Import our model from Model import db db.init_app(app) return app
def create_app(config_filename): app = Flask(__name__) app.config.from_object(config_filename) from app import api_bp app.register_blueprint(api_bp, url_prefix='/api') from Model import db db.init_app(app) from flask_jwt_extended import JWTManager JWTManager(app) return app
def create_app(config_filename): app = Flask(__name__) app.config.from_object(config_filename) PROJECT_HOME = os.path.dirname(os.path.realpath(__file__)) UPLOAD_FOLDER = '{}/uploads/'.format(PROJECT_HOME) app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER from app import api_bp app.register_blueprint(api_bp, url_prefix='/api') from Model import db db.init_app(app) return app
def create_app(config_filename): app = Flask(__name__) app.config.from_object(config_filename) # app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True from app import api_bp app.register_blueprint(api_bp, url_prefix='/api') from Model import db db.init_app(app) # from app import api_bp # db.init_app(app) return app
def create_app(config_filename): app = Flask(__name__, static_folder="./templates") app.config.from_object(config_filename) @app.route('/') def index(): return render_template('index.html') from app import api_bp app.register_blueprint(api_bp, url_prefix='/api') from Model import db db.init_app(app) return app
def create_app(config_filename): app = Flask(__name__) # flask_cors.CORS(app, expose_headers='Authorization') app.config.from_object(config_filename) from app import api_bp # Legger til CORS-funksjonalitet. Har ikke peiling hvordan det fungerer CORS(api_bp) app.register_blueprint(api_bp, url_prefix='/api') from Model import db db.init_app(app) jwt = JWTManager(app) @jwt.token_in_blacklist_loader def check_if_token_in_blacklist(decrypted_token): jti = decrypted_token['jti'] return RevokedTokenModel.is_jti_blacklisted(jti) # Legger til en melding som blir sendt om en bruker prøver å logge på med en utgått token. @jwt.revoked_token_loader def my_revoked_token_callback(): return jsonify({ 'status': 401, 'sub_status': 43, 'msg': 'The token has been revoked. Try to login again for renewed access.' }), 401 @jwt.expired_token_loader def my_expired_token_callback(): return jsonify({ 'status': 401, 'sub_status': 42, 'msg': 'The token has expired. Try to login again for renewed access.' }), 401 return app
def create_app(config_filename): app = Flask(__name__) CORS(app) # cors = CORS(app, resources={r"/foo": {"origins": "http://localhost:port"}}) # CORS(app, origins="http://localhost:5000", allow_headers=[ # "Content-Type", "Authorization", "Access-Control-Allow-Credentials"], # supports_credentials=True) app.config.from_object(config_filename) app.config['SECRET_KEY'] = 'Elegance-1234' from app import api_bp app.register_blueprint(api_bp, url_prefix='/api') from Model import db db.init_app(app) return app
def create_app(config_filename): """ Instantiates the Flask instance and associated parameters. :params: configuration filename :return: flask app :rtype: flask object """ app = Flask(__name__) app.config.from_object(config_filename) from app import api_bp app.register_blueprint(api_bp, url_prefix='/api') from Model import db with app.app_context(): db.init_app(app) db.create_all([None]) return app
def create_app(config_filename): app = Flask(__name__) app.config.from_object(config_filename) from app import api_bp app.register_blueprint(api_bp, url_prefix='/api') from Model import db db.init_app(app) from flask_jwt_extended import JWTManager jwt = JWTManager(app) @jwt.token_in_blacklist_loader def check_if_token_in_blacklist(decrypted_token): jti = decrypted_token['jti'] query = RevokedToken.query.filter_by(jti=jti).first() return query return app
def create_app(config_filename): app = Flask(__name__) CORS(app) cors = CORS(app, resources={r"/api/*": {"origins": "*"}}) app.config.from_object(config_filename) from app import api_bp app.register_blueprint(api_bp, url_prefix='/') from Model import db db.init_app(app) return app
from flask import Flask from flask_cors import CORS app = Flask(__name__) CORS(app) app.config.from_object("config") from app import api_bp app.register_blueprint(api_bp, url_prefix='/api') from Model import db db.init_app(app) if __name__ == "__main__": app.run()
from werkzeug.contrib.fixers import ProxyFix from celery import Celery import os import sys reload(sys) sys.setdefaultencoding('utf-8') basedir = os.path.abspath(os.path.dirname(__file__)) app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI']= 'sqlite:///' + os.path.join(basedir, 'data.sqlite') bootstrap = Bootstrap(app) db.app = app db.init_app(app) # Celery configuration app.config['CELERY_BROKER_URL'] = 'redis://localhost:6379/0' app.config['CELERY_RESULT_BACKEND'] = 'redis://localhost:6379/0' # Initialize Celery celery = Celery(app.name, broker=app.config['CELERY_BROKER_URL']) celery.conf.update(app.config) # celery operation in background @celery.task def addTalk(cont, locat): with app.app_context(): newPost = Post(content=cont, location=locat) db.session.add(newPost)
def create_app(config_filename): app = Flask(__name__, static_folder = './public', template_folder="./static") logging.basicConfig(filename='fiplanner.log', level=logging.DEBUG, format='%(asctime)s %(levelname)s %(name)s %(threadName)s : %(message)s') load_dotenv(find_dotenv()) #@app.before_first_request #def init_configs(): app.config['SECRET_KEY'] = os.environ.get('SECRET_KEY') app.config['JWT_SECRET_KEY'] = os.environ.get('JWT_SECRET_KEY') app.config['JWT_ACCESS_TOKEN_EXPIRES'] = datetime.timedelta(days=1) app.config['JWT_BLACKLIST_ENABLED'] = True app.config['JWT_BLACKLIST_TOKEN_CHECKS'] = ['access', 'refresh'] app.config['JWT_TOKEN_LOCATION'] = ['cookies'] # Only allow JWT cookies to be sent over https. In production, this should likely be True app.config['JWT_COOKIE_SECURE'] = False app.config['JWT_ACCESS_COOKIE_PATH'] = '/api/' app.config['JWT_REFRESH_COOKIE_PATH'] = '/api/Token' # Twilio verify key app.config['VERIFICATION_SID'] = os.environ.get('VERIFICATION_SID') # Enable csrf double submit protection. See this for a thorough # explanation: http://www.redotheweb.com/2015/11/09/api-security.html app.config['JWT_COOKIE_CSRF_PROTECT'] = True app.config['SQLALCHEMY_ECHO'] = False app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True project_dir = os.path.dirname(os.path.abspath(__file__)) p = Path(project_dir).parents[0] database_file = "sqlite:///{}".format(os.path.join(p, "fiplanner.db")) app.config['SQLALCHEMY_DATABASE_URI'] = database_file from app import api_bp app.register_blueprint(api_bp, url_prefix='/api') from Model import db db.init_app(app) login_manager = LoginManager() login_manager.login_view = 'medium_blueprint.login' login_manager.init_app(app) jwt = JWTManager(app) # A storage engine to save revoked tokens. In production if # speed is the primary concern, redis is a good bet. blacklist = set() @jwt.token_in_blacklist_loader def check_if_token_in_blacklist(decrypted_token): jti = decrypted_token['jti'] return jti in blacklist # Create a function that will be called whenever create_access_token # is used. It will take whatever object is passed into the # create_access_token method, and lets us define what custom claims # should be added to the access token. @jwt.user_claims_loader def add_claims_to_access_token(user): try: user['user_name'] except: return {'user_name': user} return {'user_name': user['user_name']} # Create a function that will be called whenever create_access_token # is used. It will take whatever object is passed into the # create_access_token method, and lets us define what the identity # of the access token should be. @jwt.user_identity_loader def user_identity_lookup(user): try: user['user_name'] except: return {'user_name': user} return user['user_name'] # New function, verify the user claims in an access token @jwt.claims_verification_loader def verify_user_claims(user_claims): expected_keys = ['user_name'] for key in expected_keys: if key not in user_claims: return False else: checkJWT = get_jwt_identity() if(checkJWT != None): if(checkJWT['user_name'] != user_claims[key]): return False return True # New function, change the return value if user claims verification failed @jwt.claims_verification_failed_loader def failed_user_claim_verification_error(): return jsonify({'msg': 'Access token is incorrect'}), 404 @app.route('/logout', methods=['POST']) @jwt_required def logout(): jti = get_raw_jwt()['jti'] blacklist.add(jti) resp = jsonify({'logout': True}) unset_jwt_cookies(resp) return resp, 200 from Model import User, Membership @login_manager.user_loader def load_user(user_id): loaded_user = User.query.get(int(user_id)) if not loaded_user: return user_membership = Membership.query.filter_by(related_user_id=loaded_user.id).first() loaded_user.related_role_id = user_membership.related_role_id return loaded_user @app.before_request def before_request(): g.user = current_user # used to instantiate comments.db with app.app_context(): from Model import Comment from Model import Category db.create_all() db.session.commit() from templates.hello.routes import hello_blueprint from templates.medium_articles.routes import medium_blueprint # register the blueprints app.register_blueprint(hello_blueprint) app.register_blueprint(medium_blueprint) @app.before_request def block_method(): ip = request.environ.get('HTTP_X_REAL_IP', request.remote_addr) dct = dict(ip_ban_list) val = dct.get(ip) if val is not None: present = datetime.datetime.utcnow() if val > present: app.logger.warning('Blocked request from blacklist') abort(403) @app.route('/', defaults={'path': ''}) @app.route('/<path:path>') def catch_all(path): return redirect("/", code=302) return app
from flask import Flask from app import api_bp from Model import db myapp = Flask(__name__) myapp.config.from_object("config") myapp.register_blueprint(api_bp, url_prefix='/api') db.init_app(myapp) if __name__ == "__main__": myapp.run(debug=True)