Skip to content

Commit

Permalink
Add implementation of testRedirectRule
Browse files Browse the repository at this point in the history
  • Loading branch information
vkuznet committed Nov 3, 2024
1 parent 1f1b182 commit 38e23fa
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions redirect.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"net/url"
"os"
"path/filepath"
"sort"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -76,6 +75,11 @@ func reverseProxy(targetURL string, w http.ResponseWriter, r *http.Request) {
TLSHandshakeTimeout: time.Duration(Config.TLSHandshakeTimeout) * time.Second,
}
}
// set CORPS headers if server is configured with them
if Config.Corps {
w.Header().Set("Access-Control-Allow-Origin", "*") // Allow all origins
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, OPTIONS")
}

// set custom transport to capture size of response body
// proxy.Transport = &transport{http.DefaultTransport}
Expand Down Expand Up @@ -258,7 +262,6 @@ func readIngressRules() (map[string]Ingress, []string) {
rmap, rules = RedirectRules(Config.Ingress)
}
if Config.Verbose > 0 {
sort.Strings(rules)
var maxLen int
for _, r := range rules {
if len(r) > maxLen {
Expand Down Expand Up @@ -288,6 +291,31 @@ func redirectRule(r Ingress, maxLen int) string {
return out
}

// helper function to test matching of redirect rule
// logic should match func redirect
func testRedirectRule(config, rule string) {
err := parseConfig(config)
if err != nil {
log.Fatal(err)
}
fmt.Println("find new rule for path", rule)
_ingressMap, _ingressRules = readIngressRules()
for _, key := range _ingressRules {
rec := _ingressMap[key]
// check that request URL path had ingress path with slash
if PathMatched(rule, rec.Path, rec.Strict) {
fmt.Printf("\n--- new match found: rule=%s rec=%+v key=%s\n", rule, rec, key)
fmt.Printf("HTTP r.URL.Path=%s redirected to %s%s\n", rule, rec.ServiceURL, rec.NewPath)
url := srvURL(rec.ServiceURL)
if rec.OldPath != "" {
// replace old path to new one, e.g. /couchdb/_all_dbs => /_all_dbs
rule = strings.Replace(rule, rec.OldPath, rec.NewPath, 1)
fmt.Printf("service url %s, new request path %s\n", url, rule)
}
}
}
}

// helper function to redirect HTTP requests based on configuration ingress rules
func redirect(w http.ResponseWriter, r *http.Request) {
for _, key := range _ingressRules {
Expand Down

0 comments on commit 38e23fa

Please sign in to comment.