# インフラ構成図

## 全体構成図

```mermaid
flowchart TB
    subgraph External["外部アクセス"]
        User["ユーザー"]
        Admin["管理者"]
    end

    subgraph Docker["Docker Compose Stack"]
        subgraph Proxy["リバースプロキシ層"]
            Caddy["inventree-proxy<br/>(Caddy/Nginx)<br/>:80, :443"]
        end

        subgraph App["アプリケーション層"]
            Server["inventree-server<br/>(Gunicorn + Django)<br/>:8000"]
            Worker["inventree-worker<br/>(django-q)"]
        end

        subgraph Cache["キャッシュ層"]
            Redis["inventree-cache<br/>(Redis 7)<br/>:6379"]
        end

        subgraph DB["データベース層"]
            PostgreSQL["inventree-db<br/>(PostgreSQL 17)<br/>:5432"]
        end

        subgraph Storage["ストレージ"]
            Volume["INVENTREE_EXT_VOLUME"]
            Static["/var/www/static"]
            Media["/var/www/media"]
            DBData["/var/lib/postgresql/data"]
            RedisData["/data"]
        end
    end

    User -->|HTTP/HTTPS| Caddy
    Admin -->|HTTP/HTTPS| Caddy

    Caddy -->|proxy_pass :8000| Server
    Caddy -->|static files| Static
    Caddy -->|media files + auth| Media

    Server -->|cache| Redis
    Server -->|database| PostgreSQL
    Worker -->|cache| Redis
    Worker -->|database| PostgreSQL

    Volume --> Static
    Volume --> Media
    Volume --> DBData
    Volume --> RedisData
```

## サービス依存関係図

```mermaid
flowchart LR
    subgraph Services["Docker Services"]
        DB["inventree-db<br/>(PostgreSQL)"]
        Cache["inventree-cache<br/>(Redis)"]
        Server["inventree-server<br/>(Gunicorn)"]
        Worker["inventree-worker<br/>(django-q)"]
        Proxy["inventree-proxy<br/>(Caddy)"]
    end

    DB --> Server
    Cache --> Server
    Server --> Worker
    Server --> Proxy
```

## ネットワーク構成図

```mermaid
flowchart TB
    subgraph Internet["Internet"]
        Client["クライアント"]
    end

    subgraph DockerNetwork["Docker Bridge Network"]
        subgraph PublicPorts["公開ポート"]
            HTTP[":80 (HTTP)"]
            HTTPS[":443 (HTTPS)"]
        end

        subgraph InternalPorts["内部ポート"]
            App8000[":8000 (Gunicorn)"]
            DB5432[":5432 (PostgreSQL)"]
            Redis6379[":6379 (Redis)"]
        end
    end

    Client -->|HTTP| HTTP
    Client -->|HTTPS| HTTPS
    HTTP --> App8000
    HTTPS --> App8000
```

## コンテナ構成詳細図

```mermaid
flowchart TB
    subgraph ProxyContainer["inventree-proxy"]
        CaddyProcess["Caddy Server"]
        CaddyConfig["Caddyfile"]
        CaddyLog["/var/log/caddy/"]
    end

    subgraph ServerContainer["inventree-server"]
        GunicornProcess["Gunicorn (4+ workers)"]
        DjangoApp["Django Application"]
        StaticDir["/home/inventree/data/static"]
        MediaDir["/home/inventree/data/media"]
    end

    subgraph WorkerContainer["inventree-worker"]
        DjangoQ["django-q Worker"]
        InvokeWorker["invoke worker"]
    end

    subgraph CacheContainer["inventree-cache"]
        RedisProcess["Redis Server"]
        RedisStore["/data"]
    end

    subgraph DBContainer["inventree-db"]
        PostgresProcess["PostgreSQL Server"]
        PGData["/var/lib/postgresql/data/pgdb"]
    end

    CaddyProcess --> GunicornProcess
    GunicornProcess --> DjangoApp
    DjangoApp --> RedisProcess
    DjangoApp --> PostgresProcess
    DjangoQ --> RedisProcess
    DjangoQ --> PostgresProcess
```

## データフロー図

