ponshu-room-lite/tools/proxy
Ponshu Developer 10f772942a feat: Implement Redis persistence for Synology Proxy rate limiting
Infrastructure Improvements:
- Add Redis container to tools/proxy/docker-compose.yml with AOF persistence
- Migrate rate limiting from in-memory to Redis-based storage
- Add TTL-based daily quota reset (expires at midnight)
- Implement health checks for Redis and Proxy containers

Proxy Server Changes (tools/proxy/server.js):
- Add redis client with async connection handling
- Replace usageStore object with Redis GET/INCR/EXPIRE operations
- Add responseMimeType: 'application/json' to Gemini client config
  (fixes Markdown response bug)
- Add comprehensive debug logging for JSON parsing issues

Flutter App Configuration:
- Change Secrets.useProxy defaultValue from false to true
- Development builds now use local Synology proxy by default
- Release builds can override with --dart-define=USE_PROXY=false

Documentation:
- Add REDIS_MIGRATION_GUIDE.md with step-by-step migration instructions
- Add tools/proxy/README.md with architecture overview
- Create .env.example template for secrets configuration
- Update PROJECT_TODO.md to mark H3 (Proxy永続化) as in progress

Dependencies:
- Add redis@^4.7.0 to package.json

This resolves the critical tech debt where rate limits reset on container restart.
Redis AOF persistence ensures quota tracking survives server reboots.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2026-02-21 19:35:59 +09:00
..
.env.example feat: Implement Redis persistence for Synology Proxy rate limiting 2026-02-21 19:35:59 +09:00
.gitignore feat: Implement Redis persistence for Synology Proxy rate limiting 2026-02-21 19:35:59 +09:00
Dockerfile feat: v1.0.16 brushup + Synology Proxy integration 2026-02-16 00:13:12 +09:00
README.md feat: Implement Redis persistence for Synology Proxy rate limiting 2026-02-21 19:35:59 +09:00
REDIS_MIGRATION_GUIDE.md feat: Implement Redis persistence for Synology Proxy rate limiting 2026-02-21 19:35:59 +09:00
docker-compose.yml feat: Implement Redis persistence for Synology Proxy rate limiting 2026-02-21 19:35:59 +09:00
package.json feat: Implement Redis persistence for Synology Proxy rate limiting 2026-02-21 19:35:59 +09:00
server.js feat: Implement Redis persistence for Synology Proxy rate limiting 2026-02-21 19:35:59 +09:00

README.md

Ponshu Room Proxy Server (Redis永続化対応)

Gemini APIキーを隠蔽し、デバイスごとの利用制限レート制限を管理するプロキシサーバー。

🎯 機能

  • APIキー保護: クライアントアプリにAPIキーを含めず、プロキシサーバーのみが保持
  • レート制限: デバイスIDごとに1日あたりのリクエスト数を制限
  • Redis永続化: Dockerコンテナ再起動後もカウントを維持
  • 認証: Bearer Token認証オプション
  • Dokploy対応: 将来のDokploy移行に対応した構成

🚀 クイックスタートSynology NAS

1. 環境変数ファイル作成

cp .env.example .env

.env を編集してAPIキーと認証トークンを設定:

GEMINI_API_KEY=AIzaSy...あなたのAPIキー
PROXY_AUTH_TOKEN=$(openssl rand -hex 32)  # ランダム生成推奨
DAILY_LIMIT=50

2. Docker Composeで起動

docker-compose up -d

3. 動作確認

curl http://localhost:8080/health
# 期待される出力: OK

📊 Redis データ確認

# Redisコンテナに接続
docker exec -it ponshu-redis redis-cli

# 使用状況確認
KEYS usage:*
GET "usage:device-123:2026-02-21"
TTL "usage:device-123:2026-02-21"

🔄 運用コマンド

# ログ確認
docker-compose logs -f proxy

# 再起動
docker-compose restart

# 停止
docker-compose stop

# 完全削除(データも削除)
docker-compose down -v

📚 ドキュメント


🏗️ アーキテクチャ

Flutter App (Client)
    ↓ HTTP POST /analyze
    ↓ Authorization: Bearer <token>
Proxy Server (Node.js/Express)
    ↓ Rate Limit Check (Redis)
    ↓ Gemini API Call
Gemini 2.5 Flash API

🔐 セキュリティ

  • .env ファイルは Git管理しない.gitignoreに追加済み)
  • PROXY_AUTH_TOKEN長いランダム文字列 を使用
  • 外部公開する場合は リバースプロキシ + HTTPS 推奨

📝 環境変数

変数名 説明 デフォルト
GEMINI_API_KEY Gemini APIキー必須 なし
PROXY_AUTH_TOKEN Bearer Token認証推奨 なし(認証無効)
DAILY_LIMIT 1日あたりのリクエスト上限 50
REDIS_HOST Redisホスト名 redis
REDIS_PORT Redisポート 6379
PORT Proxy Serverポート 8080

🆘 トラブルシューティング

Q: Redis connection failed

解決策: Redisコンテナが起動していることを確認

docker-compose ps
# ponshu-redis が "Up (healthy)" になっているか確認

Q: Flutter アプリから接続できない

解決策:

  1. .envPROXY_AUTH_TOKEN を確認
  2. Flutter の lib/secrets.local.dart で同じトークンを設定
  3. Synology NASのIPアドレス100.76.7.3)が正しいか確認

Last Updated: 2026-02-21