+39
-0
lines changedFilter options
+39
-0
lines changed Original file line number Diff line number Diff line change
@@ -101,6 +101,10 @@ func GrafanaAPIKeyFromContext(ctx context.Context) string {
101
101
102
102
type grafanaClientKey struct{}
103
103
104
+
func makeBasePath(path string) string {
105
+
return strings.Join([]string{strings.TrimRight(path, "/"), "api"}, "/")
106
+
}
107
+
104
108
// ExtractGrafanaClientFromEnv is a StdioContextFunc that extracts Grafana configuration
105
109
// from environment variables and injects a configured client into the context.
106
110
var ExtractGrafanaClientFromEnv server.StdioContextFunc = func(ctx context.Context) context.Context {
@@ -116,6 +120,7 @@ var ExtractGrafanaClientFromEnv server.StdioContextFunc = func(ctx context.Conte
116
120
panic(fmt.Errorf("invalid %s: %w", grafanaURLEnvVar, err))
117
121
}
118
122
cfg.Host = parsedURL.Host
123
+
cfg.BasePath = makeBasePath(parsedURL.Path)
119
124
// The Grafana client will always prefer HTTPS even if the URL is HTTP,
120
125
// so we need to limit the schemes to HTTP if the URL is HTTP.
121
126
if parsedURL.Scheme == "http" {
@@ -144,6 +149,7 @@ var ExtractGrafanaClientFromHeaders server.SSEContextFunc = func(ctx context.Con
144
149
if u != "" {
145
150
if url, err := url.Parse(u); err == nil {
146
151
cfg.Host = url.Host
152
+
cfg.BasePath = makeBasePath(url.Path)
147
153
if url.Scheme == "http" {
148
154
cfg.Schemes = []string{"http"}
149
155
}
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@ import (
8
8
"net/http"
9
9
"testing"
10
10
11
+
"github.com/go-openapi/runtime/client"
11
12
"github.com/stretchr/testify/assert"
12
13
"github.com/stretchr/testify/require"
13
14
)
@@ -73,3 +74,35 @@ func TestExtractGrafanaInfoFromHeaders(t *testing.T) {
73
74
assert.Equal(t, "my-test-api-key", apiKey)
74
75
})
75
76
}
77
+
78
+
func TestExtractGrafanaClientPath(t *testing.T) {
79
+
t.Run("no custom path", func(t *testing.T) {
80
+
t.Setenv("GRAFANA_URL", "http://my-test-url.grafana.com/")
81
+
ctx := ExtractGrafanaClientFromEnv(context.Background())
82
+
83
+
c := GrafanaClientFromContext(ctx)
84
+
require.NotNil(t, c)
85
+
rt := c.Transport.(*client.Runtime)
86
+
assert.Equal(t, "/api", rt.BasePath)
87
+
})
88
+
89
+
t.Run("custom path", func(t *testing.T) {
90
+
t.Setenv("GRAFANA_URL", "http://my-test-url.grafana.com/grafana")
91
+
ctx := ExtractGrafanaClientFromEnv(context.Background())
92
+
93
+
c := GrafanaClientFromContext(ctx)
94
+
require.NotNil(t, c)
95
+
rt := c.Transport.(*client.Runtime)
96
+
assert.Equal(t, "/grafana/api", rt.BasePath)
97
+
})
98
+
99
+
t.Run("custom path, trailing slash", func(t *testing.T) {
100
+
t.Setenv("GRAFANA_URL", "http://my-test-url.grafana.com/grafana/")
101
+
ctx := ExtractGrafanaClientFromEnv(context.Background())
102
+
103
+
c := GrafanaClientFromContext(ctx)
104
+
require.NotNil(t, c)
105
+
rt := c.Transport.(*client.Runtime)
106
+
assert.Equal(t, "/grafana/api", rt.BasePath)
107
+
})
108
+
}
You can’t perform that action at this time.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4