Skip to content

A cache-enabled WeaveVM gateway written in JavaScript

License

Notifications You must be signed in to change notification settings

weaveVM/resolver.bot

Repository files navigation

WVM Network Gateway

A high-performance gateway service that provides easy access to WVM Network bundles with local file caching, application tracking, and automatic content-type detection.

A hosted gateway deployed from this repo is available to test at resolver.bot

Features

  • Local file caching with SQLite database
  • Application-based content organization
  • Automatic content-type detection
  • Cache management and cleanup
  • Comprehensive logging
  • Cache statistics and monitoring
  • Docker deployment support
  • CORS enabled

Quick Start

Using npm

# Install dependencies
npm install

# Create configuration
cp config/example.json config/default.json

# Start the server
npm start

Using Docker

# Build and start the container
docker compose up -d

# View logs
docker compose logs -f

Development

We provide a development helper script for common tasks:

# Make script executable
chmod +x scripts/dev.sh

# Show available commands
./scripts/dev.sh help

# Set up development environment
./scripts/dev.sh setup

# Start development server
./scripts/dev.sh start

# Run tests
./scripts/dev.sh test

For more detailed development instructions, see Development Guide.

Configuration

The gateway can be configured using JSON configuration files. See Configuration Guide for detailed settings documentation.

Environment-specific configurations:

  • config/default.json - Default settings
  • config/development.json - Development settings (smaller cache, more logging)
  • config/example.json - Example configuration template

API Usage

Bundle Content

GET /bundle/:txHash/:index

Parameters

  • txHash: Transaction hash of the bundle
  • index: Index of the envelope within the bundle (zero-based)

Response Headers

  • Content-Type: Automatically detected content type
  • Content-Length: Size of the content in bytes
  • Cache-Control: public, max-age=31536000 (1 year)
  • X-Cache: HIT/MISS (indicates if content was served from cache)

Example

curl http://localhost:3001/bundle/0xf4aa3cc6580d1ecbc93a9c015087718bee67290a6cf14587bbae7ef69e8e65fe/0

Cache Management

Get Statistics

GET /cache/stats

Returns overall cache statistics.

List Applications

GET /cache/apps

Returns list of applications with cached content.

Get Application Content

GET /cache/apps/:appName

Returns all cached content for an application.

Get Application Statistics

GET /cache/apps/:appName/stats

Returns statistics for an application.

Clear Cache

POST /cache/clear                  # Clear all cache
POST /cache/apps/:appName/clear   # Clear application cache

Bundle Tags

The gateway uses these tags from bundles:

  • Content-Type: Content type (optional)
  • Application-Name: Application grouping (optional, defaults to "unknown")

Development

Project Structure

.
├── config/             # Configuration files
├── docker/            # Docker documentation
├── scripts/           # Helper scripts
├── src/
│   └── lib/           # Core modules
│       ├── config.js      # Configuration management
│       ├── database.js    # SQLite database operations
│       ├── logger.js      # Logging functionality
│       └── cacheManager.js # Cache management
├── tests/
│   ├── unit/         # Unit tests
│   ├── integration/  # Integration tests
│   └── fixtures/     # Test fixtures
└── index.js          # Application entry point

Testing

Run tests:

npm test                  # Run all tests
npm run test:watch       # Watch mode
npm run test:coverage    # Coverage report

Code Quality

npm run lint            # Run ESLint
npm run format          # Format with Prettier

Deployment

Requirements

  • Node.js 16 or higher
  • Persistent storage for cache
  • Write access to cache/logs directories

System Service Installation

  1. Using Installation Script:
sudo ./scripts/install-service.sh
  1. Using Docker: See Docker Deployment Guide

The installation script:

  • Creates service user and group
  • Sets up directories and permissions
  • Installs systemd service
  • Configures monitoring and log rotation
  • Starts and enables the service

Environment Variables

  • NODE_ENV: Environment mode (development, production)
  • CONFIG_PATH: Path to configuration file
  • PORT: Server port (overrides config)
  • HOST: Server host (overrides config)
  • LOG_LEVEL: Logging level (overrides config)

Monitoring and Management

Cache Management

# View cache statistics
npm run cache:stats

# List applications
npm run cache:apps

# Verify cache integrity
npm run cache:verify

# Run cache cleanup
npm run cache:cleanup

# Advanced cache operations
./scripts/cache-manager.js help

System Monitoring

The gateway includes automated monitoring:

  • Service health checks (every 5 minutes)
  • Cache cleanup (daily)
  • Cache integrity verification (weekly)
  • Statistics export (daily)
  • Log rotation (weekly)

Configure monitoring alerts:

  1. Edit scripts/monitor.sh
  2. Set ALERT_EMAIL for email notifications
  3. Set SLACK_WEBHOOK for Slack notifications

View monitoring status:

# Run manual check
./scripts/monitor.sh

# View monitoring logs
tail -f /var/log/wvm-gateway/monitor.log

Log Management

Logs are automatically rotated with these settings:

  • Application logs: Daily rotation, 14 days retention
  • Statistics files: Weekly rotation, 52 weeks retention
  • All logs are compressed after rotation

Log locations:

  • Application logs: /var/log/wvm-gateway/*.log
  • Statistics: /var/log/wvm-gateway/stats/*.json
  • System logs: journalctl -u wvm-gateway

Maintenance

Backup

The gateway provides multiple backup options:

  1. Configuration backup
  2. Cache data backup
  3. Log files backup

Use the uninstall script with backup option:

sudo ./scripts/uninstall.sh
# Choose 'y' when prompted for backup

Upgrades

  1. Stop the service:
sudo systemctl stop wvm-gateway
  1. Update files and restart:
git pull
sudo systemctl restart wvm-gateway

Uninstallation

To remove the gateway and all its components:

sudo ./scripts/uninstall.sh

The script will:

  • Stop and disable the service
  • Remove configuration files
  • Clean up cache and logs
  • Remove system user and group
  • Optionally create backups

Error Handling

The API returns appropriate status codes:

  • 400: Invalid parameters/input
  • 404: Envelope not found
  • 500: Internal server error
  • Status codes from bundler.wvm.network are passed through

For detailed error handling information, see System Administration Guide.

Documentation

License

ISC

About

A cache-enabled WeaveVM gateway written in JavaScript

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published