mirror of
https://github.com/akvorado/akvorado.git
synced 2025-12-12 06:24:10 +01:00
console: remove proxy to Grafana
This is not really our job to do that.
This commit is contained in:
@@ -2,8 +2,6 @@ package console
|
||||
|
||||
// Configuration describes the configuration for the console component.
|
||||
type Configuration struct {
|
||||
// GrafanaURL is the URL to acess Grafana.
|
||||
GrafanaURL string
|
||||
// ServeLiveFS serve files from the filesystem instead of the embedded versions.
|
||||
ServeLiveFS bool
|
||||
}
|
||||
|
||||
@@ -2,21 +2,15 @@
|
||||
package console
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"html/template"
|
||||
"io/fs"
|
||||
"log"
|
||||
netHTTP "net/http"
|
||||
"net/http/httputil"
|
||||
"net/url"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"sync"
|
||||
|
||||
"github.com/rs/zerolog"
|
||||
|
||||
"akvorado/common/http"
|
||||
"akvorado/common/reporter"
|
||||
)
|
||||
@@ -44,28 +38,6 @@ func New(reporter *reporter.Reporter, config Configuration, dependencies Depende
|
||||
config: config,
|
||||
}
|
||||
|
||||
// Grafana proxy
|
||||
if c.config.GrafanaURL != "" {
|
||||
// Provide a proxy for Grafana
|
||||
url, err := url.Parse(config.GrafanaURL)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to parse Grafana URL %q: %w", config.GrafanaURL, err)
|
||||
}
|
||||
proxy := httputil.NewSingleHostReverseProxy(url)
|
||||
proxy.Transport = &netHTTP.Transport{
|
||||
Proxy: nil, // Disable proxy
|
||||
}
|
||||
proxy.ErrorLog = log.New(c.r.With().
|
||||
Str("proxy", "grafana").
|
||||
Str("level", zerolog.LevelWarnValue).
|
||||
Logger(), "", 0)
|
||||
proxyHandler := netHTTP.HandlerFunc(
|
||||
func(w netHTTP.ResponseWriter, r *netHTTP.Request) {
|
||||
proxy.ServeHTTP(w, r)
|
||||
})
|
||||
c.d.HTTP.AddHandler("/grafana/", proxyHandler)
|
||||
}
|
||||
|
||||
c.d.HTTP.AddHandler("/", netHTTP.HandlerFunc(c.assetsHandlerFunc))
|
||||
c.d.HTTP.AddHandler("/api/v0/docs/", netHTTP.HandlerFunc(c.docsHandlerFunc))
|
||||
|
||||
|
||||
@@ -1,51 +0,0 @@
|
||||
package console
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
netHTTP "net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"akvorado/common/helpers"
|
||||
"akvorado/common/http"
|
||||
"akvorado/common/reporter"
|
||||
)
|
||||
|
||||
func TestProxy(t *testing.T) {
|
||||
// Mock HTTP server
|
||||
server := httptest.NewServer(
|
||||
netHTTP.HandlerFunc(
|
||||
func(w netHTTP.ResponseWriter, r *netHTTP.Request) {
|
||||
fmt.Fprintf(w, "hello world!")
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
r := reporter.NewMock(t)
|
||||
h := http.NewMock(t, r)
|
||||
_, err := New(r, Configuration{
|
||||
GrafanaURL: server.URL,
|
||||
}, Dependencies{
|
||||
HTTP: h,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("New() error:\n%+v", err)
|
||||
}
|
||||
|
||||
// Check the proxy works as expected
|
||||
resp, err := netHTTP.Get(fmt.Sprintf("http://%s/grafana/test", h.Address))
|
||||
if err != nil {
|
||||
t.Fatalf("GET /grafana/test:\n%+v", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
t.Fatalf("GET /grafana/test: cannot read body:\n%+v", err)
|
||||
}
|
||||
if resp.StatusCode != 200 {
|
||||
t.Errorf("GET /grafana/test: got status code %d, not 200", resp.StatusCode)
|
||||
}
|
||||
if diff := helpers.Diff(string(body), "hello world!"); diff != "" {
|
||||
t.Errorf("GET /grafana/test (-got, +want):\n%s", diff)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user