feat: add GoongService for authenticated proxying and caching of Goong API requests
All checks were successful
Build and Release / release (push) Successful in 1m31s
All checks were successful
Build and Release / release (push) Successful in 1m31s
This commit is contained in:
@@ -9,6 +9,7 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@@ -101,6 +102,27 @@ func (s *goongService) ProxyRequest(ctx context.Context, method string, targetUR
|
|||||||
respHeaders[k] = v[0]
|
respHeaders[k] = v[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
contentType := resp.Header.Get("Content-Type")
|
||||||
|
isText := strings.Contains(contentType, "json") ||
|
||||||
|
strings.Contains(contentType, "text") ||
|
||||||
|
strings.Contains(contentType, "javascript") ||
|
||||||
|
strings.Contains(contentType, "xml")
|
||||||
|
|
||||||
|
if isText && len(respBody) > 0 {
|
||||||
|
pattern := fmt.Sprintf(`(?i)([?&])api_key=%s(&?)`, regexp.QuoteMeta(apiKey))
|
||||||
|
if re, err := regexp.Compile(pattern); err == nil {
|
||||||
|
respBodyStr := string(respBody)
|
||||||
|
cleanedStr := re.ReplaceAllStringFunc(respBodyStr, func(match string) string {
|
||||||
|
groups := re.FindStringSubmatch(match)
|
||||||
|
if len(groups) > 2 && groups[2] == "&" {
|
||||||
|
return groups[1]
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
})
|
||||||
|
respBody = []byte(cleanedStr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if method == http.MethodGet && resp.StatusCode == http.StatusOK {
|
if method == http.MethodGet && resp.StatusCode == http.StatusOK {
|
||||||
entry := CacheEntry{
|
entry := CacheEntry{
|
||||||
StatusCode: resp.StatusCode,
|
StatusCode: resp.StatusCode,
|
||||||
|
|||||||
Reference in New Issue
Block a user