HTTPミドルウェア
HTTPミドルウェア
Section titled “HTTPミドルウェア”ミドルウェアはルート処理の前後にHTTPリクエストを処理します。
ミドルウェアの仕組み
Section titled “ミドルウェアの仕組み”ミドルウェアはHTTPハンドラをラップして処理ロジックを追加します。各ミドルウェアはオプションマップを受け取り、ハンドララッパーを返します:
middleware: - cors - ratelimitoptions: cors.allow.origins: "https://example.com" ratelimit.requests: "100"オプションはドット記法を使用:middleware_name.option.name。後方互換性のためにレガシーアンダースコア形式もサポートされています。
マッチ前 vs マッチ後
Section titled “マッチ前 vs マッチ後”middleware: # マッチ前 - cors - compressoptions: cors.allow.origins: "*"
post_middleware: # マッチ後 - endpoint_firewallpost_options: endpoint_firewall.action: "access"利用可能なミドルウェア
Section titled “利用可能なミドルウェア”CORS {#cors}
Section titled “CORS {#cors}”ブラウザリクエスト用のCross-Origin Resource Sharing。
middleware: - corsoptions: cors.allow.origins: "https://app.example.com" cors.allow.credentials: "true"| オプション | デフォルト | 説明 |
|---|---|---|
cors.allow.origins | * | 許可するオリジン(カンマ区切り、*.example.comをサポート) |
cors.allow.methods | GET,POST,PUT,DELETE,OPTIONS,PATCH | 許可するメソッド |
cors.allow.headers | Origin,Content-Type,Accept,Authorization,X-Requested-With | 許可するリクエストヘッダー |
cors.expose.headers | - | クライアントに公開するヘッダー |
cors.allow.credentials | false | Cookie/認証を許可 |
cors.max.age | 86400 | プリフライトキャッシュ(秒) |
cors.allow.private.network | false | プライベートネットワークアクセス |
OPTIONSプリフライトリクエストは自動的に処理されます。
レート制限 {#ratelimit}
Section titled “レート制限 {#ratelimit}”キーごとの追跡を持つトークンバケットレート制限。
middleware: - ratelimitoptions: ratelimit.requests: "100" ratelimit.window: "1m" ratelimit.key: "ip"| オプション | デフォルト | 説明 |
|---|---|---|
ratelimit.requests | 100 | ウィンドウごとのリクエスト数 |
ratelimit.window | 1m | 時間ウィンドウ |
ratelimit.burst | 20 | バースト容量 |
ratelimit.key | ip | キー戦略 |
ratelimit.cleanup_interval | 5m | クリーンアップ頻度 |
ratelimit.entry_ttl | 10m | エントリ有効期限 |
ratelimit.max_entries | 100000 | 追跡する最大キー数 |
キー戦略: ip、header:X-API-Key、query:api_key
429 Too Many Requestsをヘッダー付きで返します:X-RateLimit-Limit、X-RateLimit-Remaining、X-RateLimit-Reset。
圧縮 {#compress}
Section titled “圧縮 {#compress}”レスポンスのGzip圧縮。
middleware: - compressoptions: compress.level: "default" compress.min.length: "1024"| オプション | デフォルト | 説明 |
|---|---|---|
compress.level | default | fastest、default、またはbest |
compress.min.length | 1024 | 最小レスポンスサイズ(バイト) |
クライアントがAccept-Encoding: gzipを送信した場合のみ圧縮します。
Real IP {#real_ip}
Section titled “Real IP {#real_ip}”プロキシヘッダーからクライアントIPを抽出。
middleware: - real_ipoptions: real_ip.trusted.subnets: "10.0.0.0/8,172.16.0.0/12"| オプション | デフォルト | 説明 |
|---|---|---|
real_ip.trusted.subnets | プライベートネットワーク | 信頼するプロキシCIDR |
real_ip.trust_all | false | すべてのソースを信頼(安全でない) |
ヘッダー優先順位: True-Client-IP > X-Real-IP > X-Forwarded-For
トークン認証 {#token_auth}
Section titled “トークン認証 {#token_auth}”トークンベースの認証。トークンストア設定についてはセキュリティを参照。
middleware: - token_authoptions: token_auth.store: "app:tokens"| オプション | デフォルト | 説明 |
|---|---|---|
token_auth.store | 必須 | トークンストアレジストリID |
token_auth.header.name | Authorization | ヘッダー名 |
token_auth.header.prefix | Bearer | ヘッダープレフィックス |
token_auth.query.param | x-auth-token | クエリパラメータフォールバック |
token_auth.cookie.name | x-auth-token | Cookieフォールバック |
ダウンストリームミドルウェア用にコンテキストにアクターとセキュリティスコープを設定します。リクエストをブロックしません—認可はファイアウォールミドルウェアで行われます。
メトリクス {#metrics}
Section titled “メトリクス {#metrics}”PrometheusスタイルのHTTPメトリクス。設定オプションはありません。
middleware: - metrics| メトリクス | タイプ | 説明 |
|---|---|---|
wippy_http_requests_total | Counter | 総リクエスト数 |
wippy_http_request_duration_seconds | Histogram | リクエストレイテンシー |
wippy_http_requests_in_flight | Gauge | 同時リクエスト数 |
エンドポイントファイアウォール {#endpoint_firewall}
Section titled “エンドポイントファイアウォール {#endpoint_firewall}”マッチしたエンドポイントに基づく認可。token_authからのアクターが必要。
post_middleware: - endpoint_firewallpost_options: endpoint_firewall.action: "access"| オプション | デフォルト | 説明 |
|---|---|---|
endpoint_firewall.action | access | チェックする権限アクション |
401 Unauthorized(アクターなし)または403 Forbidden(権限拒否)を返します。
リソースファイアウォール {#resource_firewall}
Section titled “リソースファイアウォール {#resource_firewall}”IDで特定のリソースを保護。ルーターレベルで便利。
post_middleware: - resource_firewallpost_options: resource_firewall.action: "admin" resource_firewall.target: "app:admin-panel"| オプション | デフォルト | 説明 |
|---|---|---|
resource_firewall.action | access | 権限アクション |
resource_firewall.target | 必須 | リソースレジストリID |
Sendfile {#sendfile}
Section titled “Sendfile {#sendfile}”ハンドラからのX-Sendfileヘッダー経由でファイルを配信。
middleware: - sendfileoptions: sendfile.fs: "app:downloads"ハンドラはファイル配信をトリガーするためにヘッダーを設定:
| ヘッダー | 説明 |
|---|---|
X-Sendfile | ファイルシステム内のファイルパス |
X-File-Name | ダウンロードファイル名 |
再開可能なダウンロードのためのRangeリクエストをサポート。
WebSocketリレー {#websocket_relay}
Section titled “WebSocketリレー {#websocket_relay}”WebSocket接続をプロセスにリレー。WebSocketリレーを参照。
post_middleware: - websocket_relaypost_options: wsrelay.allowed.origins: "https://app.example.com"SSEリレー {#sse_relay}
Section titled “SSEリレー {#sse_relay}”プロセスからServer-Sent Eventsをストリーミングします。Server-Sent Eventsを参照してください。
post_middleware: - sse_relaypost_options: sserelay.allowed.origins: "https://app.example.com"OpenTelemetry {#otel}
Section titled “OpenTelemetry {#otel}”受信リクエストのOpenTelemetryスパンとメトリクスを記録します。OTelが有効な場合に自動登録され、それ以外の場合はno-opとして動作します。
middleware: - otelオプションは受け取りません。metricsミドルウェアと連携して動作します。PrometheusカウンターとOTelトレースの両方が必要な場合は、両方を有効にしてください。
ミドルウェアの順序
Section titled “ミドルウェアの順序”ミドルウェアはリストされた順序で実行されます。推奨される順序:
middleware: - real_ip # 1. 最初にReal IPを抽出 - cors # 2. CORSプリフライトを処理 - compress # 3. レスポンス圧縮をセットアップ - ratelimit # 4. レート制限をチェック - metrics # 5. メトリクスを記録 - token_auth # 6. リクエストを認証
post_middleware: - endpoint_firewall # ルートマッチ後に認可- ルーティング - ルーター設定
- セキュリティ - トークンストアとポリシー
- WebSocketリレー - WebSocket処理
- Server-Sent Events - SSEストリーミング
- ターミナル - ターミナルサービス