Skip to content

Repository files navigation

DGate v2 - Rust Edition

A high-performance API Gateway written in Rust, featuring JavaScript module support via QuickJS engine.

CICrates.ioDockerLicense: MIT

Features

  • High Performance: Built with Rust and async I/O using Tokio
  • Dynamic Routing: Configure routes, services, and domains dynamically via Admin API
  • Namespace Isolation: Organize resources into namespaces for multi-tenancy
  • Simple KV Storage: Document storage without JSON schema requirements
  • TLS Support: HTTPS with dynamic certificate loading per domain
  • Reverse Proxy: Forward requests to upstream services with load balancing support

Supported

  • HTTP/1.1 ✅
  • HTTP/2 ✅
  • WebSocket ✅
  • gRPC ✅
  • QUIC/HTTP3 ❌

Resource Diagram

┌─────────────────────────────────────────────────────────────┐
│ DGate V2 │
├─────────────────────────────────────────────────────────────┤
│ │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │
│ │ Domains │───▶│ Namespaces │───▶│ Routes │ │
│ └─────────────┘ └─────────────┘ └─────────────┘ │
│ │ │
│ ▼ │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │
│ │ Modules │◀───│ Handler │───▶│ Services │ │
│ │ (QuickJS) │ └─────────────┘ │ (Upstream) │ │
│ └─────────────┘ └─────────────┘ │
│ │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │
│ │ Documents │ │ Secrets │ │ Collections │ │
│ │ (KV) │ └─────────────┘ └─────────────┘ │
│ └─────────────┘ │
│ │
└─────────────────────────────────────────────────────────────┘

Installation

From Cargo (crates.io)

cargo install dgate

From Source

git clone https://github.com/dgate-io/dgate.git
cd dgate
cargo build --release

Docker

# Pull from GitHub Container Registry
docker pull ghcr.io/dgate-io/dgate:latest
# Run with a config file
docker run -d \
-p 80:80 -p 443:443 -p 9080:9080 \
-v $(pwd)/config.yaml:/app/config.yaml \
ghcr.io/dgate-io/dgate:latest

Quick Start

Run

# With default config
./target/release/dgate-server
# With custom config
./target/release/dgate-server -c config.dgate.yaml

Configuration

Create a config.dgate.yaml file:

version: v1log_level: infostorage:
type: filedir: .dgate/data/proxy:
port: 80host: 0.0.0.0admin:
port: 9080host: 0.0.0.0

Resources

Namespaces

Namespaces organize resources and provide multi-tenancy:

# Create a namespace
curl -X PUT http://localhost:9080/api/v1/namespace/myapp \
-H "Content-Type: application/json" \
-d '{"tags": ["production"]}'

Services

Services define upstream endpoints:

curl -X PUT http://localhost:9080/api/v1/service/myapp/backend \
-H "Content-Type: application/json" \
-d '{ "urls": ["http://backend-1:8080", "http://backend-2:8080"], "request_timeout_ms": 30000, "retries": 3 }'

Routes

Routes match incoming requests to services:

curl -X PUT http://localhost:9080/api/v1/route/myapp/api-route \
-H "Content-Type: application/json" \
-d '{ "paths": ["/api/**"], "methods": ["GET", "POST", "PUT", "DELETE"], "service": "backend", "strip_path": true }'

Modules

JavaScript modules for request/response processing:

# Create a module
curl -X PUT http://localhost:9080/api/v1/module/myapp/auth-check \
-H "Content-Type: application/json" \
-d '{ "moduleType": "javascript", "payload": "BASE64_ENCODED_JAVASCRIPT" }'

Module Functions:

