OpenGate
π Overview
OpenGate is a modern, lightweight API Gateway built in Go that provides intelligent request routing, authentication, and middleware management for microservices architectures. It offers dynamic route configuration, built-in authentication strategies, and high-performance request forwarding.
β¨ Key Features
- π Dynamic Route Management: File-based route configuration with hot-reload capabilities
- π Flexible Authentication: Support for multiple authentication strategies including OpenAuth
- β‘ High Performance: Built on Gin framework for optimal request handling
- π§ Middleware Support: Extensible middleware pipeline with CORS, logging, and custom middleware
- π Health Monitoring: Built-in health checks and ping endpoints
- π³ Container Ready: Docker support for easy deployment
- π Multiple Backends: Support for local file-based and remote configuration sources
- β±οΈ Configurable Timeouts: Per-route timeout configuration
- π Hot Configuration Reload: Automatic detection and reload of configuration changes
ποΈ Architecture
βββββββββββββββββββ ββββββββββββββββββββ βββββββββββββββββββ
β Client β β OpenGate β β Backend β
β Application ββββββ API Gateway ββββββ Services β
βββββββββββββββββββ ββββββββββββββββββββ βββββββββββββββββββ
β
βΌ
ββββββββββββββββββββ
β Configuration β
β Management β
ββββββββββββββββββββ
OpenGate acts as a reverse proxy and load balancer, sitting between client applications and backend services, providing:
- Request routing based on path prefixes
- Authentication and authorization
- Request/response transformation
- Traffic management and monitoring
π¦ Installation
Prerequisites
- Go 1.23.3 or higher
- Docker (optional, for containerized deployment)
From Source
# Clone the repository
git clone https://github.com/gofreego/opengate.git
cd opengate
# Install dependencies
go mod tidy
# Build the application
make build
# Run the application
./application -env=dev -path=./
Using Docker
# Build Docker image
make docker
# Run with Docker
make docker-run
Using Go Install
# Install required tools
make install
# Run directly
make run
βοΈ Configuration
OpenGate uses YAML configuration files for setup. The main configuration file (dev.yaml) defines:
Server Configuration
Server:
Port: 8080
GinMode: debug
ReadTimeout: 30s
WriteTimeout: 30s
IdleTimeout: 120s
MaxHeaderBytes: 1048576
EnableCors: true
Repository Configuration
Repository:
Name: Local # or "OpenAuth" for remote configuration
Local:
RoutesFolderPath: ./resources/configs/routes/
OpenAuth:
endpoint: localhost:8086
username: admin
password: admin123
timeout: 5s
tls: false
Service Configuration
Service:
ChangeDetector:
RouteUpdateInterval: 30 # seconds
Auth:
Name: "OpenAuth"
π£οΈ Route Configuration
Routes are defined in YAML files within the routes directory. Each service gets its own configuration file:
# resources/configs/routes/example-service.yaml
Name: example-service
PathPrefix: /api/v1/users
TargetURL: http://localhost:3001
StripPrefix: false
Authentication:
Required: true
Except:
- Path: "/api/v1/users/health"
Methods: ["GET"]
- Path: "/api/v1/users/metrics"
Methods: ["GET", "POST"]
Middleware:
- cors
- logging
Timeout: 30s
Route Configuration Options
| Field |
Type |
Description |
Name |
string |
Service identifier for logging and management |
PathPrefix |
string |
URL path prefix that triggers this route |
TargetURL |
string |
Backend service URL where requests are forwarded |
StripPrefix |
boolean |
Whether to remove the path prefix before forwarding |
Authentication.Required |
boolean |
Whether authentication is required |
Authentication.Except |
array |
Paths/methods exempt from auth requirements |
Middleware |
array |
Ordered list of middleware to apply |
Timeout |
duration |
Request timeout for this route |
π Authentication
OpenGate supports multiple authentication strategies:
OpenAuth Integration
Service:
Auth:
Name: "OpenAuth"
Repository:
OpenAuth:
endpoint: auth-service:8086
username: gateway-user
password: secure-password
timeout: 5s
tls: true
Route-Level Authentication
Authentication:
Required: true
Except:
- Path: "/health"
Methods: ["GET"]
- Path: "/public/*"
Methods: ["GET", "POST"]
π Getting Started
1. Basic Setup
# Clone and build
git clone https://github.com/gofreego/opengate.git
cd opengate
make build
Create a route configuration file:
# resources/configs/routes/my-service.yaml
Name: my-service
PathPrefix: /api/my-service
TargetURL: http://localhost:3000
StripPrefix: true
Authentication:
Required: false
Middleware:
- cors
Timeout: 30s
3. Start the Gateway
./application -env=dev -path=./
4. Test Your Route
# Health check
curl http://localhost:8080/ping
# Test your service route
curl http://localhost:8080/api/my-service/endpoint
π οΈ Development
Project Structure
opengate/
βββ cmd/
β βββ http_server/ # HTTP server implementation
βββ internal/
β βββ configs/ # Configuration management
β βββ constants/ # Application constants
β βββ models/ # Data models
β βββ repository/ # Data access layer
β βββ service/ # Business logic
β βββ auth/ # Authentication services
β βββ change_detector/ # Configuration change detection
β βββ route_manager/ # Route management
βββ pkg/
β βββ utils/ # Utility packages
βββ resources/
β βββ configs/routes/ # Route configurations
β βββ images/ # Static assets
βββ test/ # Test files
Building and Testing
# Run tests
make test
# Build for Linux
make build-linux
# Clean build artifacts
make clean
# Run development server
make run
Adding New Routes
- Create a new YAML file in
resources/configs/routes/
- Define your route configuration
- OpenGate will automatically detect and load the new route
- Test the route with your preferred HTTP client
Custom Middleware
OpenGate supports extensible middleware. To add custom middleware:
- Implement the middleware in the service layer
- Register it in the middleware registry
- Reference it in your route configuration
π Monitoring and Health Checks
Health Endpoints
GET /ping - Basic health check
GET /health - Detailed health status (if configured)
Logging
OpenGate provides structured logging with configurable levels:
Logger:
AppName: opengate
Build: dev
Level: debug # debug, info, warn, error
π³ Docker Deployment
Using the Provided Dockerfile
# Build image
docker build -t opengate .
# Run container
docker run -d \
--name opengate \
-p 8080:8080 \
-v $(pwd)/resources/configs:/app/resources/configs \
opengate
Docker Compose Example
version: '3.8'
services:
opengate:
build: .
ports:
- "8080:8080"
volumes:
- ./resources/configs:/app/resources/configs
- ./dev.yaml:/app/dev.yaml
environment:
- ENV=production
OpenGate is designed for high performance:
- Gin Framework: Fast HTTP routing and middleware
- Connection Pooling: Efficient backend connections
- Configurable Timeouts: Prevent hanging requests
- Hot Reload: Zero-downtime configuration updates
- Lightweight: Minimal resource footprint
Server:
ReadTimeout: 30s # Adjust based on your needs
WriteTimeout: 30s # Balance between performance and reliability
IdleTimeout: 120s # Connection keep-alive duration
MaxHeaderBytes: 1048576 # Maximum header size
π€ Contributing
We welcome contributions! Please see our contributing guidelines:
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature)
- Commit your changes (
git commit -m 'Add amazing feature')
- Push to the branch (
git push origin feature/amazing-feature)
- Open a Pull Request
Development Guidelines
- Follow Go best practices and conventions
- Add tests for new functionality
- Update documentation for API changes
- Ensure all tests pass before submitting
π License
This project is licensed under the MIT License - see the LICENSE file for details.
π Support
πΊοΈ Roadmap
- Rate limiting and throttling
- WebSocket support
- Circuit breaker pattern
- Metrics and observability improvements
- gRPC gateway support
- Load balancing strategies
- Admin API for runtime configuration
π Acknowledgments
- Built with Gin - High-performance HTTP web framework
- Powered by GoUtils - Utility libraries
- Authentication via OpenAuth - Authentication service
Made with β€οΈ by the GoFreego Team