Go to file
Oleg Proskurin fa65264410 feat: phase 3 part 3 - scope management and IP rate limiting
Implement comprehensive live scope management API and IP-based rate limiting
for live URL generation endpoints (Sections 8.5 and 8.6).

**Scope Management CRUD Endpoints:**
- POST /api/v1/live/scopes - Create scope manually with slug validation
- GET /api/v1/live/scopes - List scopes with pagination and stats
- GET /api/v1/live/scopes/:slug - Get single scope by slug with stats
- PUT /api/v1/live/scopes/:slug - Update scope settings
- POST /api/v1/live/scopes/:slug/regenerate - Regenerate scope images
- DELETE /api/v1/live/scopes/:slug - Delete scope with cascading image deletion

**Scope Management Features:**
- Slug format validation (alphanumeric, hyphens, underscores)
- Duplicate slug prevention with 409 Conflict response
- Scope statistics (currentGenerations, lastGeneratedAt)
- Settings management (allowNewGenerations, newGenerationsLimit)
- Regeneration support (single image or all images in scope)
- Hard delete with image cleanup following alias protection rules
- All endpoints require Project Key authentication

**IP-Based Rate Limiting:**
- In-memory rate limit store with automatic cleanup
- Limits: 10 new generations per hour per IP address
- Only cache MISS (new generation) counts toward limit
- Cache HIT does NOT count toward limit
- X-Forwarded-For header support for proxy/load balancer setups
- Rate limit headers: X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset
- Retry-After header on 429 Too Many Requests response
- Automatic cleanup of expired entries every 5 minutes

**IP Rate Limiter Middleware:**
- ipRateLimiterMiddleware attaches to live URL endpoint
- getClientIp() extracts IP from X-Forwarded-For or req.ip
- checkIpRateLimit() validates and increments counter
- getRemainingRequests() returns available request count
- getResetTime() returns seconds until reset
- Middleware attaches checkIpRateLimit function to request
- Rate limit check executed AFTER cache check (only for cache MISS)

**Type System Updates:**
- Added LiveScopeResponse interface with all scope fields
- Added LiveScopeWithImagesResponse with images array
- Added response type aliases for all CRUD operations
- Added toLiveScopeResponse() converter function
- Added CreateLiveScopeRequest, UpdateLiveScopeRequest interfaces
- Added ListLiveScopesQuery with pagination parameters
- Added RegenerateScopeRequest with optional imageId

**Route Integration:**
- Mounted scopes router at /api/v1/live/scopes
- Applied ipRateLimiterMiddleware to live URL endpoint
- Rate limit increments only on cache MISS (new generation)
- Cache HIT bypasses rate limit check entirely

**Technical Notes:**
- All scope endpoints return toLiveScopeResponse() format
- Pagination using buildPaginationMeta helper
- Bracket notation for meta field access (TypeScript strict mode)
- Proper number parsing with fallback defaults
- All Phase 3 Part 3 code is fully type-safe with zero TypeScript errors

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-17 23:15:29 +07:00
.claude chore: add agents 2025-10-05 23:10:50 +07:00
apps feat: phase 3 part 3 - scope management and IP rate limiting 2025-11-17 23:15:29 +07:00
docs feat: phase 1 - parameter renames, auto-detection, and flowId logic 2025-11-17 01:20:49 +07:00
packages/database feat: phase 3 part 1 - live scopes database schema and service 2025-11-17 22:50:20 +07:00
prod-env feat: update setup 2025-10-12 21:12:58 +07:00
scripts feat: minio integration 2025-09-28 22:43:06 +07:00
tests fix: service and tests 2025-11-10 00:57:06 +07:00
.dockerignore feat: add docker ignore 2025-09-27 00:32:36 +07:00
.env.example feat: log generation requests 2025-10-06 23:25:20 +07:00
.gitignore feat: update setup 2025-10-12 21:12:58 +07:00
.mcp.json feat: add DocPage 2025-10-21 22:47:45 +07:00
.prettierignore chore: prettier 2025-10-09 23:16:42 +07:00
.prettierrc.json feat: add prettier 2025-10-09 22:48:29 +07:00
CLAUDE.md chore: update docs 2025-10-12 22:19:10 +07:00
README.md feat: update setup 2025-10-12 21:12:58 +07:00
UPLOAD_COMPONENTS_DELIVERY.md feat: upload page 2025-10-11 01:02:13 +07:00
UPLOAD_PAGE_IMPLEMENTATION.md feat: upload page 2025-10-11 01:02:13 +07:00
api-refactoring-final.md feat: update refactor requirements 2025-11-17 00:46:44 +07:00
banatie-api-requirements.md chore: update documentation 2025-11-09 21:24:14 +07:00
banatie-database-design.md feat: add documentation 2025-10-26 22:26:02 +07:00
banatie.code-workspace chore: add workspace 2025-10-04 00:47:01 +07:00
demo.test.ts feat: add test 2025-10-09 23:27:15 +07:00
infrastructure.md chore: prettier 2025-10-09 23:16:42 +07:00
minio-setup.md chore: prettier 2025-10-09 23:16:42 +07:00
package.json fix: tests 2025-11-10 00:09:40 +07:00
pnpm-lock.yaml fix: tests 2025-11-10 00:09:40 +07:00
pnpm-workspace.yaml feat: implement apikey auth 2025-10-01 00:14:14 +07:00
test-api.sh feat: remove unused endpoints 2025-10-09 23:48:25 +07:00
test-filename-sanitization.js feat: filename sanitization 2025-10-11 18:11:56 +07:00
vitest.config.ts feat: add test 2025-10-09 23:27:15 +07:00

