mirror of
https://github.com/VictoriaMetrics/VictoriaMetrics.git
synced 2024-11-21 14:44:00 +00:00
feat(httpserver): add http.externalUrl
config to http server, it adds prefix to http path automatically (#452)
This commit is contained in:
parent
8e041f1911
commit
a0589f2ca5
1 changed files with 13 additions and 0 deletions
|
@ -23,6 +23,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
httpExternalURL = flag.String("http.externalURL", "", "The URL under which the http service is externally reachable")
|
||||||
disableResponseCompression = flag.Bool("http.disableResponseCompression", false, "Disable compression of HTTP responses for saving CPU resources. By default compression is enabled to save network bandwidth")
|
disableResponseCompression = flag.Bool("http.disableResponseCompression", false, "Disable compression of HTTP responses for saving CPU resources. By default compression is enabled to save network bandwidth")
|
||||||
maxGracefulShutdownDuration = flag.Duration("http.maxGracefulShutdownDuration", 7*time.Second, "The maximum duration for graceful shutdown of HTTP server. "+
|
maxGracefulShutdownDuration = flag.Duration("http.maxGracefulShutdownDuration", 7*time.Second, "The maximum duration for graceful shutdown of HTTP server. "+
|
||||||
"Highly loaded server may require increased value for graceful shutdown")
|
"Highly loaded server may require increased value for graceful shutdown")
|
||||||
|
@ -47,6 +48,16 @@ type RequestHandler func(w http.ResponseWriter, r *http.Request) bool
|
||||||
//
|
//
|
||||||
// The compression is also disabled if -http.disableResponseCompression flag is set.
|
// The compression is also disabled if -http.disableResponseCompression flag is set.
|
||||||
func Serve(addr string, rh RequestHandler) {
|
func Serve(addr string, rh RequestHandler) {
|
||||||
|
// parse and format `httpExternalURL` to /<defined_string>` ,
|
||||||
|
if *httpExternalURL != "" {
|
||||||
|
if !strings.HasPrefix(*httpExternalURL, "/") {
|
||||||
|
*httpExternalURL = "/" + *httpExternalURL
|
||||||
|
}
|
||||||
|
if strings.HasSuffix(*httpExternalURL, "/") {
|
||||||
|
*httpExternalURL = strings.TrimRight(*httpExternalURL, "/")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
logger.Infof("starting http server at http://%s/", addr)
|
logger.Infof("starting http server at http://%s/", addr)
|
||||||
logger.Infof("pprof handlers are exposed at http://%s/debug/pprof/", addr)
|
logger.Infof("pprof handlers are exposed at http://%s/debug/pprof/", addr)
|
||||||
ln, err := netutil.NewTCPListener("http", addr)
|
ln, err := netutil.NewTCPListener("http", addr)
|
||||||
|
@ -130,6 +141,8 @@ var metricsHandlerDuration = metrics.NewHistogram(`vm_http_request_duration_seco
|
||||||
|
|
||||||
func handlerWrapper(w http.ResponseWriter, r *http.Request, rh RequestHandler) {
|
func handlerWrapper(w http.ResponseWriter, r *http.Request, rh RequestHandler) {
|
||||||
requestsTotal.Inc()
|
requestsTotal.Inc()
|
||||||
|
// delete extra url string, extra is a string formated as `/<defined_string>` when server start up
|
||||||
|
r.URL.Path = strings.Replace(r.URL.Path, *httpExternalURL, "", 1)
|
||||||
switch r.URL.Path {
|
switch r.URL.Path {
|
||||||
case "/health":
|
case "/health":
|
||||||
w.Header().Set("Content-Type", "text/plain")
|
w.Header().Set("Content-Type", "text/plain")
|
||||||
|
|
Loading…
Reference in a new issue