foxy-io 0.3.10

A configuration-driven and hyper-extensible HTTP proxy library
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
# Foxy 🦊

[![CI-Crate](https://img.shields.io/github/actions/workflow/status/johan-steffens/foxy/crate.yml?label=crate-build)](https://github.com/johan-steffens/foxy/actions/workflows/crate.yml)
[![CI-Docker](https://img.shields.io/github/actions/workflow/status/johan-steffens/foxy/docker.yml?label=docker-build)](https://github.com/johan-steffens/foxy/actions/workflows/docker.yml)
[![codecov](https://codecov.io/github/johan-steffens/foxy/graph/badge.svg?token=3L37Q54F17)](https://codecov.io/github/johan-steffens/foxy)
[![Crates.io Version](https://img.shields.io/crates/v/foxy-io)](https://crates.io/crates/foxy-io)
[![Crates.io Downloads](https://img.shields.io/crates/d/foxy-io)](https://crates.io/crates/foxy-io)
[![Rust Version](https://img.shields.io/badge/rust-1.70%2B-blue)](https://blog.rust-lang.org/2023/06/01/Rust-1.70.0.html)
[![Docker Version](https://img.shields.io/docker/v/johansteffens/foxy?label=docker%20tag)](https://hub.docker.com/r/johansteffens/foxy)
[![Docker Pulls](https://img.shields.io/docker/pulls/johansteffens/foxy)](https://hub.docker.com/r/johansteffens/foxy)
[![License](https://img.shields.io/github/license/johan-steffens/foxy)](https://github.com/johan-steffens/foxy/blob/main/LICENSE.md)
[![Ask DeepWiki](https://deepwiki.com/badge.svg)](https://deepwiki.com/johan-steffens/foxy)

A minimal, configuration-driven, hyper-extensible Rust HTTP proxy library.

## Use Cases

**Foxy is ideal for:**

- **API Gateway**: Centralize routing, security, and observability for your microservices.
- **Edge Proxy**: Secure and control traffic at the edge of your network.
- **Backend for Frontend (BFF)**: Tailor API responses for specific client applications.
- **Protocol Translation**: Transform requests and responses between different protocols.
- **Load Balancing**: Distribute incoming traffic across multiple upstream services.

## Features

- 🛣️ **Powerful Routing**: Predicate-based routing with path patterns, HTTP methods, headers, and query matching
- 🔄 **Flexible Filters**: Pre- and post-processing filters for request/response modification
- ⚙️ **Configuration Superpowers**: Layered configuration from files and environment variables
- 🌐 **Fine-grained Control**: Route-specific filter chains for precise request handling
- 🔒 **Pluggable Security**: Configurable authentication with built-in OIDC support
- 📊 **Observability**: OpenTelemetry integration for distributed tracing
- 📝 **Structured Logging**: JSON logging with trace IDs for better observability
- 🚀 **Modern Async Architecture**: Built on Tokio and Hyper for high performance
- 📦 **Lightweight Dependencies**: Minimal external dependencies for core functionality
- 🧩 **Highly Extensible**: Custom predicates, filters, and security providers via simple traits
- 🚢 **Docker Support**: Official container image for rapid deployment

## Quickstart

### As a Library

Add Foxy to your `Cargo.toml`:

```toml
[dependencies]
foxy-io = "..."
```

Build an instance and start the server:

```rust
use foxy::Foxy;

// Create a new Foxy instance with layered configuration
let foxy = Foxy::loader()
    .with_env_vars()                  // Environment variables (highest priority)
    .with_config_file("config.toml")  // File-based config (medium priority)
    .with_config_file("defaults.toml") // Defaults (lowest priority)
    .build().await?;

// Start the proxy server and wait for it to complete
foxy.start().await?;
```

### Run the Example

```bash
git clone https://github.com/johan-steffens/foxy.git
cd foxy
export RUST_LOG=debug
export FOXY_CONFIG_FILE=$(pwd)/config/example.json
cargo run --bin foxy
```

### Building from Source

Foxy uses platform-specific TLS backends to optimize for different build environments and avoid dependency issues.

#### Prerequisites
- Rust 1.70+
- Platform-specific TLS dependencies handled automatically

#### Build
```bash
git clone https://github.com/johan-steffens/foxy.git
cd foxy
cargo build --release
```

#### TLS Backend Strategy
- **Windows**: Uses `rustls-tls` (pure Rust) to avoid OpenSSL build issues completely
- **Unix/Linux**: Uses `native-tls` with **vendored OpenSSL** for Docker builds
- **Docker**: Vendored SSL eliminates build-time dependency issues - no need to install OpenSSL at build time
- **Cross-compilation**: Platform detection ensures correct TLS backend automatically

### Run with Docker

> **Prerequisites:** Docker 20.10+ installed  

Pull the multi-arch image:

```bash
docker pull johansteffens/foxy:latest
```

Run the proxy, exposing port **8080**:

```bash
docker run --rm -p 8080:8080 johansteffens/foxy:latest
```

#### Using a Custom Configuration

1. Create a `config.json` file on your host
2. Ensure your configuration binds to address `0.0.0.0`
3. Mount it into the container:

```bash
docker run --rm -p 8080:8080 \
  -v "$(pwd)/config.json:/app/config.json:ro" \
  -e FOXY_CONFIG_FILE=/app/config.json \
  johansteffens/foxy:latest 
```

### Run with Docker Compose

Create a `docker-compose.yml` file:

```yaml
version: "3.9"
services:
  foxy:
    image: johansteffens/foxy:latest
    container_name: foxy
    ports:
      - "8080:8080"
    environment:
      FOXY_CONFIG_FILE: /config/config.json
    volumes:
      - ./config.json:/config/config.json:ro
```

Start the service:

```bash
docker compose up -d
```

> **Tip:** When you update `config.json`, restart with `docker compose restart foxy` to apply changes.

## Core Concepts

### Routing System

Foxy uses a predicate-based routing system to determine how requests are handled:

- **Predicates**: Conditions that match against request properties (path, method, headers, query)
- **Priority**: Routes with higher priority are evaluated first
- **Filters**: Processing steps applied to matched routes

### Configuration

Foxy's configuration can be provided through multiple sources:

```rust
// Build a layered configuration
let foxy = Foxy::loader()
    .with_env_vars()                   // First priority
    .with_config_file("config.json")   // Second priority
    .build().await?;
```

Example configuration:

```json
{
  "routes": [
    {
      "id": "api-route",
      "target": "https://api.example.com",
      "filters": [
        {
          "type": "path_rewrite",
          "config": {
            "pattern": "^/api/(.*)$",
            "replacement": "/v2/$1"
          }
        }
      ],
      "predicates": [
        {
          "type_": "path",
          "config": {
            "pattern": "/api/*"
          }
        }
      ]
    }
  ]
}
```

For detailed configuration options, see the [Configuration Guide](docs/CONFIGURATION.md).

### Security

Add JWT validation with the OIDC security provider:

```json
{
  "proxy": {
    "security_chain": [
      {
        "type": "oidc",
        "config": {
          "issuer-uri": "https://id.example.com",
          "jwks-uri": "https://id.example.com/.well-known/jwks.json",
          "aud": "my-api",
          "bypass": [
            { "methods": ["GET"], "path": "/health" }
          ]
        }
      }
    ]
  }
}
```

This configuration validates all requests against the identity provider, while allowing public access to `/health`.

### Structured Logging

Foxy supports structured JSON logging for better observability in production environments:
        
```json
{
  "proxy": {
    "logging": {
      "structured": true,
      "format": "json",
      "include_trace_id": true,
      "static_fields": {
        "environment": "production",
        "service": "api-gateway"
      }
    }
  }
}
```

Key benefits:
- **Trace IDs**: Every request gets a unique ID for end-to-end tracking
- **JSON Format**: Machine-parseable logs for integration with log aggregation systems
- **Rich Context**: Detailed request information and timing metrics
- **Static Fields**: Add environment-specific fields to all logs

For detailed configuration options, see the [Configuration Guide](docs/CONFIGURATION.md#structured-logging).

## Features

### OpenTelemetry Feature

Enable distributed tracing with OpenTelemetry:

```toml
# In your Cargo.toml
[dependencies]
foxy-io = { version = "...", features = ["opentelemetry"] }
```

Configure the OpenTelemetry collector in your configuration:

```json
{
  "proxy": {
    "opentelemetry": {
      "endpoint": "http://otel-collector:4317",
      "service_name": "my-proxy-service",
      "include_headers": true,
      "resource_attributes": {
        "host.name": "proxy-pod-abc123"
      },
      "collector_headers": {
        "X-API-Key": "d41000b6-6191-47c5-99f1-7b88b1b97409"
      }
    }
  }
}
```

### SwaggerUI Feature

Enable the Swagger UI feature:

```toml
# In your Cargo.toml
[dependencies]
foxy-io = { version = "...", features = ["swagger-ui"] }
```

Configure the OpenAPI schemas to be served on the Swagger UI in your configuration:

```json
{
  "proxy": {
    "swagger_ui": {
      "enabled": true,
      "path": "/swagger-ui",
      "sources": [
        {
          "name": "Petstore",
          "url": "https://petstore.swagger.io/v2/swagger.json"
        }
      ]
    }
  }
}
```

### Vault Config Feature

The `vault-config` feature enables secret interpolation from the filesystem into configuration values. This allows you to store sensitive information like passwords, API keys, and tokens in separate files and reference them in your configuration.

## Enabling the Feature

Add the `vault-config` feature to your `Cargo.toml`:

```toml
[dependencies]
foxy-io = { version = "0.3.6", features = ["vault-config"] }
```

Or enable it when building:

```bash
cargo build --features vault-config
```

### Usage

#### Basic Setup

1. Create a vault directory structure (default: `/vault/secret/`)
2. Store your secrets as individual files in the vault directory
3. Reference secrets in your configuration using `${secret.name}` syntax
4. Wrap your configuration provider with `VaultConfigProvider`

#### Example

**Vault directory structure:**
```
/vault/secret/
├── redis_password
```

**Secret files content:**
```bash
# /vault/secret/redis_password
super_secret_redis_password
```

**Configuration file (config.json):**
```json
{
  "server": {
    "listen": "0.0.0.0:8080",
    "redis_url": "redis://localhost:6379",
    "redis_password": "${secret.redis_password}"
  }
}
```

#### Example Application

See `examples/vault_example.rs` for a complete working example:

```bash
cargo run --example vault_example --features vault-config
```

This example demonstrates:
- Creating a temporary vault directory
- Setting up secret files
- Configuring the vault provider
- Accessing interpolated configuration values


## Extension Points

Foxy is designed to be highly extensible. You can inject your own custom logic into the proxy pipeline by implementing a few simple traits. This allows you to add custom routing rules, request/response modifications, and authentication mechanisms without forking the project.

The primary extension points are:
- **`Filter`**: Modify requests and responses.
- **`Predicate`**: Implement custom routing logic.
- **`SecurityProvider`**: Add custom authentication and authorization.

All extension points follow a similar pattern:
1.  **Implement** the corresponding trait.
2.  **Register** your implementation with Foxy's global registry at startup.
3.  **Use** your custom component in the configuration file.

For a detailed guide on adding extension points, see the [Extension Guide](docs/EXTENSION.md).

## License

This project is licensed under the [Mozilla Public License Version 2.0](LICENSE.md).

## Contributions

We welcome and appreciate contributions to Foxy! Please see our [Contribution Guide](docs/CONTRIBUTING.md) for details on how to get involved, including our development workflow, code style, and testing procedures.

### Contributors

A big thank you to all the individuals who have contributed to Foxy!

[Johan Steffens](https://github.com/johan-steffens)
[Armand Eicker](https://github.com/Armand-Eicker-Dev)
[Ohan Smit](https://github.com/Psynosaur)