Skip to content

Commit 4af2308

Browse files
authored
Merge pull request #1 from HueCodes/updatedcode
Updatedcode
2 parents 489691b + 95a5f5f commit 4af2308

File tree

9 files changed

+1782
-161
lines changed

9 files changed

+1782
-161
lines changed

Cargo.lock

Lines changed: 1098 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[package]
2+
name = "reverse_proxy"
3+
version = "0.2.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
hyper = { version = "1.0", features = ["full"] }
8+
hyper-util = { version = "0.1", features = ["full"] }
9+
http-body-util = "0.1"
10+
tokio = { version = "1", features = ["full"] }
11+
serde = { version = "1.0", features = ["derive"] }
12+
serde_yaml = "0.9"
13+
serde_json = "1.0"
14+
log = "0.4"
15+
env_logger = "0.10"
16+
anyhow = "1.0"
17+
rand = "0.8"
18+
chrono = { version = "0.4", features = ["serde"] }

Dockerfile

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
FROM rust:1.75 AS builder
2+
3+
WORKDIR /app
4+
COPY Cargo.toml Cargo.lock ./
5+
COPY src/ src/
6+
7+
RUN cargo build --release
8+
9+
FROM debian:bookworm-slim
10+
11+
RUN apt-get update && \
12+
apt-get install -y ca-certificates && \
13+
rm -rf /var/lib/apt/lists/*
14+
15+
COPY --from=builder /app/target/release/reverse_proxy /usr/local/bin/
16+
COPY config.yaml /etc/reverse-proxy/config.yaml
17+
18+
WORKDIR /etc/reverse-proxy
19+
EXPOSE 3000
20+
21+
CMD ["reverse_proxy"]

README.md

Lines changed: 136 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -1,158 +1,180 @@
11
# Rust Reverse Proxy
22

3-
[![Rust](https://github.com/HueCodes/rust-reverse-proxy/actions/workflows/rust.yml/badge.svg)](https://github.com/HueCodes/rust-reverse-proxy/actions/workflows/rust.yml)
4-
[![Crates.io](https://img.shields.io/crates/v/rust-reverse-proxy.svg)](https://crates.io/crates/rust-reverse-proxy)
5-
6-
A lightweight and fast HTTP reverse proxy written in Rust. This project leverages the [Warp](https://github.com/seanmonstar/warp) framework for efficient request handling and routing, providing a simple yet powerful solution for proxying HTTP traffic.
3+
A lightweight, high-performance HTTP reverse proxy written in Rust, prioritizing security, memory safety, and concurrency.
74

85
## Features
96

10-
- **High Performance**: Built with Rust's zero-cost abstractions and async runtime for low-latency proxying.
11-
- **Flexible Routing**: Supports multiple upstream backends with easy configuration.
12-
- **TLS Support**: Optional TLS termination and forwarding (planned).
13-
- **Logging and Metrics**: Built-in structured logging and basic metrics (in development).
14-
- **Configurable**: YAML or TOML-based configuration for backends, filters, and middleware.
7+
- **High Performance**: Built with Tokio and Hyper for maximum throughput
8+
- **Load Balancing**: Round-robin load balancing across multiple backend servers
9+
- **Health Checks**: Automatic health monitoring of backend servers
10+
- **Configuration-Driven**: YAML-based configuration for easy management
11+
- **Structured Logging**: Comprehensive request/response logging with configurable levels
12+
- **Fault Tolerance**: Automatic failover and error handling
13+
- **Request Timeouts**: Configurable timeouts for reliability
14+
- **Header Forwarding**: Proper X-Forwarded-* headers for backend services
1515

1616
## Quick Start
1717

18-
### Prerequisites
19-
20-
- Rust 1.70+ (stable)
21-
- Cargo
22-
23-
### Installation
24-
25-
Clone the repository and build with Cargo:
26-
27-
```bash
28-
git clone https://github.com/HueCodes/rust-reverse-proxy.git
29-
cd rust-reverse-proxy
30-
cargo build --release
31-
```
18+
1. **Build the project**:
19+
```bash
20+
cargo build --release
21+
```
22+
23+
2. **Configure your backends** in `config.yaml`:
24+
```yaml
25+
server:
26+
host: "127.0.0.1"
27+
port: 3000
28+
29+
backends:
30+
- url: "http://backend1.example.com"
31+
weight: 1
32+
health_check_path: "/health"
33+
- url: "http://backend2.example.com"
34+
weight: 1
35+
health_check_path: "/health"
36+
```
37+
38+
3. **Run the proxy**:
39+
```bash
40+
cargo run --release
41+
```
3242

33-
### Running the Proxy
43+
## Configuration
3444

35-
Create a basic configuration file `config.yaml`:
45+
The proxy is configured using a `config.yaml` file:
3646

3747
```yaml
38-
backends:
39-
- name: "api"
40-
host: "http://localhost:8080"
41-
path_prefix: "/api"
42-
- name: "web"
43-
host: "http://localhost:3000"
44-
path_prefix: "/"
45-
46-
port: 80
47-
```
48+
server:
49+
host: "127.0.0.1" # Interface to bind to
50+
port: 3000 # Port to listen on
4851

49-
Start the proxy:
50-
51-
```bash
52-
cargo run -- --config config.yaml
52+
backends:
53+
- url: "http://httpbin.org" # Backend server URL
54+
weight: 1 # Load balancing weight
55+
health_check_path: "/status/200" # Health check endpoint
56+
- url: "http://example.com"
57+
weight: 1
58+
health_check_path: "/"
59+
60+
load_balancing:
61+
strategy: "round_robin" # Load balancing strategy
62+
63+
health_checks:
64+
enabled: true # Enable/disable health checks
65+
interval_seconds: 30 # Health check interval
66+
timeout_seconds: 5 # Health check timeout
67+
failure_threshold: 3 # Failures before marking unhealthy
68+
69+
logging:
70+
level: "info" # Log level (debug, info, warn, error)
71+
format: "json" # Log format (json, text)
72+
73+
timeouts:
74+
request_timeout_seconds: 30 # Backend request timeout
75+
connect_timeout_seconds: 10 # Connection timeout
5376
```
5477
55-
Or with the release binary:
78+
## Architecture
5679
57-
```bash
58-
./target/release/rust-reverse-proxy --config config.yaml
59-
```
80+
The proxy consists of several key components:
6081
61-
The proxy will listen on port 80 and forward requests to the configured backends based on path prefixes.
82+
- **ProxyService**: Main request handling service
83+
- **LoadBalancer**: Manages backend selection and health status
84+
- **HealthChecker**: Monitors backend server health
85+
- **Configuration**: YAML-based configuration management
6286
63-
## Configuration
87+
## Load Balancing
6488
65-
The proxy uses a YAML configuration file. Key sections:
89+
Currently supports round-robin load balancing:
90+
- Requests are distributed evenly across healthy backends
91+
- Unhealthy backends are automatically excluded
92+
- Backends are marked unhealthy after consecutive failures
6693
67-
- `backends`: List of upstream services with `name`, `host`, `path_prefix`, and optional `strip_prefix`.
68-
- `port`: Listening port (default: 80).
69-
- `log_level`: Logging verbosity (e.g., "info", "debug").
70-
- `tls`: TLS configuration (cert and key paths, enabled by default if provided).
94+
## Health Checks
7195
72-
For full options, see `config.yaml.example` (to be added).
96+
- Periodic health checks to all configured backends
97+
- Configurable health check paths and intervals
98+
- Automatic failover when backends become unhealthy
99+
- Recovery detection when backends come back online
73100
74-
## Usage Examples
101+
## Logging
75102
76-
### Basic Proxying
103+
Structured logging with configurable levels:
104+
- Request/response logging with timing information
105+
- Health check status updates
106+
- Error tracking and debugging information
107+
- JSON or plain text output formats
77108
78-
Proxy all `/api/*` requests to `http://backend:8080`:
109+
## Error Handling
79110
80-
```yaml
81-
backends:
82-
- name: "backend"
83-
host: "http://backend:8080"
84-
path_prefix: "/api"
85-
```
111+
Robust error handling with appropriate HTTP status codes:
112+
- `503 Service Unavailable`: No healthy backends
113+
- `502 Bad Gateway`: Backend request failures
114+
- `504 Gateway Timeout`: Backend request timeouts
115+
- `400 Bad Request`: Client request issues
86116

87-
### Load Balancing
117+
## Performance
88118

89-
Support for round-robin load balancing across multiple hosts (planned feature):
90-
91-
```yaml
92-
backends:
93-
- name: "api-cluster"
94-
hosts:
95-
- "http://backend1:8080"
96-
- "http://backend2:8080"
97-
path_prefix: "/api"
98-
strategy: "round_robin"
99-
```
100-
101-
### Middleware
102-
103-
Add rate limiting or authentication middleware via configuration (in development).
119+
Optimized for high performance:
120+
- Async/await with Tokio runtime
121+
- Connection pooling with Hyper client
122+
- Zero-copy request/response forwarding where possible
123+
- Minimal memory allocations
104124

105125
## Development
106126

107-
This project is actively under development. Current focus:
108-
109-
- Implementing core proxy logic with Warp.
110-
- Adding configuration parsing.
111-
- TLS integration with Rustls.
112-
113-
### Building and Testing
127+
### Building
128+
```bash
129+
cargo build
130+
```
114131

132+
### Testing
115133
```bash
116-
# Run tests
117134
cargo test
118-
119-
# Run with clippy
120-
cargo clippy -- -D warnings
121-
122-
# Format code
123-
cargo fmt
124135
```
125136

126-
### Dependencies
127-
128-
See `Cargo.toml` for details. Key crates:
137+
### Running with Debug Logging
138+
```bash
139+
RUST_LOG=debug cargo run
140+
```
129141

130-
- `warp`: Web server framework.
131-
- `tokio`: Async runtime.
132-
- `serde`: Configuration serialization.
133-
- `tracing`: Logging.
142+
## Docker Support
143+
144+
Create a `Dockerfile`:
145+
```dockerfile
146+
FROM rust:1.70 AS builder
147+
WORKDIR /app
148+
COPY . .
149+
RUN cargo build --release
150+
151+
FROM debian:bookworm-slim
152+
RUN apt-get update && apt-get install -y ca-certificates && rm -rf /var/lib/apt/lists/*
153+
COPY --from=builder /app/target/release/reverse_proxy /usr/local/bin/
154+
COPY config.yaml /etc/reverse-proxy/
155+
WORKDIR /etc/reverse-proxy
156+
EXPOSE 3000
157+
CMD ["reverse_proxy"]
158+
```
134159

135160
## Contributing
136161

137-
Contributions are welcome! Please:
138-
139-
1. Fork the repository.
140-
2. Create a feature branch (`git checkout -b feature/my-feature`).
141-
3. Commit changes (`git commit -am 'Add my feature'`).
142-
4. Push to the branch (`git push origin feature/my-feature`).
143-
5. Open a Pull Request.
144-
145-
See [CONTRIBUTING.md](CONTRIBUTING.md) for more details (to be created).
162+
1. Fork the repository
163+
2. Create a feature branch
164+
3. Make your changes
165+
4. Add tests if applicable
166+
5. Submit a pull request
146167

147168
## License
148169

149-
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
150-
151-
## Acknowledgments
152-
153-
- Inspired by various Rust web projects and the need for a simple, performant proxy.
154-
- Thanks to the Warp and Tokio teams for excellent crates.
170+
This project is licensed under the MIT License - see the LICENSE file for details.
155171

156-
---
172+
## Roadmap
157173

158-
*Project started in 2025. More features coming soon!*
174+
- [ ] HTTPS/TLS termination support
175+
- [ ] WebSocket proxying
176+
- [ ] Metrics and monitoring endpoints
177+
- [ ] Rate limiting
178+
- [ ] Path-based routing
179+
- [ ] Circuit breaker pattern
180+
- [ ] Admin API for runtime configuration

cargo.tml/cargo.tml

Lines changed: 0 additions & 8 deletions
This file was deleted.

config.example.yaml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Example configuration for development
2+
server:
3+
host: "127.0.0.1"
4+
port: 8080
5+
6+
backends:
7+
- url: "http://httpbin.org"
8+
weight: 2
9+
health_check_path: "/status/200"
10+
- url: "http://jsonplaceholder.typicode.com"
11+
weight: 1
12+
health_check_path: "/posts/1"
13+
14+
load_balancing:
15+
strategy: "round_robin"
16+
17+
health_checks:
18+
enabled: true
19+
interval_seconds: 15
20+
timeout_seconds: 3
21+
failure_threshold: 2
22+
23+
logging:
24+
level: "debug"
25+
format: "text"
26+
27+
timeouts:
28+
request_timeout_seconds: 20
29+
connect_timeout_seconds: 5

0 commit comments

Comments
 (0)