HTTPサーバー

HTTPサーバー(http.service)はポートをリッスンし、ルーター、エンドポイント、静的ファイルハンドラをホストします。

設定

- name: gateway
  kind: http.service
  addr: ":8080"
  timeouts:
    read: "5s"
    write: "30s"
    idle: "60s"
  host:
    buffer_size: 1024
    worker_count: 4
  lifecycle:
    auto_start: true
    security:
      actor:
        id: "http-gateway"
      policies:
        - app:http_policy
フィールド デフォルト 説明
addr string 必須 リッスンアドレス(:80800.0.0.0:443
timeouts.read duration - リクエスト読み取りタイムアウト
timeouts.write duration - レスポンス書き込みタイムアウト
timeouts.idle duration - Keep-alive接続タイムアウト
host.buffer_size int 1024 メッセージリレーバッファサイズ
host.worker_count int NumCPU メッセージリレーワーカー

タイムアウト

リソース枯渇を防ぐためにタイムアウトを設定:

timeouts:
  read: "10s"    # リクエストヘッダー読み取りの最大時間
  write: "60s"   # レスポンス書き込みの最大時間
  idle: "120s"   # Keep-aliveタイムアウト
  • read - APIには短め(5-10秒)、アップロードには長め
  • write - 予想されるレスポンス生成時間に合わせる
  • idle - 接続の再利用とリソース使用のバランス
期間形式: 30s1m2h15m。無効化するには0を使用。

ホスト設定

hostセクションはWebSocketリレーなどのコンポーネントが使用するサーバーの内部メッセージリレーを設定します:

host:
  buffer_size: 2048
  worker_count: 8
フィールド デフォルト 説明
buffer_size 1024 ワーカーごとのメッセージキュー容量
worker_count NumCPU 並列メッセージ処理goroutine
高スループットWebSocketアプリケーションではこれらの値を増やしてください。メッセージリレーはHTTPコンポーネントとプロセス間の非同期配信を処理します。

セキュリティ

HTTPサーバーはライフサイクル設定を通じてデフォルトのセキュリティコンテキストを適用できます:

lifecycle:
  auto_start: true
  security:
    actor:
      id: "gateway-service"
    policies:
      - app:http_access_policy

これはすべてのリクエストに対するベースラインのアクターとポリシーを設定します。認証されたリクエストの場合、token_authミドルウェアは検証されたトークンに基づいてアクターをオーバーライドし、ユーザーごとのセキュリティポリシーを可能にします。

ライフサイクル

サーバーはスーパーバイザによって管理されます:

lifecycle:
  auto_start: true
  start_timeout: 30s
  stop_timeout: 60s
  depends_on:
    - app:database
フィールド 説明
auto_start アプリケーション起動時に開始
start_timeout サーバー起動の最大待機時間
stop_timeout グレースフルシャットダウンの最大時間
depends_on これらのエントリが準備完了後に開始

コンポーネントの接続

ルーターと静的ハンドラはメタデータを通じてサーバーを参照します:

entries:
  - name: gateway
    kind: http.service
    addr: ":8080"

  - name: api
    kind: http.router
    meta:
      server: gateway
    prefix: /api

  - name: static
    kind: http.static
    meta:
      server: gateway
    path: /
    fs: app:public

複数サーバー

異なる目的のために別々のサーバーを実行:

entries:
  # パブリックAPI
  - name: public
    kind: http.service
    addr: ":8080"
    lifecycle:
      auto_start: true

  # 管理者(localhostのみ)
  - name: admin
    kind: http.service
    addr: "127.0.0.1:9090"
    lifecycle:
      auto_start: true
TLS終端は通常リバースプロキシ(Nginx、Caddy、ロードバランサー)で処理されます。WippyのHTTPサーバーに転送するようにプロキシを設定してください。

関連項目