Skip to content

Rails 本番化対応 (rack-cors / /healthz / Dockerfile.production)#20

Merged
en-Barry merged 9 commits into
developfrom
feature/2605/en-Barry/rails-production-ready
May 12, 2026
Merged

Rails 本番化対応 (rack-cors / /healthz / Dockerfile.production)#20
en-Barry merged 9 commits into
developfrom
feature/2605/en-Barry/rails-production-ready

Conversation

@en-Barry
Copy link
Copy Markdown
Owner

@en-Barry en-Barry commented May 8, 2026

概要

Rails 7 (API only) を本番環境で動かせる状態に整えました。

実装内容

1. rack-cors 追加

  • CORS_ALLOWED_ORIGINS 環境変数でカンマ区切りパース
  • 環境変数が空なら CORS は無効化
  • /api/* リソースに対してのみ許可(GET / OPTIONS / HEAD)

2. /healthz ヘルスチェックエンドポイント

  • DB 接続確認を実装
  • 正常時: 200 OK, {"status":"ok"}
  • DB 失敗時: 503 Service Unavailable, {"status":"error"} + Rails.logger.warn
  • ALB ターゲットグループのヘルスチェック用

3. production.rb 本番対応

  • config.force_ssl = true を有効化
  • config.ssl_options/healthz だけ HTTPS リダイレクトを除外(ALB 内部 HTTP チェック対応)
  • config.host_authorization/healthz だけ検証を skip
  • stdout ログ常時出力(env 変数不要)
  • config.require_master_key = false を明示

4. database.yml 簡素化

  • production セクションを adapter / encoding / pool のみに簡素化
  • DATABASE_URL 環境変数で接続先を制御

5. Dockerfile.production (マルチステージビルド)

  • builder stage: Ruby 3.1.2-slim-bullseye + build dependencies
  • runtime stage: slim-bullseye + libpq5 のみ
  • 非 root ユーザー (rails:1000) で実行
  • RAILS_ENV=production を明示

6. README 更新

  • /healthz エンドポイント説明
  • 本番ビルドコマンド
  • 本番環境変数説明(必須: DATABASE_URL / SECRET_KEY_BASE、オプション: CORS_ALLOWED_ORIGINS)

テスト結果

✅ 全 74 テスト PASS
✅ /healthz エンドポイント: 200 {"status":"ok"}
✅ /api/v1/electricity_bill_simulations: 正常動作

関連 Issue

🤖 Generated with Claude Code

@en-Barry en-Barry force-pushed the feature/2605/en-Barry/db-migration branch from 9da5f9a to 3b7036d Compare May 11, 2026 05:02
@en-Barry en-Barry force-pushed the feature/2605/en-Barry/rails-production-ready branch from 48d691f to dd00202 Compare May 11, 2026 05:03
@en-Barry en-Barry force-pushed the feature/2605/en-Barry/db-migration branch from 3b7036d to a9a7304 Compare May 11, 2026 06:01
@en-Barry en-Barry force-pushed the feature/2605/en-Barry/rails-production-ready branch from dd00202 to 474d6ee Compare May 11, 2026 06:03
Repository owner deleted a comment from vercel Bot May 11, 2026
@en-Barry en-Barry force-pushed the feature/2605/en-Barry/db-migration branch from a9a7304 to ea6df28 Compare May 11, 2026 08:59
@en-Barry en-Barry force-pushed the feature/2605/en-Barry/rails-production-ready branch from 474d6ee to 6a36211 Compare May 11, 2026 09:00
@en-Barry en-Barry force-pushed the feature/2605/en-Barry/db-migration branch from ea6df28 to 45015a3 Compare May 11, 2026 10:09
@en-Barry en-Barry force-pushed the feature/2605/en-Barry/rails-production-ready branch from 6a36211 to 213df1a Compare May 11, 2026 10:19
Repository owner deleted a comment from vercel Bot May 11, 2026
@en-Barry en-Barry force-pushed the feature/2605/en-Barry/db-migration branch from 45015a3 to a0c5cb7 Compare May 12, 2026 07:41
@en-Barry en-Barry force-pushed the feature/2605/en-Barry/rails-production-ready branch from 213df1a to ec82b9b Compare May 12, 2026 07:43
@en-Barry en-Barry force-pushed the feature/2605/en-Barry/db-migration branch from a0c5cb7 to 66be6ac Compare May 12, 2026 10:40
@en-Barry en-Barry force-pushed the feature/2605/en-Barry/rails-production-ready branch from ec82b9b to 2648f9d Compare May 12, 2026 10:40
Base automatically changed from feature/2605/en-Barry/db-migration to develop May 12, 2026 10:41
@en-Barry en-Barry merged commit 0c68e6e into develop May 12, 2026
@en-Barry en-Barry deleted the feature/2605/en-Barry/rails-production-ready branch May 12, 2026 10:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant