Skip to content

Latest commit

 

History

History
552 lines (434 loc) · 10.8 KB

File metadata and controls

552 lines (434 loc) · 10.8 KB

Development Guide

Complete guide for local development of VideoGen Messenger.

Quick Start

# Clone repository
git clone https://github.com/yourusername/videogen-messenger.git
cd videogen-messenger

# Install backend dependencies
cd backend
npm install

# Copy environment template
cp .env.example .env

# Start development server
npm run dev

Prerequisites

Required Software

  • Node.js: v18.0.0 or higher
  • npm: v9.0.0 or higher
  • Docker Desktop: Latest version
  • Git: Latest version
  • PostgreSQL: v15+ (or use Docker)
  • Redis: v7+ (or use Docker)
  • Elasticsearch: v8+ (or use Docker)

Optional Tools

  • Postman/Insomnia: API testing
  • pgAdmin: Database management
  • RedisInsight: Redis visualization
  • Kibana: Elasticsearch management

Local Environment Setup

1. Database Setup

Option A: Docker (Recommended)

# PostgreSQL
docker run --name videogen-postgres \
  -e POSTGRES_DB=videogen_dev \
  -e POSTGRES_USER=postgres \
  -e POSTGRES_PASSWORD=postgres \
  -p 5432:5432 \
  -d postgres:15-alpine

# Redis
docker run --name videogen-redis \
  -p 6379:6379 \
  -d redis:7-alpine

# Elasticsearch
docker run --name videogen-elasticsearch \
  -e "discovery.type=single-node" \
  -e "xpack.security.enabled=false" \
  -p 9200:9200 \
  -p 9300:9300 \
  -d elasticsearch:8.11.0

Option B: Docker Compose

Create docker-compose.dev.yml:

version: '3.8'

services:
  postgres:
    image: postgres:15-alpine
    environment:
      POSTGRES_DB: videogen_dev
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
    ports:
      - "5432:5432"
    volumes:
      - postgres_data:/var/lib/postgresql/data

  redis:
    image: redis:7-alpine
    ports:
      - "6379:6379"
    volumes:
      - redis_data:/data

  elasticsearch:
    image: elasticsearch:8.11.0
    environment:
      - discovery.type=single-node
      - xpack.security.enabled=false
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
    ports:
      - "9200:9200"
      - "9300:9300"
    volumes:
      - es_data:/usr/share/elasticsearch/data

volumes:
  postgres_data:
  redis_data:
  es_data:

Start services:

docker-compose -f docker-compose.dev.yml up -d

2. Environment Variables

Create .env file in backend/:

# Application
NODE_ENV=development
PORT=3000
API_VERSION=v1

# Database
DATABASE_URL=postgresql://postgres:postgres@localhost:5432/videogen_dev
DATABASE_POOL_MIN=2
DATABASE_POOL_MAX=10

# Redis
REDIS_URL=redis://localhost:6379
REDIS_DB=0

# Elasticsearch
ELASTICSEARCH_NODE=http://localhost:9200
ELASTICSEARCH_INDEX=videos_dev

# AWS (use LocalStack for local development)
AWS_REGION=us-east-1
AWS_ACCESS_KEY_ID=test
AWS_SECRET_ACCESS_KEY=test
AWS_S3_BUCKET_VIDEOS=videogen-videos-dev
AWS_S3_BUCKET_THUMBNAILS=videogen-thumbnails-dev
AWS_S3_ENDPOINT=http://localhost:4566  # LocalStack

# AI Providers (use test keys for development)
GOOGLE_VEO_API_KEY=your_test_key_here
RUNWAY_API_KEY=your_test_key_here
MINIMAX_API_KEY=your_test_key_here

# Security
JWT_SECRET=dev-secret-change-in-production-min-32-chars
JWT_EXPIRY=24h
API_KEY_SALT=dev-salt-change-in-production

# Rate Limiting
RATE_LIMIT_WINDOW_MS=900000
RATE_LIMIT_MAX_REQUESTS=1000

# Logging
LOG_LEVEL=debug

3. Database Migration

# Run migrations
npm run migrate

# Check migration status
npm run migrate:status

# Rollback last migration
npm run migrate:rollback

# Seed database with test data
npm run db:seed

4. Start Development Server

# Start with auto-reload
npm run dev

# Server will start on http://localhost:3000

Project Structure

backend/
├── api/
│   ├── server.js              # Express app entry point
│   ├── routes/                # Route definitions
│   │   ├── index.js
│   │   ├── generation.js
│   │   ├── search.js
│   │   ├── trending.js
│   │   ├── videos.js
│   │   └── auth.js
│   ├── controllers/           # Request handlers
│   │   ├── generationController.js
│   │   ├── searchController.js
│   │   ├── trendingController.js
│   │   ├── videoController.js
│   │   └── authController.js
│   ├── middleware/            # Express middleware
│   │   ├── auth.js
│   │   ├── validation.js
│   │   ├── rateLimit.js
│   │   └── errorHandler.js
│   └── utils/                 # Utility functions
│       └── logger.js
├── services/                  # Business logic
│   ├── generation/
│   │   └── GenerationService.js
│   ├── search/
│   │   └── SearchService.js
│   ├── queue/
│   │   └── QueueService.js
│   ├── trending/
│   │   └── TrendingService.js
│   └── video/
│       └── VideoService.js
├── __tests__/                 # Test files
│   ├── unit/
│   ├── integration/
│   └── e2e/
├── migrations/                # Database migrations
├── scripts/                   # Utility scripts
├── package.json
└── .env

