Skip to content

Commit 0d90ead

Browse files
committed
feat: Update README and add YAML config support with header modifications
1 parent 6765d46 commit 0d90ead

File tree

5 files changed

+403
-71
lines changed

5 files changed

+403
-71
lines changed

README.md

Lines changed: 121 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@ A lightweight (1 file), dynamic reverse proxy server with live configuration rel
1313

1414
## Features
1515

16-
- Live configuration reloading
17-
- Support for both local and remote proxy targets
18-
- Configurable logging options
19-
- HTTP/HTTPS support
20-
- Path-based routing
21-
- Automatic configuration file watching
22-
- Verbose logging options for debugging
16+
- Live configuration reloading with file watching
17+
- Support for HTTP/HTTPS proxy targets
18+
- Static file and directory serving
19+
- Request/Response header modification
20+
- Configurable timeouts and retries
21+
- Comprehensive logging options
22+
- Graceful shutdown support
23+
- Health check endpoint
24+
- Environment variable configuration
2325

2426
## Installation
2527

@@ -35,51 +37,139 @@ cd lightymux
3537
go build
3638
```
3739

38-
## Usage
40+
## Configuration
3941

40-
Basic usage:
42+
### Environment Variables
4143

42-
```bash
43-
./lightymux [flags] <config_file>
44+
- `HTTP_ADDR`: HTTP listen address (default: "")
45+
- `READ_TIMEOUT`: Read timeout duration (default: 30s)
46+
- `WRITE_TIMEOUT`: Write timeout duration (default: 30s)
47+
- `IDLE_TIMEOUT`: Idle timeout duration (default: 60s)
48+
- `PROXY_TIMEOUT`: Proxy timeout duration (default: 60s)
49+
- `VERBOSE`: Enable verbose logging (default: false)
50+
- `LOG_REQUESTS`: Log incoming requests (default: false)
51+
- `LOG_RESPONSES`: Log outgoing responses (default: false)
52+
- `LOG_ERRORS`: Log proxy errors (default: true)
53+
- `LOG_FILE`: Log to file instead of stderr
54+
- `HEALTH_CHECK`: Health check endpoint path (default: "/health")
55+
- `RETRY_ATTEMPTS`: Number of retry attempts (default: 3)
56+
57+
### Configuration File Format
58+
59+
The configuration file uses YAML format. Each route is defined by its path and target configuration:
60+
61+
```yaml
62+
/api:
63+
target: http://api.example.com
64+
rules:
65+
- request:
66+
headers:
67+
X-API-Key: secret-key
68+
- response:
69+
headers:
70+
Access-Control-Allow-Origin: "*"
71+
72+
/static:
73+
target: /var/www/static
74+
75+
/files:
76+
target: /path/to/files
4477
```
4578
46-
### Command Line Flags
79+
Routes can be configured for:
80+
- Remote HTTP/HTTPS endpoints
81+
- Local directories (static file serving)
82+
- Single files
83+
84+
#### Header Modification
85+
86+
You can modify request and response headers for each route using three operations:
87+
88+
- `header-add`: Add values to existing headers (supports multiple values)
89+
- `header-set`: Set headers to specific values, replacing any existing ones
90+
- `header-del`: Remove headers completely
91+
92+
Example:
93+
94+
```yaml
95+
/api:
96+
target: http://api.example.com
97+
rules:
98+
- request:
99+
headers:
100+
header-add:
101+
Accept: ["application/json", "text/plain"]
102+
X-Custom: ["value1", "value2"]
103+
header-set:
104+
Authorization: "Bearer token123"
105+
Content-Type: "application/json"
106+
header-del:
107+
- "X-Old-Header"
108+
- "X-Deprecated"
109+
- response:
110+
headers:
111+
header-add:
112+
Access-Control-Allow-Methods: ["GET", "POST", "OPTIONS"]
113+
header-set:
114+
Access-Control-Allow-Origin: "*"
115+
Cache-Control: "max-age=3600"
116+
header-del:
117+
- "X-Internal-Header"
118+
```
47119

48-
- `-http`: HTTP listen address (e.g., ":8080")
49-
- `-verbose`: Enable verbose logging
50-
- `-log-requests`: Log incoming requests
51-
- `-log-responses`: Log outgoing responses
52-
- `-log-errors`: Log proxy errors (default: true)
53-
- `-log-file`: Log to file instead of stderr
120+
In this example:
121+
- Request modifications:
122+
- Adds multiple values to `Accept` and `X-Custom` headers
123+
- Sets `Authorization` and `Content-Type` headers to specific values
124+
- Removes `X-Old-Header` and `X-Deprecated` headers
125+
- Response modifications:
126+
- Adds multiple CORS methods
127+
- Sets CORS origin and caching headers
128+
- Removes an internal header
54129

55-
### Configuration File Format
130+
## Usage
56131

57-
The configuration file uses a simple space-separated format where each line contains a path and target:
132+
Basic usage:
58133

134+
```bash
135+
lightymux [config_file]
59136
```
60-
/path target_url_or_path
61-
/api http://api.example.com
62-
/static /var/www/static
137+
138+
Example with environment variables:
139+
140+
```bash
141+
HTTP_ADDR=:8080 VERBOSE=true LOG_REQUESTS=true lightymux config.yaml
63142
```
64143

65144
The configuration file is watched for changes and automatically reloaded when modified.
66145

67146
## Example
68147

69-
1. Create a configuration file `proxy.conf`:
70-
```
71-
/api http://localhost:3000
72-
/static http://localhost:8080
148+
1. Create a configuration file `config.yaml`:
149+
```yaml
150+
/api:
151+
target: http://localhost:3000
152+
rules:
153+
- response:
154+
headers:
155+
Access-Control-Allow-Origin: "*"
156+
157+
/static:
158+
target: /var/www/static
159+
160+
/docs:
161+
target: http://localhost:8080
73162
```
74163

75164
2. Start the proxy:
76165
```bash
77-
./lightymux -http :8000 proxy.conf
166+
lightymux config.yaml
78167
```
79168

80-
3. The proxy will now forward requests:
81-
- `/api/*``http://localhost:3000/*`
82-
- `/static/*``http://localhost:8080/*`
169+
3. The proxy will now:
170+
- Forward `/api/*` to `http://localhost:3000/*` with CORS headers
171+
- Serve static files from `/var/www/static` at `/static/*`
172+
- Forward `/docs/*` to `http://localhost:8080/*`
83173

84174
## License
85175

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ require github.com/fsnotify/fsnotify v1.8.0
77
require (
88
github.com/caarlos0/env/v11 v11.3.1
99
golang.org/x/sys v0.13.0 // indirect
10+
gopkg.in/yaml.v3 v3.0.1
1011
)

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,7 @@ github.com/fsnotify/fsnotify v1.8.0 h1:dAwr6QBTBZIkG8roQaJjGof0pp0EeF+tNV7YBP3F/
44
github.com/fsnotify/fsnotify v1.8.0/go.mod h1:8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8TRcHyHii0=
55
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
66
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
7+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
8+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
9+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
10+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 commit comments

Comments
 (0)