An API Gateway built with Rust. This provides a robust solution for managing API traffic, featuring authentication, rate limiting and caching.
-
Authentication
-
Bearer token authentication
-
Configurable token validation
-
Secure token management
-
Rate Limiting
- Per-client rate limiting
- Configurable time windows
- Protection against DoS attacks
-
Caching
- In-memory caching for GET requests
- Configurable cache duration
- Automatic cache cleanup
-
High Performance
- Built with Rust's async/await
- Efficient memory usage
- Connection pooling
-
Proxy Capabilities
- Request/Response transformation
- Path-based routing
- Backend service proxying
-
Monitoring
-
Request/Response logging
-
Performance metrics
-
Error tracking
api-gateway/
├── src/
│ ├── services/ # Core business logic
│ │ ├── mod.rs
│ │ └── tests.rs
│ ├── handlers/ # Request handlers
│ │ ├── mod.rs
│ │ └── tests.rs
│ ├── middleware/ # HTTP middleware
│ │ ├── mod.rs
│ │ └── tests.rs
│ ├── lib.rs # Library definitions
│ ├── main.rs # Application entry point
│ ├── config.rs # Configuration
│ ├── error.rs # Error handling
│ └── models.rs # Data structures
└── tests/
└── integration_tests.rs
- Rust 1.75 or higher
- Cargo package manager
- A backend service to proxy to
- Clone the repository:
git clone https://github.com/miky-rola/api-gateway
cd api-gateway- Build the project:
cargo build --release- Configure the gateway in
config.rs:
pub const BACKEND_BASE: &str = "http://localhost:8081";
pub const RATE_LIMIT_REQUESTS: u32 = 100;
pub const RATE_LIMIT_WINDOW_SECS: u64 = 60;- Run the gateway:
cargo run --releaseThe gateway will start on http://127.0.0.1:3030
| Parameter | Description | Default |
|---|---|---|
BACKEND_BASE |
Backend service URL | http://localhost:8081 |
RATE_LIMIT_REQUESTS |
Requests per window | 100 |
RATE_LIMIT_WINDOW_SECS |
Rate limit window | 60 seconds |
REQUEST_TIMEOUT_SECS |
Request timeout | 30 seconds |
CACHE_DURATION_SECS |
Cache duration | 300 seconds |
STRIP_PATH_PREFIX |
Path prefix to strip | /api |
curl http://localhost:3030/healthcurl -H "Authorization: Bearer example-token" \
http://localhost:3030/api/your-endpointcurl -H "Authorization: Bearer example-token" \
http://localhost:3030/api/cached-endpointRun all tests:
cargo testRun specific test categories:
# Unit tests
cargo test --lib
# Integration tests
cargo test --test integration_tests
# With logging
RUST_LOG=debug cargo test# Enable debug logging
RUST_LOG=debug cargo run- Bearer token authentication
- Rate limiting protection
- Request timeouts
- CORS protection
- No sensitive data logging
- Clone and install dependencies:
git clone https://github.com/miky-rola/api-gateway
cd rust-api-gateway
cargo build- Run tests:
cargo test- Run with logging:
RUST_LOG=debug cargo run- Fork the repository
- Create your feature branch:
git checkout -b new-feature- Commit your changes:
git commit -m 'Add amazing feature'- Push to the branch:
git push origin new-feature- Open a Pull Request
miky rola - [email protected]
https://github.com/miky-rola/api-gateway
Star us on GitHub — it motivates us to make the gateway even better!