示例#1
0
// RegisterWeb registers handlers to serve files for the web interface.
func RegisterWeb(r *route.Router, reloadCh chan<- struct{}) {
	ihf := prometheus.InstrumentHandlerFunc

	r.Get("/app/*filepath", ihf("app_files",
		func(w http.ResponseWriter, req *http.Request) {
			fp := route.Param(route.Context(req), "filepath")
			serveAsset(w, req, filepath.Join("ui/app", fp))
		},
	))
	r.Get("/lib/*filepath", ihf("lib_files",
		func(w http.ResponseWriter, req *http.Request) {
			fp := route.Param(route.Context(req), "filepath")
			serveAsset(w, req, filepath.Join("ui/lib", fp))
		},
	))

	r.Get("/metrics", prometheus.Handler().ServeHTTP)

	r.Get("/", ihf("index", func(w http.ResponseWriter, req *http.Request) {
		serveAsset(w, req, "ui/app/index.html")
	}))

	r.Post("/-/reload", func(w http.ResponseWriter, req *http.Request) {
		w.Write([]byte("Reloading configuration file..."))
		reloadCh <- struct{}{}
	})

	r.Get("/debug/*subpath", http.DefaultServeMux.ServeHTTP)
	r.Post("/debug/*subpath", http.DefaultServeMux.ServeHTTP)
}
示例#2
0
// RegisterWeb registers handlers to serve files for the web interface.
func RegisterWeb(r *route.Router) {
	r.Get("/app/*filepath", func(w http.ResponseWriter, req *http.Request) {
		fp := route.Param(route.Context(req), "filepath")
		serveAsset(w, req, filepath.Join("ui/app", fp))
	})
	r.Get("/lib/*filepath", func(w http.ResponseWriter, req *http.Request) {
		fp := route.Param(route.Context(req), "filepath")
		serveAsset(w, req, filepath.Join("ui/lib", fp))
	})
	r.Get("/", func(w http.ResponseWriter, req *http.Request) {
		serveAsset(w, req, "ui/app/index.html")
	})
}
示例#3
0
func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	ctx := route.Context(r)

	name := strings.Trim(route.Param(ctx, "filepath"), "/")
	if name == "" {
		name = "index.html"
	}

	file, err := GetFile(StaticFiles, name)
	if err != nil {
		if err != io.EOF {
			log.Warn("Could not get file: ", err)
		}
		w.WriteHeader(http.StatusNotFound)
		return
	}
	contentType := http.DetectContentType(file)
	if strings.Contains(contentType, "text/plain") || strings.Contains(contentType, "application/octet-stream") {
		parts := strings.Split(name, ".")
		contentType = mimeMap[parts[len(parts)-1]]
	}
	w.Header().Set("Content-Type", contentType)
	w.Header().Set("Cache-Control", "public, max-age=259200")
	w.Write(file)
}
示例#4
0
// RegisterWeb registers handlers to serve files for the web interface.
func RegisterWeb(r *route.Router) {
	ihf := prometheus.InstrumentHandlerFunc

	r.Get("/app/*filepath", ihf("app_files",
		func(w http.ResponseWriter, req *http.Request) {
			fp := route.Param(route.Context(req), "filepath")
			serveAsset(w, req, filepath.Join("ui/app", fp))
		},
	))
	r.Get("/lib/*filepath", ihf("lib_files",
		func(w http.ResponseWriter, req *http.Request) {
			fp := route.Param(route.Context(req), "filepath")
			serveAsset(w, req, filepath.Join("ui/lib", fp))
		},
	))

	r.Get("/metrics", prometheus.Handler().ServeHTTP)

	r.Get("/", ihf("index", func(w http.ResponseWriter, req *http.Request) {
		serveAsset(w, req, "ui/app/index.html")
	}))
}
示例#5
0
文件: web.go 项目: mjseid/prometheus
func (h *Handler) consoles(w http.ResponseWriter, r *http.Request) {
	ctx := route.Context(r)
	name := route.Param(ctx, "filepath")

	file, err := http.Dir(h.options.ConsoleTemplatesPath).Open(name)
	if err != nil {
		http.Error(w, err.Error(), http.StatusNotFound)
		return
	}
	text, err := ioutil.ReadAll(file)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	// Provide URL parameters as a map for easy use. Advanced users may have need for
	// parameters beyond the first, so provide RawParams.
	rawParams, err := url.ParseQuery(r.URL.RawQuery)
	if err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}
	params := map[string]string{}
	for k, v := range rawParams {
		params[k] = v[0]
	}
	data := struct {
		RawParams url.Values
		Params    map[string]string
		Path      string
	}{
		RawParams: rawParams,
		Params:    params,
		Path:      strings.TrimLeft(name, "/"),
	}

	tmpl := template.NewTemplateExpander(string(text), "__console_"+name, data, model.Now(), h.queryEngine, h.options.ExternalURL.Path)
	filenames, err := filepath.Glob(h.options.ConsoleLibrariesPath + "/*.lib")
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	result, err := tmpl.ExpandHTML(filenames)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	io.WriteString(w, result)
}
示例#6
0
func serveStaticAsset(w http.ResponseWriter, req *http.Request) {
	fp := route.Param(route.Context(req), "filepath")
	fp = filepath.Join("web/ui/static", fp)

	info, err := ui.AssetInfo(fp)
	if err != nil {
		log.With("file", fp).Warn("Could not get file info: ", err)
		w.WriteHeader(http.StatusNotFound)
		return
	}
	file, err := ui.Asset(fp)
	if err != nil {
		if err != io.EOF {
			log.With("file", fp).Warn("Could not get file: ", err)
		}
		w.WriteHeader(http.StatusNotFound)
		return
	}

	http.ServeContent(w, req, info.Name(), info.ModTime(), bytes.NewReader(file))
}