// Modify request before proxyingfunctionrequestModifier(ctx){ctx.request.headers['X-Custom']='value';}// Handle request without upstream (serverless-style)functionrequestHandler(ctx){ctx.setStatus(200);ctx.json({message: 'Hello from DGate!'});}// Modify response after upstreamfunctionresponseModifier(ctx,res){res.headers['X-Processed']='true';}// Handle errorsfunctionerrorHandler(ctx,error){ctx.setStatus(500);ctx.json({error: error.message});}// Custom upstream URL selectionfunctionfetchUpstreamUrl(ctx){return'http://custom-backend:8080';}

Domains

Control ingress traffic routing:

curl -X PUT http://localhost:9080/api/v1/domain/myapp/main \
-H "Content-Type: application/json" \
-d '{ "patterns": ["*.myapp.com", "myapp.local"], "priority": 10 }'

Collections & Documents

Simple KV storage:

# Create a collection
curl -X PUT http://localhost:9080/api/v1/collection/myapp/users \
-H "Content-Type: application/json" \
-d '{"visibility": "private"}'# Create a document
curl -X PUT http://localhost:9080/api/v1/document/myapp/users/user-123 \
-H "Content-Type: application/json" \
-d '{ "data": { "name": "John Doe", "email": "john@example.com" } }'# Get document
curl http://localhost:9080/api/v1/document/myapp/users/user-123

Secrets

Store sensitive data:

curl -X PUT http://localhost:9080/api/v1/secret/myapp/api-key \
-H "Content-Type: application/json" \
-d '{"data": "sk-secret-key-123"}'

API Reference

Admin Endpoints

MethodPathDescription
GET/Root info
GET/healthHealth check
GET/readyzReadiness check
GET/api/v1/namespaceList namespaces
PUT/api/v1/namespace/:nameCreate/update namespace
DELETE/api/v1/namespace/:nameDelete namespace
GET/api/v1/routeList routes
PUT/api/v1/route/:ns/:nameCreate/update route
DELETE/api/v1/route/:ns/:nameDelete route
GET/api/v1/serviceList services
PUT/api/v1/service/:ns/:nameCreate/update service
DELETE/api/v1/service/:ns/:nameDelete service
GET/api/v1/moduleList modules
PUT/api/v1/module/:ns/:nameCreate/update module
DELETE/api/v1/module/:ns/:nameDelete module
GET/api/v1/domainList domains
PUT/api/v1/domain/:ns/:nameCreate/update domain
DELETE/api/v1/domain/:ns/:nameDelete domain
GET/api/v1/collectionList collections
PUT/api/v1/collection/:ns/:nameCreate/update collection
DELETE/api/v1/collection/:ns/:nameDelete collection
GET/api/v1/document?namespace=x&collection=yList documents
PUT/api/v1/document/:ns/:col/:idCreate/update document
DELETE/api/v1/document/:ns/:col/:idDelete document
GET/api/v1/secretList secrets
PUT/api/v1/secret/:ns/:nameCreate/update secret
DELETE/api/v1/secret/:ns/:nameDelete secret
GET/api/v1/changelogList change logs

Environment Variables

VariableDescriptionDefault
LOG_LEVELLogging levelinfo
PORTProxy server port80
PORT_SSLHTTPS port443
DG_DISABLE_BANNERDisable startup banner-

Performance

DGate v2 is built for high performance:

  • Async I/O: Non-blocking operations with Tokio
  • Zero-copy: Efficient buffer handling
  • Connection Pooling: Reuse upstream connections
  • Module Caching: Compiled JS modules are cached

Comparison with v1 (Go)

Featurev1 (Go)v2 (Rust)
RuntimeGo 1.21+Rust 1.75+
JS EnginegojaQuickJS (rquickjs)
HTTPchi/stdlibaxum/hyper
Storagebadger/fileredb/memory
DocumentsJSON SchemaSimple KV

Development

Running Tests

# Unit tests
cargo test# Functional tests
./functional-tests/run-all-tests.sh
# Performance tests (requires k6)
./perf-tests/run-tests.sh quick

Building for Release

cargo build --release

License

MIT License

Releases

Packages

Used by

Contributors

Languages