...

Source file src/github.com/prometheus/alertmanager/ui/web.go

Documentation: github.com/prometheus/alertmanager/ui

     1  // Copyright 2015 Prometheus Team
     2  // Licensed under the Apache License, Version 2.0 (the "License");
     3  // you may not use this file except in compliance with the License.
     4  // You may obtain a copy of the License at
     5  //
     6  // http://www.apache.org/licenses/LICENSE-2.0
     7  //
     8  // Unless required by applicable law or agreed to in writing, software
     9  // distributed under the License is distributed on an "AS IS" BASIS,
    10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package ui
    15  
    16  import (
    17  	"fmt"
    18  	"net/http"
    19  	_ "net/http/pprof" // Comment this line to disable pprof endpoint.
    20  	"path"
    21  
    22  	"github.com/go-kit/log"
    23  	"github.com/prometheus/client_golang/prometheus/promhttp"
    24  	"github.com/prometheus/common/route"
    25  
    26  	"github.com/prometheus/alertmanager/asset"
    27  )
    28  
    29  // Register registers handlers to serve files for the web interface.
    30  func Register(r *route.Router, reloadCh chan<- chan error, logger log.Logger) {
    31  	r.Get("/metrics", promhttp.Handler().ServeHTTP)
    32  
    33  	r.Get("/", func(w http.ResponseWriter, req *http.Request) {
    34  		disableCaching(w)
    35  
    36  		req.URL.Path = "/static/"
    37  		fs := http.FileServer(asset.Assets)
    38  		fs.ServeHTTP(w, req)
    39  	})
    40  
    41  	r.Get("/script.js", func(w http.ResponseWriter, req *http.Request) {
    42  		disableCaching(w)
    43  
    44  		req.URL.Path = "/static/script.js"
    45  		fs := http.FileServer(asset.Assets)
    46  		fs.ServeHTTP(w, req)
    47  	})
    48  
    49  	r.Get("/favicon.ico", func(w http.ResponseWriter, req *http.Request) {
    50  		disableCaching(w)
    51  
    52  		req.URL.Path = "/static/favicon.ico"
    53  		fs := http.FileServer(asset.Assets)
    54  		fs.ServeHTTP(w, req)
    55  	})
    56  
    57  	r.Get("/lib/*path", func(w http.ResponseWriter, req *http.Request) {
    58  		disableCaching(w)
    59  
    60  		req.URL.Path = path.Join("/static/lib", route.Param(req.Context(), "path"))
    61  		fs := http.FileServer(asset.Assets)
    62  		fs.ServeHTTP(w, req)
    63  	})
    64  
    65  	r.Post("/-/reload", http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
    66  		errc := make(chan error)
    67  		defer close(errc)
    68  
    69  		reloadCh <- errc
    70  		if err := <-errc; err != nil {
    71  			http.Error(w, fmt.Sprintf("failed to reload config: %s", err), http.StatusInternalServerError)
    72  		}
    73  	}))
    74  
    75  	r.Get("/-/healthy", http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
    76  		w.WriteHeader(http.StatusOK)
    77  		fmt.Fprintf(w, "OK")
    78  	}))
    79  	r.Head("/-/healthy", http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
    80  		w.WriteHeader(http.StatusOK)
    81  	}))
    82  	r.Get("/-/ready", http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
    83  		w.WriteHeader(http.StatusOK)
    84  		fmt.Fprintf(w, "OK")
    85  	}))
    86  	r.Head("/-/ready", http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
    87  		w.WriteHeader(http.StatusOK)
    88  	}))
    89  
    90  	r.Get("/debug/*subpath", http.DefaultServeMux.ServeHTTP)
    91  	r.Post("/debug/*subpath", http.DefaultServeMux.ServeHTTP)
    92  }
    93  
    94  func disableCaching(w http.ResponseWriter) {
    95  	w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
    96  	w.Header().Set("Pragma", "no-cache")
    97  	w.Header().Set("Expires", "0") // Prevent proxies from caching.
    98  }
    99  

View as plain text