Memo is a lightweight, scalable API Gateway built using Rust and the Actix Web framework. It is designed to route incoming HTTP requests to the appropriate backend services, enforce configuration rules, and handle errors gracefully.
- Dynamic Routing: Routes requests based on external paths defined in a YAML configuration file.
- Protocol Support: Handles both HTTP and HTTPS protocols.
- Timeout Management: Ensures upstream requests do not hang.
- Header Forwarding: Forwards all client headers, excluding connection-specific ones.
- Configurable via YAML: Easily specify routes, domains, ports, and allowed methods.
- Error Handling: Detailed error responses for timeout, invalid configurations, and upstream errors.
- Rust: Version 1.65 or higher
- Cargo: For managing dependencies and building the project
- YAML Config File: A
config.yml
file must be present for routing definitions
git clone https://github.com/DanielSarmiento04/memo
cd memo
cargo build --release
cargo run
The API Gateway relies on a config.yml
file for routing definitions. Below is the structure of the configuration file:
version: 1
routes:
- name: localhost
domain: localhost
port: 8080
protocol: http
external_path: /identity/register
internal_path: /api/identity/register
methods:
- POST
- GET
- name: google
domain: google.com
port: 443
protocol: https
external_path: /identity/register/v2
internal_path: /api/identity/register/v2
methods:
- POST
- GET
- name: Descriptive name for the route. Used for easier identification in the configuration file.
- domain: The target domain for forwarding requests. Specifies the upstream service's domain name.
- port: The target port of the backend service. Common values are
80
for HTTP and443
for HTTPS. - protocol: Protocol for forwarding (
http
orhttps
). Determines whether the request is sent securely. - external_path: Path that clients use to access the service. It must begin with
/
and is matched against incoming request paths. - internal_path: Path on the backend server where requests are forwarded. It must also begin with
/
and specifies the upstream service's endpoint. - methods: List of HTTP methods allowed for this route. Examples include
GET
,POST
,PUT
, etc.
Assume the following route in config.yml
:
routes:
- name: localhost
domain: localhost
port: 8080
protocol: http
external_path: /identity/register
internal_path: /api/identity/register
methods:
- POST
- GET
You can make a request to the gateway like this:
curl --location 'http://localhost:8080/identity/register' \
--header 'Content-Type: application/json' \
--data '{"username": "example", "password": "12345"}'
The gateway will forward the request to http://localhost:8080/api/identity/register
.
- main.rs: Contains the main application logic, including server initialization and request routing.
- yaml_config.rs: Handles parsing and validation of the
config.yml
file. - error_handler.rs: Defines custom error types and their corresponding HTTP responses.
- redirect_service.rs: Contains helper functions for formatting and forwarding requests.
Error Type | HTTP Status Code | Description |
---|---|---|
Timeout | 504 Gateway Timeout | Request to the upstream service timed out. |
Internal Server Error | 500 Internal Server Error | An unexpected error occurred in the gateway. |
Config Error | 502 Bad Gateway | Invalid or missing route configuration. |
Upstream Error | 502 Bad Gateway | Error occurred while forwarding the request. |
The application uses env_logger
for structured logging. To enable logging, set the RUST_LOG
environment variable before running the application:
RUST_LOG=info cargo run
Logs include details about received requests, routing decisions, and upstream responses.
Contributions are welcome! Feel free to open an issue or submit a pull request.
- Fork the repository.
- Create a feature branch:
git checkout -b feature-name
- Commit changes:
git commit -m 'Add feature'
- Push to the branch:
git push origin feature-name
- Open a pull request.
This project is licensed under the MIT License. See LICENSE
for details.
-
Route Not Found:
- Ensure
config.yml
is properly formatted and matches the requested path. - Restart the server after modifying the configuration.
- Ensure
-
Timeouts:
- Check upstream service availability.
- Increase the timeout duration in
main.rs
.
-
Headers Not Forwarded:
- Ensure headers are correctly formatted in the request.
For further assistance, contact support at josedanielsarmiento219@gmail.com
.