Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add header_up rule support #22

Merged
merged 1 commit into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions internal/caddy/caddy.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ func appendRule(builder *strings.Builder, rule config.Rule, port int) {
builder.WriteString(fmt.Sprintf(" tls {\n %s\n }\n", newTls))
}

// Add header_up directives
for _, header := range rule.HeaderUp {
builder.WriteString(fmt.Sprintf(" header_up %s %s\n", header.Name, header.Value))
}

for _, handle := range rule.Handle {
builder.WriteString(fmt.Sprintf(" handle %s {\n", handle.Path))
for _, directive := range handle.Directives {
Expand Down
8 changes: 7 additions & 1 deletion internal/caddy/caddy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,18 @@ func TestConvertToCaddyfile(t *testing.T) {
To: "http://localhost:{port}",
},
},
HeaderUp: []config.HeaderUp{
{
Name: "X-Real-IP",
Value: "{http.request.remote.host}",
},
},
},
},
}

caddyfile := ConvertToCaddyfile(caddyCfg, 8080)
expectedCaddyfile := "{\n email [email protected]\n on_demand_tls {\n ask https://acme.example.com/directory\n interval 3600\n burst 13\n }\n}\n\nlocalhost {\n tls {\n internal\n }\n handle / {\n root * /usr/share/caddy\n }\n handle /healthz {\n respond \"OK\" 200\n }\n reverse_proxy / http://localhost:8080\n}\n\n"
expectedCaddyfile := "{\n email [email protected]\n on_demand_tls {\n ask https://acme.example.com/directory\n interval 3600\n burst 13\n }\n}\n\nlocalhost {\n tls {\n internal\n }\n header_up X-Real-IP {http.request.remote.host}\n handle / {\n root * /usr/share/caddy\n }\n handle /healthz {\n respond \"OK\" 200\n }\n reverse_proxy / http://localhost:8080\n}\n\n"
assert.Equal(t, expectedCaddyfile, caddyfile)
}

Expand Down
6 changes: 6 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,17 @@ type Handle struct {
Directives []string `yaml:"directives"`
}

type HeaderUp struct {
Name string `yaml:"name"`
Value string `yaml:"value"`
}

type Rule struct {
Match string `yaml:"match"`
Tls string `yaml:"tls"`
ReverseProxy []ReverseProxy `yaml:"reverse_proxy"`
Handle []Handle `yaml:"handle"`
HeaderUp []HeaderUp `yaml:"header_up"`
}

type OnDemandTlsConfig struct {
Expand Down
Loading