DGate Documentation
website·Indexed 32 days ago
https://dgate.ioA function-native, modular API gateway and high-performance open-source proxy server. DGate allows developers to manage ingress traffic using domains, namespaces, and routes, and extend functionality through modules. The ecosystem includes the DGate Server and dgate-cli for interacting with the Admin API.
What's inside DGate
- DGate is a modular, function-native API Gateway that acts as a high-level reverse proxy and entry point for microservices. It handles standard gateway tasks such as authentication, retrying, circuit breaker logic, rate limiting, and load balancing. It can route requests based on request path, method, or domain name. Key architectural features include an Admin API powered by a Badger key-value database for resource management and Raft consensus for high availability and automatic resource replication across multiple instances.
Extend DGate functionality using Modules
Modules in DGate are used to extend the core functionality of the gateway. They allow developers to modify incoming requests and outgoing responses, handle errors, and implement custom logic. Modules are executed in a specific sequence, and each module is provided with access to the request and response context.Understand the Secret data structure
Secrets are used to store sensitive information (e.g., API keys, passwords) within an application. Once a secret is created, it can be referenced in code, but it cannot be retrieved via the Admin API or CLI for security reasons.{ "name": "secret1", "namespace": "namespace1", "data": "secret", "tags": ["tag1", "tag2"] }Understand the DGate Document data structure
Documents in DGate are data entities stored in the database, organized into collections, and manipulated via the API. Unlike other DGate resources, documents are stored and accessed within modules and do not utilize tags. Each document is identified by a unique ID.{ "id": "document1", "collection": "collection1", "data": { "field1": "value1", "field2": "value2" }, "namespace": "namespace1" }Extend DGate functionality using Modules
Modules in DGate are used to extend the system's core functionality. They allow developers to modify incoming requests and outgoing responses, handle errors, and implement custom logic. Modules are executed in a specific sequence, and each module is granted access to the request and response context.Organize DGate resources using tags
Tags in DGate are used to categorize and organize resources, enabling grouped management and the implementation of access control and policies. They can be applied for advanced use cases such as permissions, canary deployments, and server tag-based resource management. Note that all DGate resources support tags except for Documents.Understand DGate Namespace organization and access control
Namespaces in DGate are used to organize resources, group them together, and manage access control and policies. Every resource in DGate belongs to a namespace, with the exception of Documents. A critical security constraint is that resources located in different namespaces cannot access each other.Extract matched path values in DGate modules
Within a module, you can retrieve the value of a matched routing pattern using thectx.pathParam()method. Use the parameter name defined in the curly braces (e.g.,'id') or the*character for wildcard matches.function testFunction(ctx: ModuleContext) { // If pattern is /route/{id} const id = ctx.pathParam('id'); // If pattern is /route/* const wildcardValue = ctx.pathParam('*'); }Initialize infrastructure for a URL shortener
To set up a URL shortener in DGate, you must create a namespace for isolation, a domain with a specific pattern to handle incoming requests, and a document collection to store the mapping between short IDs and original URLs.# Create a namespace for the URL shortener dgate-cli namespace create name=url_shortener-ns # Create a domain with a pattern for the URL shortener dgate-cli domain create \ name=url_shortener-dm \ patterns:='["url_shortener.com"]' \ namespace=url_shortener-ns # Create a collection for the short link documents using json schema dgate-cli collection create \ schema:='{"type":"object","properties":{"url":{"type":"string"}}}' \ name=short_link type=document namespace=url_shortener-nsInstall dgate-cli via Go
Install the DGate CLI using the Go toolchain to interact with the DGate Admin API.go install github.com/dgate-io/dgate/cmd/dgate-cli@latest dgate-cli --helpContribute to the DGate project
You can contribute to DGate by creating issues or submitting pull requests on the official GitHub repository.# DGate GitHub Repository https://github.com/dgate-io/dgateManage module execution and function exposure
To ensure a module function is triggered, it must be defined or exported in the first module listed in the route configuration.
If multiple modules are used in a route, only functions in the first module are triggered. Other modules can be imported into the first module to share logic, but functions exported only from secondary modules will be ignored by the DGate engine.
// main.ts (The first module in route config) import { requestModifier as reqMod } from './custom' export { fetchUpstream } from './custom' const requestModifier = async (ctx: ModuleContext) => { console.info("id", ctx.id, JSON.stringify(ctx)); return reqMod(ctx); }// Route Config { "path": "/", "method": "GET", "modules": ["main", "custom"] }