# インフラ構成図

## 開発環境構成（Dev Container）

```mermaid
graph TB
    subgraph "Developer Machine"
        IDE[VS Code / IDE]
    end

    subgraph "Docker Compose Environment"
        subgraph "rails container"
            direction TB
            RAILS[Rails Application]
            PUMA[Puma Web Server]
            RUBY[Ruby 4.0.1]
            RAILS --> PUMA
            PUMA --> RUBY
        end

        subgraph "Database Services"
            POSTGRES[(PostgreSQL<br/>latest)]
            MYSQL[(MySQL<br/>latest)]
        end

        subgraph "Cache Services"
            REDIS[(Valkey/Redis 9)]
            MEMCACHED[(Memcached<br/>latest)]
        end

        subgraph "Volumes"
            PG_VOL[postgres-data]
            MYSQL_VOL[mysql-data]
            REDIS_VOL[redis-data]
        end
    end

    IDE -->|devcontainer.json| RAILS
    RAILS -->|PGHOST=postgres| POSTGRES
    RAILS -->|MYSQL_HOST=mysql| MYSQL
    RAILS -->|REDIS_URL| REDIS
    RAILS -->|MEMCACHE_SERVERS| MEMCACHED

    POSTGRES --> PG_VOL
    MYSQL --> MYSQL_VOL
    REDIS --> REDIS_VOL
```

## 本番環境構成（Kamal Deploy）

```mermaid
graph TB
    subgraph "Internet"
        CLIENT[Client Browser]
        LETSENCRYPT[Let's Encrypt]
    end

    subgraph "Web Tier"
        subgraph "Web Server 1"
            THRUSTER1[Thruster<br/>HTTP/2 Proxy<br/>Port 80/443]
            PUMA1[Puma >= 7.1]
            RAILS1[Rails Application]
            SOLIDQ1[Solid Queue<br/>In-Process]

            THRUSTER1 --> PUMA1
            PUMA1 --> RAILS1
            RAILS1 --> SOLIDQ1
        end

        subgraph "Web Server N"
            THRUSTER_N[Thruster]
            PUMA_N[Puma]
            RAILS_N[Rails]

            THRUSTER_N --> PUMA_N
            PUMA_N --> RAILS_N
        end
    end

    subgraph "Data Tier (Accessories)"
        DB[(Database<br/>PostgreSQL/MySQL)]
        REDIS_PROD[(Valkey/Redis 9<br/>Cache + Cable)]
    end

    subgraph "Storage"
        STORAGE[Persistent Volume<br/>/rails/storage]
        S3[AWS S3 / GCS<br/>Active Storage]
    end

    subgraph "Registry"
        REGISTRY[Container Registry<br/>ghcr.io / Docker Hub]
    end

    CLIENT -->|HTTPS| THRUSTER1
    CLIENT -->|HTTPS| THRUSTER_N
    LETSENCRYPT -->|SSL Cert| THRUSTER1
    LETSENCRYPT -->|SSL Cert| THRUSTER_N

    RAILS1 --> DB
    RAILS1 --> REDIS_PROD
    RAILS1 --> STORAGE
    RAILS1 --> S3

    RAILS_N --> DB
    RAILS_N --> REDIS_PROD

    REGISTRY -->|Docker Pull| THRUSTER1
    REGISTRY -->|Docker Pull| THRUSTER_N
```

## Docker イメージビルドフロー

```mermaid
graph LR
    subgraph "Build Stage: base"
        BASE[ruby:slim]
        BASE_PKG[curl, libjemalloc2,<br/>libvips, DB client]
        BASE --> BASE_PKG
    end

    subgraph "Build Stage: build"
        BUILD_PKG[build-essential,<br/>git, pkg-config,<br/>libyaml-dev]
        GEMS[Bundle Install]
        ASSETS[Asset Precompile]
        BOOTSNAP[Bootsnap Precompile]

        BUILD_PKG --> GEMS
        GEMS --> ASSETS
        ASSETS --> BOOTSNAP
    end

    subgraph "Final Stage"
        FINAL_USER[rails user<br/>UID 1000]
        COPY_GEMS[Copy Gems]
        COPY_APP[Copy Application]
        ENTRYPOINT[docker-entrypoint]
        CMD[Thruster + Puma]

        FINAL_USER --> COPY_GEMS
        COPY_GEMS --> COPY_APP
        COPY_APP --> ENTRYPOINT
        ENTRYPOINT --> CMD
    end

    BASE_PKG --> BUILD_PKG
    BOOTSNAP --> COPY_GEMS
```

## CI/CD パイプライン構成

