2019-10-27 05:53:25 +00:00
|
|
|
package cluster
|
|
|
|
|
|
|
|
import (
|
2024-04-25 23:49:47 +00:00
|
|
|
"fmt"
|
2019-10-27 05:53:25 +00:00
|
|
|
"net/http"
|
2024-04-25 23:49:47 +00:00
|
|
|
|
|
|
|
"github.com/k3s-io/k3s/pkg/util"
|
2019-10-27 05:53:25 +00:00
|
|
|
)
|
|
|
|
|
2020-09-24 06:29:25 +00:00
|
|
|
// getHandler returns a basic request handler that processes requests through
|
|
|
|
// the cluster's request router chain.
|
2019-10-27 05:53:25 +00:00
|
|
|
func (c *Cluster) getHandler(handler http.Handler) (http.Handler, error) {
|
|
|
|
next := c.router()
|
|
|
|
|
|
|
|
return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
|
|
|
handler.ServeHTTP(rw, req)
|
|
|
|
next.ServeHTTP(rw, req)
|
|
|
|
}), nil
|
|
|
|
}
|
|
|
|
|
2020-09-24 06:29:25 +00:00
|
|
|
// router is a stub request router that returns a Service Unavailable response
|
|
|
|
// if no additional handlers are available.
|
2019-10-27 05:53:25 +00:00
|
|
|
func (c *Cluster) router() http.Handler {
|
|
|
|
return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
2024-04-25 23:49:47 +00:00
|
|
|
if c.config.Runtime.Handler != nil {
|
|
|
|
c.config.Runtime.Handler.ServeHTTP(rw, req)
|
|
|
|
} else {
|
|
|
|
util.SendError(fmt.Errorf("starting"), rw, req, http.StatusServiceUnavailable)
|
2019-10-27 05:53:25 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|