Introduce WebReporter to hold web-related options

This commit is contained in:
Roland Schilter
2017-08-11 14:19:16 +01:00
parent f8fa524617
commit 1f27310f62
5 changed files with 28 additions and 15 deletions

View File

@@ -548,17 +548,17 @@ func (r *Registry) RendererForTopology(topologyID string, values url.Values, rpt
return topology.renderer, nil, nil
}
type reporterHandler func(context.Context, Reporter, string, http.ResponseWriter, *http.Request)
type reporterHandler func(context.Context, Reporter, http.ResponseWriter, *http.Request)
func captureReporter(rep Reporter, metricsGraphURL string, f reporterHandler) CtxHandlerFunc {
func captureReporter(rep Reporter, f reporterHandler) CtxHandlerFunc {
return func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
f(ctx, rep, metricsGraphURL, w, r)
f(ctx, rep, w, r)
}
}
type rendererHandler func(context.Context, render.Renderer, render.Decorator, report.RenderContext, http.ResponseWriter, *http.Request)
func (r *Registry) captureRenderer(rep Reporter, metricsGraphURL string, f rendererHandler) CtxHandlerFunc {
func (r *Registry) captureRenderer(rep Reporter, f rendererHandler) CtxHandlerFunc {
return func(ctx context.Context, w http.ResponseWriter, req *http.Request) {
var (
topologyID = mux.Vars(req)["topology"]
@@ -579,6 +579,6 @@ func (r *Registry) captureRenderer(rep Reporter, metricsGraphURL string, f rende
respondWith(w, http.StatusInternalServerError, err)
return
}
f(ctx, renderer, decorator, report.RenderContext{Report: rpt, MetricsGraphURL: metricsGraphURL}, w, req)
f(ctx, renderer, decorator, RenderContextForReporter(rep, rpt), w, req)
}
}

View File

@@ -56,7 +56,6 @@ func handleNode(ctx context.Context, renderer render.Renderer, decorator render.
func handleWebsocket(
ctx context.Context,
rep Reporter,
metricsGraphURL string,
w http.ResponseWriter,
r *http.Request,
) {
@@ -124,10 +123,7 @@ func handleWebsocket(
log.Errorf("Error generating report: %v", err)
return
}
newTopo := detailed.Summaries(
report.RenderContext{Report: re, MetricsGraphURL: metricsGraphURL},
renderer.Render(re, decorator),
)
newTopo := detailed.Summaries(RenderContextForReporter(rep, re), renderer.Render(re, decorator))
diff := detailed.TopoDiff(previousTopo, newTopo)
previousTopo = newTopo

View File

@@ -34,6 +34,23 @@ type Reporter interface {
UnWait(context.Context, chan struct{})
}
// WebReporter is a reporter that creates reports whose data is eventually
// displayed on websites. It carries fields that will be forwarded to the
// report.RenderContext
type WebReporter struct {
Reporter
MetricsGraphURL string
}
// RenderContextForReporter creates the rendering context for the given reporter.
func RenderContextForReporter(rep Reporter, r report.Report) report.RenderContext {
rc := report.RenderContext{Report: r}
if wrep, ok := rep.(WebReporter); ok {
rc.MetricsGraphURL = wrep.MetricsGraphURL
}
return rc
}
// Adder is something that can accept reports. It's a convenient interface for
// parts of the app, and several experimental components. It takes the following
// arguments:

View File

@@ -91,7 +91,7 @@ func gzipHandler(h http.HandlerFunc) http.HandlerFunc {
}
// RegisterTopologyRoutes registers the various topology routes with a http mux.
func RegisterTopologyRoutes(router *mux.Router, r Reporter, capabilities map[string]bool, metricsGraphURL string) {
func RegisterTopologyRoutes(router *mux.Router, r Reporter, capabilities map[string]bool) {
get := router.Methods("GET").Subrouter()
get.HandleFunc("/api",
gzipHandler(requestContextDecorator(apiHandler(r, capabilities))))
@@ -99,15 +99,15 @@ func RegisterTopologyRoutes(router *mux.Router, r Reporter, capabilities map[str
gzipHandler(requestContextDecorator(topologyRegistry.makeTopologyList(r))))
get.
HandleFunc("/api/topology/{topology}",
gzipHandler(requestContextDecorator(topologyRegistry.captureRenderer(r, metricsGraphURL, handleTopology)))).
gzipHandler(requestContextDecorator(topologyRegistry.captureRenderer(r, handleTopology)))).
Name("api_topology_topology")
get.
HandleFunc("/api/topology/{topology}/ws",
requestContextDecorator(captureReporter(r, metricsGraphURL, handleWebsocket))). // NB not gzip!
requestContextDecorator(captureReporter(r, handleWebsocket))). // NB not gzip!
Name("api_topology_topology_ws")
get.
MatcherFunc(URLMatcher("/api/topology/{topology}/{id}")).HandlerFunc(
gzipHandler(requestContextDecorator(topologyRegistry.captureRenderer(r, metricsGraphURL, handleNode)))).
gzipHandler(requestContextDecorator(topologyRegistry.captureRenderer(r, handleNode)))).
Name("api_topology_topology_id")
get.HandleFunc("/api/report",
gzipHandler(requestContextDecorator(makeRawReportHandler(r))))

View File

@@ -61,7 +61,7 @@ func router(collector app.Collector, controlRouter app.ControlRouter, pipeRouter
app.RegisterReportPostHandler(collector, router)
app.RegisterControlRoutes(router, controlRouter)
app.RegisterPipeRoutes(router, pipeRouter)
app.RegisterTopologyRoutes(router, collector, capabilities, metricsGraphURL)
app.RegisterTopologyRoutes(router, app.WebReporter{Reporter: collector, MetricsGraphURL: metricsGraphURL}, capabilities)
uiHandler := http.FileServer(GetFS(externalUI))
router.PathPrefix("/ui").Name("static").Handler(