def init_app(): app = Flask(__name__) CORS(app) app.config.update({ 'SECRET_KEY': 'foiclientapp', 'TESTING': True, 'DEBUG': True, 'OIDC_CLIENT_SECRETS': 'client_secrets.json', 'OIDC_ID_TOKEN_COOKIE_SECURE': False, 'OIDC_REQUIRE_VERIFIED_EMAIL': False, 'OIDC_VALID_ISSUERS': ['https://iam.aot-technologies.com/auth/realms/foirealm'], 'OIDC_OPENID_REALM': 'http://*****:*****@localhost:5432/reviewdb', 'UPLOAD_FOLDER': 'c://AOT/Files/' }) db.init_app(app) return app
def create_app(): app = Flask(__name__) basedir = os.path.abspath(os.path.dirname(os.path.dirname(__file__))) app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join( basedir, 'db.sqlite') app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False db.init_app(app) app.register_blueprint(devices_blueprint, url_prefix='/devices') return app
def init_app(run_mode=os.getenv('FLASK_ENV', 'production')): app.config.from_object(CONFIG[run_mode]) CORS(app) from .requestapi import REQUESTAPI_BLUEPRINT app.register_blueprint(REQUESTAPI_BLUEPRINT) ExceptionHandler(app) setup_jwt_manager(app, jwt) db.init_app(app) @app.route('/', methods=['GET']) def index(): # pylint: disable=unused-variable return 'FOI Code Challenge - AOT TECH - BackEnd API working' # pragma: no cover return app
def setup(flask_app, with_cors=True, with_debug=False, with_jobs=False, with_login_manager=False, with_routes=False, with_scripts_manager=False, with_models_creation=False): flask_app.secret_key = os.environ.get('FLASK_SECRET', '+%+5Q83!abR+-Dp@') flask_app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get('POSTGRES_URL') flask_app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False if with_debug: flask_app.config['DEBUG'] = True db.app = flask_app db.init_app(flask_app) ApiHandler.set_db(db) flask_app.json_encoder = EnumJSONEncoder @flask_app.teardown_request def remove_db_session(exc): try: db.session.remove() except AttributeError: pass if with_cors: from flask_cors import CORS cors = CORS(flask_app, resources={r"/*": {"origins": "*"}}, supports_credentials=True) flask_app.url_map.strict_slashes = False flask_app.app_context().push() import_models(with_creation=with_models_creation) if with_login_manager: from flask_login import LoginManager flask_app.config['SESSION_COOKIE_HTTPONLY'] = not flask_app.config['TESTING'] flask_app.config['SESSION_COOKIE_SECURE'] = False if IS_DEVELOPMENT else True flask_app.config['REMEMBER_COOKIE_HTTPONLY'] = not flask_app.config['TESTING'] if not flask_app.config['TESTING']: flask_app.config['PERMANENT_SESSION_LIFETIME'] = 90 * 24 * 3600 flask_app.config['REMEMBER_COOKIE_DURATION'] = 90 * 24 * 3600 flask_app.config['REMEMBER_COOKIE_SECURE'] = True login_manager = LoginManager() login_manager.init_app(flask_app) import repository.login_manager import utils.nltk_downloader if with_routes: import_routes() if with_jobs: from apscheduler.schedulers.background import BackgroundScheduler from apscheduler.schedulers.asyncio import AsyncIOScheduler async_jobs = import_async_jobs() background_jobs = import_background_jobs() async_scheduler = AsyncIOScheduler() background_scheduler = BackgroundScheduler() for job in async_jobs: async_scheduler.add_job(**job, replace_existing=True) for job in background_jobs: background_scheduler.add_job(**job, replace_existing=True) flask_app.async_scheduler = async_scheduler flask_app.background_scheduler = background_scheduler if with_scripts_manager: from flask_script import Manager def create_app(env=None): flask_app.env = env return flask_app flask_app.manager = Manager(create_app) import_scripts()
from resources.order import Order from resources.orders import Orders from resources.route import Route from resources.routes import Routes from resources.item import Item from resources.items import Items from models.user import UserModel app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///db.sqlite' app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False app.config['SECRET_KEY'] = 'wasay' api = Api(app) db.init_app(app) login_manager.init_app(app) @app.before_first_request def create_tables(): db.create_all() api.add_resource(User, "/users/<int:id>") # 取得基本資料 api.add_resource(Users, "/users") # 註冊 api.add_resource(Farmer, "/farmers/<int:id>") # 取得基本資料 api.add_resource(Farmers, "/farmers") # 註冊 api.add_resource(Session, "/session") # 登入、登出 api.add_resource(Orders, "/orders") # 訂單
from utils.email import mail import os import logging app = Flask(__name__) if os.environ.get('WORK_ENV') == 'PROD': app_config = ProductionConfig elif os.environ.get('WORK_ENV') == 'TEST': app_config = TestingConfig else: app_config = DevelopmentConfig app.config.from_object(app_config) db.init_app(app) # 延迟绑定,创建 with app.app_context(): db.create_all() mail.init_app(app) # 初始化Mail app.register_blueprint(author_routes, url_prefix='/api/authors/') app.register_blueprint(book_routes, url_prefix='/api/books/') app.register_blueprint(user_routes, url_prefix='/api/users/') @app.after_request def add_header(response): return response