README.md

Banatie - AI Image Generation Service

A comprehensive monorepo for the Banatie AI image generation platform, featuring multiple applications for different use cases.

Architecture Overview

banatie-service/
├── apps/
│   ├── api-service/     # REST API for image generation (Express.js + TypeScript)
│   ├── landing/         # Landing page with demo (Next.js)
│   ├── studio/          # SaaS platform with billing (Next.js + Supabase + Stripe)
│   └── admin/           # Administration dashboard (Next.js)
├── data/                # Docker volumes (postgres, minio)
├── docker-compose.yml   # Infrastructure services
└── package.json         # Workspace scripts

Applications

🚀 API Service (apps/api-service)

  • Port: 3000
  • Tech: Express.js, TypeScript, Gemini AI
  • Purpose: Core REST API for AI image generation
  • Features: Image generation, file upload, rate limiting, logging

🌐 Landing Page (apps/landing)

  • Port: 3001
  • Tech: Next.js 14, Tailwind CSS
  • Purpose: Public landing page with demo
  • Features: Service overview, image generation demo

🏢 Studio (apps/studio)

  • Port: 3002
  • Tech: Next.js 14, Supabase, Stripe
  • Purpose: SaaS platform for paid users
  • Features: Authentication, billing, subscriptions, usage tracking

🔧 Admin (apps/admin)

  • Port: 3003
  • Tech: Next.js 14, Dashboard components
  • Purpose: Service administration and monitoring
  • Features: System monitoring, user management, analytics

Quick Start

Prerequisites

  • Node.js >= 18.0.0
  • pnpm >= 8.0.0
  • Docker & Docker Compose

Development Setup

# Clone and install dependencies
git clone <repository-url>
cd banatie-service
pnpm install

# Start infrastructure (PostgreSQL + MinIO)
docker compose up -d postgres minio storage-init

# Start all applications in development mode
pnpm dev

# Or start individual apps
pnpm dev:api      # API Service on :3000
pnpm dev:landing  # Landing Page on :3001
pnpm dev:studio   # Studio Platform on :3002
pnpm dev:admin    # Admin Dashboard on :3003

Production Deployment

# Build all applications
pnpm build

# Start infrastructure
docker compose up -d

# Start production servers
pnpm start:api
pnpm start:landing
pnpm start:studio
pnpm start:admin

Development Commands

# Install dependencies for all apps
pnpm install

# Development (all apps in parallel)
pnpm dev

# Build all apps
pnpm build

# Type checking
pnpm typecheck

# Linting
pnpm lint

# Testing (API service only)
pnpm test

# Clean all build outputs
pnpm clean

Environment Configuration

Each application has its own environment configuration:

  • Root: .env.docker (for Docker services)
  • API Service: apps/api-service/.env
  • Studio: apps/studio/.env.local
  • Admin: apps/admin/.env.local

See individual app README files for specific environment variables.

Services & Ports

Service Port Description
API Service 3000 Core REST API
Landing Page 3001 Public website
Studio Platform 3002 SaaS application
Admin Dashboard 3003 Administration
PostgreSQL 5460 Database
MinIO API 9000 Object storage
MinIO Console 9001 Storage admin

API Usage

Generate Image

# Text-to-image
curl -X POST http://localhost:3000/api/text-to-image \
  -H "Content-Type: application/json" \
  -H "X-API-Key: YOUR_API_KEY" \
  -d '{
    "prompt": "A magical forest with glowing mushrooms",
    "filename": "magical_forest"
  }'

See apps/api-service/README.md for detailed API documentation.

Contributing

  1. Each app has its own package.json and dependencies
  2. Use the root workspace commands for multi-app operations
  3. Follow the existing code style and patterns
  4. Run pnpm typecheck and pnpm lint before committing

License

MIT License - see individual app directories for more details.