def create_flask_app(): app = Flask(__name__) csrf = CsrfProtect(app) # sslify = SSLify(app) CORS(app) route(app) csrf_exempt(csrf) path = os.environ.get('CONFIG_PATH') if os.environ.get( 'CONFIG_PATH') != None else "./settings.ini" init_config(path) try: # Flask application configuration app.config.update(dict( SECRET_KEY=str(config['FLASK_APP']['FLASK_APP_SECRET_KEY']), WTF_CSRF_SECRET_KEY=str( config['FLASK_APP']['FLASK_APP_WTF_CSRF_SECRET_KEY']), )) print(f"\n\033[32m Сервер запустился с конфигом:\n\033[32m {path}\n") except KeyError: print(f"\033[31m Файл {path} не найден или неверный") shutdown_server() return app
def create_app() -> Flask: """create Flask app object and configure app global items (thread local is not yet available).""" app = Flask(__name__.split('.')[0]) init_config(app) app.register_blueprint(observer) app.teardown_appcontext(close_db) app.cli.add_command(init_db) return app
def create_app(config_name: str = "default") -> Flask: app = Flask(__name__) init_config(app, config_name) with app.app_context(): init_extensions(app) init_blueprints(app) init_commands(app) return app
def main(): curr_dir = os.getcwd() file_dir = os.path.dirname(os.path.realpath(__file__)) if curr_dir != file_dir: print('This script must be run from the app dir.', file=sys.stderr) sys.exit(1) # add app to path sys.path.append(curr_dir) args = sys.argv[1:] if len(args) > 1 and args[0] in ['-e', '--env']: env = args[1] args = args[2:] else: env = 'develop' # コンフィグ初期化 try: init_config(env=env) except: print('存在しない環境が指定されました: %s' % env) exit(1) if len(args) == 0 or args[0] in ['-h', '--help']: usage_exit(0) task_name = args[0] # 実行コマンドをタスク名を含めて書き換え sys.argv[0] = '%s %s' % (sys.argv[0], task_name) if task_name == 'test': # テストランナー task_module = 'tests.runner' task = __import__(task_module, fromlist=['']) task.run(*args[1:]) else: # 汎用タスクランナー task_module = 'tasks.' + args[0] try: task = __import__(task_module, fromlist=['']) task.run(env, *args[1:]) except ImportError: print('No task specified.', file=sys.stderr) usage_exit(1)
def run(): init_config(env='test') # テスト用データベースの削除 drop_database() loader = TestLoader() base_dir = os.path.dirname(__file__) package = loader.discover(base_dir) runner = TextTestRunner(verbosity=2) result = runner.run(package) if len(result.errors) > 0 or len(result.failures) > 0: sys.exit(3)
def main(): args = sys.argv[1:] # env_settings if len(args) > 1 and args[0] in ['-e', '--env']: env = args[1] args = args[2:] else: env = 'develop' # help if len(args) > 0 and args[0] in ['-h', '--help']: usage_exit() # config_initialize try: init_config(env=env) except Exception as e: print(e) exit(1) run(env=env)
parser.add_argument( '-e', '--env', help='実行環境', type=str, choices=[ 'develop', 'staging', 'production', ], default='develop', ) parser.add_argument('-p', '--port', help='ポート', type=int, default=None) args = parser.parse_known_args()[0] # コンフィグの初期化、取得 init_config(env=args.env) config = current_config() # loggerのセットアップ logging_type = 'server' setup_logger(config=config.get('logging').get(logging_type), log_dir=config.get('log_dir'), debug=config.get('debug')) if __name__ == '__main__': # サーバーの実行 from app import server # NOQA server.run(port=args.port)