```mermaid
graph TD
    subgraph "GitHub"
        PUSH[Push / PR]
        ACTIONS[GitHub Actions]
    end

    subgraph "Smoke Tests"
        SQLITE[SQLite3 Test]
        PG[PostgreSQL Test]
        MYSQL_T[MySQL Test]
        TRILOGY[Trilogy Test]
    end

    subgraph "Docker Tests"
        PODMAN[Podman Build]
        CONTAINER[Container Run]
        HEALTH[Health Check]
    end

    subgraph "Artifact"
        GHCR[ghcr.io<br/>Container Registry]
    end

    PUSH --> ACTIONS
    ACTIONS --> SQLITE
    ACTIONS --> PG
    ACTIONS --> MYSQL_T
    ACTIONS --> TRILOGY
    ACTIONS --> PODMAN

    PODMAN --> CONTAINER
    CONTAINER --> HEALTH

    SQLITE --> GHCR
    PG --> GHCR
    MYSQL_T --> GHCR
    TRILOGY --> GHCR
```

## ネットワーク構成

```mermaid
graph TB
    subgraph "External Network"
        INTERNET((Internet))
        CDN[CDN / Cloudflare<br/>SSL: Full Mode]
    end

    subgraph "DMZ"
        LB[Load Balancer<br/>Optional]
    end

    subgraph "Application Network"
        WEB1[Web Server 1<br/>Port 80]
        WEB2[Web Server 2<br/>Port 80]
        WEB3[Web Server N<br/>Port 80]
    end

    subgraph "Internal Network"
        DB_NET[Database<br/>Port 5432/3306]
        REDIS_NET[Redis<br/>Port 6379]
    end

    INTERNET --> CDN
    CDN --> LB
    LB --> WEB1
    LB --> WEB2
    LB --> WEB3

    WEB1 --> DB_NET
    WEB1 --> REDIS_NET
    WEB2 --> DB_NET
    WEB2 --> REDIS_NET
    WEB3 --> DB_NET
    WEB3 --> REDIS_NET
```

## ストレージ構成

```mermaid
graph LR
    subgraph "Rails Application"
        AS[Active Storage]
        SC[Solid Cache]
        SQ[Solid Queue]
    end

    subgraph "Local Storage"
        DISK[Disk Service<br/>/rails/storage]
        SQLITE_DB[(SQLite3<br/>production.sqlite3)]
    end

    subgraph "Cloud Storage"
        S3_BUCKET[AWS S3 Bucket]
        GCS_BUCKET[Google Cloud Storage]
    end

    subgraph "Database Storage"
        PG_DATA[(PostgreSQL<br/>Data Volume)]
        MYSQL_DATA[(MySQL<br/>Data Volume)]
    end

    subgraph "Cache Storage"
        REDIS_DATA[(Redis/Valkey<br/>Data Volume)]
    end

    AS --> DISK
    AS --> S3_BUCKET
    AS --> GCS_BUCKET

    SC --> REDIS_DATA
    SC --> PG_DATA

    SQ --> PG_DATA
    SQ --> MYSQL_DATA
```

## 環境変数構成

```mermaid
graph TD
    subgraph "Secret Management"
        MASTER_KEY[RAILS_MASTER_KEY]
        KAMAL_SECRETS[.kamal/secrets]
    end

    subgraph "Database"
        DB_HOST[DB_HOST]
        DATABASE_URL[DATABASE_URL]
        PGHOST[PGHOST]
        PGUSER[PGUSER]
        MYSQL_HOST[MYSQL_HOST]
    end

    subgraph "Cache & Queue"
        REDIS_URL[REDIS_URL]
        MEMCACHE_SERVERS[MEMCACHE_SERVERS]
        SOLID_QUEUE_IN_PUMA[SOLID_QUEUE_IN_PUMA]
    end

    subgraph "Performance"
        WEB_CONCURRENCY[WEB_CONCURRENCY]
        JOB_CONCURRENCY[JOB_CONCURRENCY]
        RAILS_LOG_LEVEL[RAILS_LOG_LEVEL]
    end

    subgraph "Rails Application"
        RAILS_ENV[RAILS_ENV]
        SECRET_KEY_BASE[SECRET_KEY_BASE]
    end

    MASTER_KEY --> RAILS_ENV
    KAMAL_SECRETS --> MASTER_KEY

    DB_HOST --> DATABASE_URL
    PGHOST --> DATABASE_URL

    REDIS_URL --> SOLID_QUEUE_IN_PUMA
    WEB_CONCURRENCY --> RAILS_ENV
```
