Skip to main content
友田 陽大

Category

Flask 本番運用ガイド(アプリケーションファクトリ/Blueprint/Flask-SQLAlchemy・Migrate/認証・JWT/Celery非同期/REST API・OpenAPI/キャッシュ・レート制限/テスト/デプロイ/セキュリティ/可観測性/技術選定)の実装記事群

Flask は『核(ルーティング・リクエスト/レスポンス・テンプレート・設定・コンテキスト)だけを提供し、ORM・フォーム検証・管理画面は内蔵しない』という設計思想の WSGI マイクロフレームワークです。だからこそ本番品質は、機能ではなく『構造設計』で決まります。本クラスタは、グローバル app を捨てる create_app のアプリケーションファクトリと Blueprint・init_app による大規模構成、import 時に存在しない app を current_app / g で引き回さないコンテキスト設計、test_client と pytest fixtures による境界の契約テスト、開発サーバーを捨て Gunicorn・ProxyFix・Docker で動かす本番デプロイ、署名 Cookie セッション・SECRET_KEY・CSRF(Flask-WTF)・自動エスケープのセキュリティ、errorhandler の JSON エラーと dictConfig 構造化ログ・リクエスト ID の可観測性、そして Flask を採用すべきか(FastAPI / Django との比較)という上流の技術選定までを扱います。さらに案件で必ず問われる実装——Flask-SQLAlchemy(2.0スタイル)+ Flask-Migrate のデータ層、Flask-Login(セッション)/ Flask-JWT-Extended(トークン)の認証、Celery × Redis の冪等な非同期タスク、MethodView による REST API 設計と Flask-smorest による OpenAPI / Swagger の自動生成、Flask-Caching / Flask-Limiter による性能最適化とレート制限——まで、拡張エコシステムを本番品質で組み合わせる設計を網羅します。経済産業大臣賞を受賞した B2B SaaS のバックエンドを Flask / SQLAlchemy / PostgreSQL で設計・実装し本番運用した知見を根拠に、設計の自由を負債にしないための規律を、各公式ドキュメントに忠実な実コードで体系化します。境界バリデーション(marshmallow)は『marshmallow』クラスタ、SQLAlchemy 本体・Alembic は『Pythonバックエンド』クラスタを参照してください。

14 articles in total

Foundational guide

Foundational guide (start here)

Python
Flask
WSGI
アーキテクチャ設計
本番運用

Flask Production Operations Guide (3.1 Series): The Overall Picture of Application Factory, Blueprints, Configuration, Context, and Production Deployment

An overall guide to designing and operating Flask 3.1 series at production quality. We systematize — in real code faithful to the latest official documentation — the philosophy of the WSGI microframework, the create_app application factory, splitting with Blueprints, configuration management with from_prefixed_env and the instance folder, the current_app/g context, extensions' init_app, error handling and logging, SECRET_KEY and safe Cookies, production deployment with Gunicorn + ProxyFix, and testing with pytest.

15 min read

Related practical articles