```mermaid
sequenceDiagram
    participant User as ユーザー
    participant Caddy as Caddy Proxy
    participant Gunicorn as Gunicorn/Django
    participant Redis as Redis Cache
    participant PostgreSQL as PostgreSQL DB
    participant Worker as Background Worker

    User->>Caddy: HTTPリクエスト

    alt 静的ファイル
        Caddy-->>User: 静的ファイル応答
    else メディアファイル
        Caddy->>Gunicorn: 認証確認 (/auth/)
        Gunicorn-->>Caddy: 認証結果
        Caddy-->>User: メディアファイル応答
    else APIリクエスト
        Caddy->>Gunicorn: プロキシ転送
        Gunicorn->>Redis: キャッシュ確認
        Redis-->>Gunicorn: キャッシュ応答
        Gunicorn->>PostgreSQL: DBクエリ
        PostgreSQL-->>Gunicorn: クエリ結果
        Gunicorn-->>Caddy: API応答
        Caddy-->>User: HTTP応答
    end

    Note over Gunicorn,Worker: バックグラウンドタスク
    Gunicorn->>Redis: タスクキュー追加
    Worker->>Redis: タスク取得
    Worker->>PostgreSQL: タスク処理
```

## ストレージマッピング図

```mermaid
flowchart LR
    subgraph HostVolume["ホストボリューム (INVENTREE_EXT_VOLUME)"]
        HostStatic["./inventree-data/static"]
        HostMedia["./inventree-data/media"]
        HostBackup["./inventree-data/backup"]
        HostConfig["./inventree-data/config.yaml"]
        HostSecret["./inventree-data/secret_key.txt"]
        HostPlugins["./inventree-data/plugins"]
        HostPGData["./inventree-data/pgdb"]
        HostRedis["./inventree-data/redis"]
    end

    subgraph Containers["コンテナマウント"]
        subgraph ServerMount["inventree-server"]
            ServerData["/home/inventree/data"]
        end
        subgraph ProxyMount["inventree-proxy"]
            ProxyStatic["/var/www/static"]
            ProxyMedia["/var/www/media"]
        end
        subgraph DBMount["inventree-db"]
            DBData["/var/lib/postgresql/data"]
        end
        subgraph CacheMount["inventree-cache"]
            CacheData["/data"]
        end
    end

    HostStatic --> ProxyStatic
    HostMedia --> ProxyMedia
    HostPGData --> DBData
    HostRedis --> CacheData
    HostStatic --> ServerData
    HostMedia --> ServerData
    HostBackup --> ServerData
    HostConfig --> ServerData
    HostSecret --> ServerData
    HostPlugins --> ServerData
```

## 環境別構成比較

```mermaid
flowchart TB
    subgraph Production["本番環境"]
        ProdProxy["Caddy<br/>(:80, :443)"]
        ProdServer["Gunicorn<br/>(DEBUG=False)"]
        ProdWorker["django-q Worker"]
        ProdDB["PostgreSQL 17"]
        ProdCache["Redis 7"]
        ProdProxy --> ProdServer
        ProdServer --> ProdDB
        ProdServer --> ProdCache
        ProdWorker --> ProdDB
        ProdWorker --> ProdCache
    end

    subgraph Development["開発環境"]
        DevServer["Django Dev Server<br/>(DEBUG=True)<br/>:8000"]
        DevVite["Vite Dev Server<br/>:5173"]
        DevDB["PostgreSQL 15"]
        DevCache["Redis 7"]
        DevServer --> DevDB
        DevServer --> DevCache
    end
```

## セキュリティ境界図

```mermaid
flowchart TB
    subgraph External["外部ネットワーク"]
        Internet["Internet"]
    end

    subgraph DMZ["DMZ (公開層)"]
        Caddy["Caddy Proxy<br/>(SSL終端)"]
    end

    subgraph Internal["内部ネットワーク"]
        subgraph AppTier["アプリケーション層"]
            Server["Gunicorn/Django"]
            Worker["Background Worker"]
        end
        subgraph DataTier["データ層"]
            Redis["Redis"]
            PostgreSQL["PostgreSQL"]
        end
    end

    Internet -->|HTTPS :443| Caddy
    Caddy -->|HTTP :8000| Server
    Server --> Redis
    Server --> PostgreSQL
    Worker --> Redis
    Worker --> PostgreSQL

    style External fill:#ffcccc
    style DMZ fill:#ffffcc
    style Internal fill:#ccffcc
```
