mirror of
https://github.com/prymitive/karma
synced 2026-05-05 03:16:51 +00:00
Merge pull request #174 from prymitive/sylr-patch-1
fix(api): proxy when upstream url has a path
This commit is contained in:
8
proxy.go
8
proxy.go
@@ -5,10 +5,11 @@ import (
|
||||
"net/http"
|
||||
"net/http/httputil"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/prymitive/karma/internal/alertmanager"
|
||||
"github.com/prymitive/karma/internal/config"
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
@@ -46,6 +47,11 @@ func NewAlertmanagerProxy(alertmanager *alertmanager.Alertmanager) (*httputil.Re
|
||||
// set hostname of proxied target
|
||||
req.Host = upstreamURL.Host
|
||||
|
||||
// Prepend with upstream URL path if exists
|
||||
if len(upstreamURL.Path) > 0 {
|
||||
req.URL.Path = strings.TrimSuffix(upstreamURL.Path, "/") + req.URL.Path
|
||||
}
|
||||
|
||||
log.Debugf("[%s] Proxy request for %s", alertmanager.Name, req.URL.Path)
|
||||
},
|
||||
Transport: alertmanager.HTTPTransport,
|
||||
|
||||
@@ -224,3 +224,34 @@ func TestProxyHeaders(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestProxyToSubURIAlertmanager(t *testing.T) {
|
||||
httpmock.Activate()
|
||||
defer httpmock.DeactivateAndReset()
|
||||
|
||||
r := ginTestEngine()
|
||||
am, err := alertmanager.NewAlertmanager(
|
||||
"suburi",
|
||||
"http://alertmanager.example.com/suburi",
|
||||
alertmanager.WithRequestTimeout(time.Second*5),
|
||||
alertmanager.WithProxy(true),
|
||||
)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
err = setupRouterProxyHandlers(r, am)
|
||||
if err != nil {
|
||||
t.Errorf("Failed to setup proxy for Alertmanager %s: %s", am.Name, err)
|
||||
}
|
||||
|
||||
httpmock.RegisterResponder("POST", "http://alertmanager.example.com/suburi/api/v1/silences", func(req *http.Request) (*http.Response, error) {
|
||||
return httpmock.NewStringResponse(200, "ok"), nil
|
||||
})
|
||||
|
||||
req := httptest.NewRequest("POST", "/proxy/alertmanager/suburi/api/v1/silences", nil)
|
||||
resp := newCloseNotifyingRecorder()
|
||||
r.ServeHTTP(resp, req)
|
||||
if resp.Code != 200 {
|
||||
t.Errorf("Got response code %d instead of 200", resp.Code)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user