Running Tests

# Run all tests
npm test

# Run tests in watch mode
npm test -- --watch

# Run tests with coverage
npm test -- --coverage

# Run specific test file
npm test -- generation.test.js

# Run unit tests only
npm test -- __tests__/unit

# Run integration tests only
npm test -- __tests__/integration

# Run E2E tests only
npm test -- __tests__/e2e

Code Style Guide

ESLint Configuration

// .eslintrc.js
module.exports = {
  env: {
    node: true,
    es2021: true,
    jest: true
  },
  extends: 'eslint:recommended',
  parserOptions: {
    ecmaVersion: 'latest',
    sourceType: 'module'
  },
  rules: {
    'no-console': 'warn',
    'no-unused-vars': ['error', { argsIgnorePattern: '^_' }],
    'prefer-const': 'error',
    'no-var': 'error'
  }
};

Prettier Configuration

{
  "semi": true,
  "singleQuote": true,
  "tabWidth": 2,
  "trailingComma": "none",
  "printWidth": 100
}

Run Linting

# Check for lint errors
npm run lint

# Fix auto-fixable issues
npm run lint -- --fix

# Format code with Prettier
npm run format

Git Workflow

Branch Naming

feature/description      # New features
bugfix/description       # Bug fixes
hotfix/description       # Production hotfixes
refactor/description     # Code refactoring
test/description         # Test additions
docs/description         # Documentation updates

Commit Messages

Follow conventional commits:

feat: add video sharing functionality
fix: resolve database connection timeout
docs: update API documentation
test: add unit tests for SearchService
refactor: optimize search query builder
perf: improve thumbnail generation speed
chore: update dependencies

Development Flow

# Create feature branch
git checkout -b feature/video-sharing

# Make changes and commit
git add .
git commit -m "feat: add video sharing endpoint"

# Push to remote
git push origin feature/video-sharing

# Create pull request on GitHub
# After review, merge to main

API Testing

Using Postman

Import the Postman collection:

# Collection located at
docs/postman/VideoGen-Messenger.postman_collection.json

Using cURL

# Health check
curl http://localhost:3000/health

# Generate video
curl -X POST http://localhost:3000/api/v1/generate \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_JWT_TOKEN" \
  -d '{
    "prompt": "A beautiful sunset over mountains",
    "quality": "hd",
    "duration": 5
  }'

# Check generation status
curl http://localhost:3000/api/v1/generate/{jobId}/status \
  -H "Authorization: Bearer YOUR_JWT_TOKEN"

# Search videos
curl "http://localhost:3000/api/v1/search?q=sunset&limit=10"

Debugging

VS Code Configuration

Create .vscode/launch.json:

{
  "version": "0.2.0",
  "configurations": [
    {
      "type": "node",
      "request": "launch",
      "name": "Debug Server",
      "skipFiles": ["<node_internals>/**"],
      "program": "${workspaceFolder}/backend/api/server.js",
      "envFile": "${workspaceFolder}/backend/.env"
    },
    {
      "type": "node",
      "request": "launch",
      "name": "Debug Tests",
      "program": "${workspaceFolder}/backend/node_modules/.bin/jest",
      "args": ["--runInBand", "--no-cache"],
      "console": "integratedTerminal"
    }
  ]
}

Debugging Tips

  1. Enable Debug Logging:

    LOG_LEVEL=debug npm run dev
  2. Use Debugger Statements:

    debugger; // Breakpoint in code
  3. Inspect Redis:

    redis-cli
    KEYS *
    GET generation:job:123
  4. Check Elasticsearch:

    # Check cluster health
    curl http://localhost:9200/_cluster/health
    
    # Search index
    curl http://localhost:9200/videos_dev/_search?pretty
  5. View Logs:

    tail -f logs/app.log

Common Development Tasks

Add New API Endpoint

  1. Create route in api/routes/:

    router.post('/new-endpoint', validateRequest, controller.handler);
  2. Create controller in api/controllers/:

    export async function handler(req, res, next) {
      try {
        // Implementation
        res.json({ success: true });
      } catch (error) {
        next(error);
      }
    }
  3. Add tests in __tests__/integration/api/

  4. Update API documentation in docs/API.md

Add New Service

  1. Create service file in services/:

    class NewService {
      constructor() {}
      async method() {}
    }
    export default NewService;
  2. Add unit tests in __tests__/unit/services/

  3. Import and use in controllers

Add Database Migration

# Create migration file
touch migrations/002_add_new_table.sql
-- Up migration
CREATE TABLE new_table (
    id UUID PRIMARY KEY,
    name VARCHAR(255)
);

-- Down migration
DROP TABLE new_table;

Performance Profiling

CPU Profiling

node --prof api/server.js
node --prof-process isolate-*.log > profile.txt

Memory Profiling

node --inspect api/server.js
# Open chrome://inspect in Chrome

Load Testing

# Install autocannon
npm install -g autocannon

# Run load test
autocannon -c 100 -d 30 http://localhost:3000/api/v1/search?q=test

Troubleshooting

See TROUBLESHOOTING.md for common issues.

Additional Resources