Skip to content

Commit

Permalink
feat: add header_up support (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
scmmishra authored Aug 6, 2024
1 parent cf07378 commit bf4093c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
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

0 comments on commit bf4093c

Please sign in to comment.