github.com/mrgossett/heapster@v0.18.2/handlers.go (about)

     1  // Copyright 2014 Google Inc. All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package main
    16  
    17  import (
    18  	"fmt"
    19  	"net/http"
    20  	"net/http/pprof"
    21  	"strings"
    22  
    23  	restful "github.com/emicklei/go-restful"
    24  	"k8s.io/heapster/api/v1"
    25  	"k8s.io/heapster/manager"
    26  	"k8s.io/heapster/sinks"
    27  	"k8s.io/heapster/sources/api"
    28  	"k8s.io/heapster/validate"
    29  )
    30  
    31  const pprofBasePath = "/debug/pprof/"
    32  
    33  func setupHandlers(sources []api.Source, sink sinks.ExternalSinkManager, m manager.Manager) http.Handler {
    34  	// Make API handler.
    35  	wsContainer := restful.NewContainer()
    36  	a := v1.NewApi(m)
    37  	a.Register(wsContainer)
    38  
    39  	// Validation/Debug handler.
    40  	handleValidate := func(req *restful.Request, resp *restful.Response) {
    41  		err := validate.HandleRequest(resp, sources, sink)
    42  		if err != nil {
    43  			fmt.Fprintf(resp, "%s", err)
    44  		}
    45  	}
    46  	ws := new(restful.WebService).
    47  		Path("/validate").
    48  		Produces("text/plain")
    49  	ws.Route(ws.GET("").To(handleValidate)).
    50  		Doc("get validation information")
    51  	wsContainer.Add(ws)
    52  
    53  	// TODO(jnagal): Add a main status page.
    54  	// Redirect root to /validate
    55  	redirectHandler := http.RedirectHandler(validate.ValidatePage, http.StatusTemporaryRedirect)
    56  	handleRoot := func(req *restful.Request, resp *restful.Response) {
    57  		redirectHandler.ServeHTTP(resp, req.Request)
    58  	}
    59  	ws = new(restful.WebService)
    60  	ws.Route(ws.GET("/").To(handleRoot))
    61  	wsContainer.Add(ws)
    62  
    63  	handlePprofEndpoint := func(req *restful.Request, resp *restful.Response) {
    64  		name := strings.TrimPrefix(req.Request.URL.Path, pprofBasePath)
    65  		switch name {
    66  		case "profile":
    67  			pprof.Profile(resp, req.Request)
    68  		case "symbol":
    69  			pprof.Symbol(resp, req.Request)
    70  		case "cmdline":
    71  			pprof.Cmdline(resp, req.Request)
    72  		default:
    73  			pprof.Index(resp, req.Request)
    74  		}
    75  	}
    76  
    77  	// Setup pporf handlers.
    78  	ws = new(restful.WebService).Path(pprofBasePath)
    79  	ws.Route(ws.GET("/{subpath:*}").To(func(req *restful.Request, resp *restful.Response) {
    80  		handlePprofEndpoint(req, resp)
    81  	})).Doc("pprof endpoint")
    82  	wsContainer.Add(ws)
    83  
    84  	return wsContainer
    85  }