Skip to content

Commit

Permalink
Refactor and centralize HTTP registration.
Browse files Browse the repository at this point in the history
  • Loading branch information
vmarmol committed Mar 11, 2015
1 parent 770eae1 commit d357c34
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 80 deletions.
4 changes: 2 additions & 2 deletions api/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@ import (

"github.com/golang/glog"
"github.com/google/cadvisor/events"
httpMux "github.com/google/cadvisor/http/mux"
info "github.com/google/cadvisor/info/v1"
"github.com/google/cadvisor/manager"
"github.com/google/cadvisor/utils"
)

const (
apiResource = "/api/"
)

func RegisterHandlers(mux utils.Mux, m manager.Manager) error {
func RegisterHandlers(mux httpMux.Mux, m manager.Manager) error {
apiVersions := getApiVersions()
supportedApiVersions := make(map[string]ApiVersion, len(apiVersions))
for _, v := range apiVersions {
Expand Down
77 changes: 5 additions & 72 deletions cadvisor.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,10 @@ import (
"runtime"
"syscall"

auth "github.com/abbot/go-http-auth"
"github.com/golang/glog"
"github.com/google/cadvisor/api"
"github.com/google/cadvisor/healthz"
cadvisorHttp "github.com/google/cadvisor/http"
"github.com/google/cadvisor/manager"
"github.com/google/cadvisor/pages"
"github.com/google/cadvisor/pages/static"
"github.com/google/cadvisor/utils/sysfs"
"github.com/google/cadvisor/validate"
"github.com/google/cadvisor/version"
)

Expand Down Expand Up @@ -76,58 +71,10 @@ func main() {

mux := http.DefaultServeMux

// TODO(vmarmol): Use Kubernetes'.
// Basic health handler.
if err := healthz.RegisterHandler(mux); err != nil {
glog.Fatalf("Failed to register healthz handler: %s", err)
}

// Validation/Debug handler.
mux.HandleFunc(validate.ValidatePage, func(w http.ResponseWriter, r *http.Request) {
err := validate.HandleRequest(w, containerManager)
if err != nil {
fmt.Fprintf(w, "%s", err)
}
})

// Register API handler.
if err := api.RegisterHandlers(mux, containerManager); err != nil {
glog.Fatalf("Failed to register API handlers: %s", err)
}

// Redirect / to containers page.
mux.Handle("/", http.RedirectHandler(pages.ContainersPage, http.StatusTemporaryRedirect))

var authenticated bool = false

// Setup the authenticator object
if *httpAuthFile != "" {
glog.Infof("Using auth file %s", *httpAuthFile)
secrets := auth.HtpasswdFileProvider(*httpAuthFile)
authenticator := auth.NewBasicAuthenticator(*httpAuthRealm, secrets)
mux.HandleFunc(static.StaticResource, authenticator.Wrap(staticHandler))
if err := pages.RegisterHandlersBasic(mux, containerManager, authenticator); err != nil {
glog.Fatalf("Failed to register pages auth handlers: %s", err)
}
authenticated = true
}
if *httpAuthFile == "" && *httpDigestFile != "" {
glog.Infof("Using digest file %s", *httpDigestFile)
secrets := auth.HtdigestFileProvider(*httpDigestFile)
authenticator := auth.NewDigestAuthenticator(*httpDigestRealm, secrets)
mux.HandleFunc(static.StaticResource, authenticator.Wrap(staticHandler))
if err := pages.RegisterHandlersDigest(mux, containerManager, authenticator); err != nil {
glog.Fatalf("Failed to register pages digest handlers: %s", err)
}
authenticated = true
}

// Change handler based on authenticator initalization
if !authenticated {
mux.HandleFunc(static.StaticResource, staticHandlerNoAuth)
if err := pages.RegisterHandlersBasic(mux, containerManager, nil); err != nil {
glog.Fatalf("Failed to register pages handlers: %s", err)
}
// Register all HTTP handlers.
err = cadvisorHttp.RegisterHandlers(mux, containerManager, *httpAuthFile, *httpAuthRealm, *httpDigestFile, *httpDigestRealm)
if err != nil {
glog.Fatalf("Failed to register HTTP handlers: %v", err)
}

// Start the manager.
Expand Down Expand Up @@ -176,17 +123,3 @@ func installSignalHandler(containerManager manager.Manager) {
os.Exit(0)
}()
}

func staticHandlerNoAuth(w http.ResponseWriter, r *http.Request) {
err := static.HandleRequest(w, r.URL)
if err != nil {
fmt.Fprintf(w, "%s", err)
}
}

func staticHandler(w http.ResponseWriter, r *auth.AuthenticatedRequest) {
err := static.HandleRequest(w, r.URL)
if err != nil {
fmt.Fprintf(w, "%s", err)
}
}
4 changes: 2 additions & 2 deletions healthz/healthz.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package healthz
import (
"net/http"

"github.com/google/cadvisor/utils"
httpMux "github.com/google/cadvisor/http/mux"
)

func handleHealthz(w http.ResponseWriter, r *http.Request) {
Expand All @@ -26,7 +26,7 @@ func handleHealthz(w http.ResponseWriter, r *http.Request) {
}

// Register simple HTTP /healthz handler to return "ok".
func RegisterHandler(mux utils.Mux) error {
func RegisterHandler(mux httpMux.Mux) error {
mux.HandleFunc("/healthz", handleHealthz)
return nil
}
101 changes: 101 additions & 0 deletions http/handlers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// Copyright 2015 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package http

import (
"fmt"
"net/http"

auth "github.com/abbot/go-http-auth"
"github.com/golang/glog"
"github.com/google/cadvisor/api"
"github.com/google/cadvisor/healthz"
httpMux "github.com/google/cadvisor/http/mux"
"github.com/google/cadvisor/manager"
"github.com/google/cadvisor/pages"
"github.com/google/cadvisor/pages/static"
"github.com/google/cadvisor/validate"
)

func RegisterHandlers(mux httpMux.Mux, containerManager manager.Manager, httpAuthFile, httpAuthRealm, httpDigestFile, httpDigestRealm string) error {
// Basic health handler.
if err := healthz.RegisterHandler(mux); err != nil {
return fmt.Errorf("failed to register healthz handler: %s", err)
}

// Validation/Debug handler.
mux.HandleFunc(validate.ValidatePage, func(w http.ResponseWriter, r *http.Request) {
err := validate.HandleRequest(w, containerManager)
if err != nil {
fmt.Fprintf(w, "%s", err)
}
})

// Register API handler.
if err := api.RegisterHandlers(mux, containerManager); err != nil {
return fmt.Errorf("failed to register API handlers: %s", err)
}

// Redirect / to containers page.
mux.Handle("/", http.RedirectHandler(pages.ContainersPage, http.StatusTemporaryRedirect))

var authenticated bool = false

// Setup the authenticator object
if httpAuthFile != "" {
glog.Infof("Using auth file %s", httpAuthFile)
secrets := auth.HtpasswdFileProvider(httpAuthFile)
authenticator := auth.NewBasicAuthenticator(httpAuthRealm, secrets)
mux.HandleFunc(static.StaticResource, authenticator.Wrap(staticHandler))
if err := pages.RegisterHandlersBasic(mux, containerManager, authenticator); err != nil {
return fmt.Errorf("failed to register pages auth handlers: %s", err)
}
authenticated = true
}
if httpAuthFile == "" && httpDigestFile != "" {
glog.Infof("Using digest file %s", httpDigestFile)
secrets := auth.HtdigestFileProvider(httpDigestFile)
authenticator := auth.NewDigestAuthenticator(httpDigestRealm, secrets)
mux.HandleFunc(static.StaticResource, authenticator.Wrap(staticHandler))
if err := pages.RegisterHandlersDigest(mux, containerManager, authenticator); err != nil {
fmt.Errorf("failed to register pages digest handlers: %s", err)
}
authenticated = true
}

// Change handler based on authenticator initalization
if !authenticated {
mux.HandleFunc(static.StaticResource, staticHandlerNoAuth)
if err := pages.RegisterHandlersBasic(mux, containerManager, nil); err != nil {
return fmt.Errorf("failed to register pages handlers: %s", err)
}
}

return nil
}

func staticHandlerNoAuth(w http.ResponseWriter, r *http.Request) {
err := static.HandleRequest(w, r.URL)
if err != nil {
fmt.Fprintf(w, "%s", err)
}
}

func staticHandler(w http.ResponseWriter, r *auth.AuthenticatedRequest) {
err := static.HandleRequest(w, r.URL)
if err != nil {
fmt.Fprintf(w, "%s", err)
}
}
3 changes: 2 additions & 1 deletion utils/mux.go → http/mux/mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package utils
package mux

import (
"net/http"
Expand All @@ -22,4 +22,5 @@ import (
type Mux interface {
HandleFunc(pattern string, handler func(http.ResponseWriter, *http.Request))
Handler(r *http.Request) (http.Handler, string)
Handle(pattern string, handler http.Handler)
}
6 changes: 3 additions & 3 deletions pages/pages.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ import (

auth "github.com/abbot/go-http-auth"
"github.com/golang/glog"
httpMux "github.com/google/cadvisor/http/mux"
info "github.com/google/cadvisor/info/v1"
"github.com/google/cadvisor/manager"
"github.com/google/cadvisor/utils"
)

var pageTemplate *template.Template
Expand Down Expand Up @@ -98,7 +98,7 @@ func dockerHandler(containerManager manager.Manager) auth.AuthenticatedHandlerFu
}

// Register http handlers
func RegisterHandlersDigest(mux utils.Mux, containerManager manager.Manager, authenticator *auth.DigestAuth) error {
func RegisterHandlersDigest(mux httpMux.Mux, containerManager manager.Manager, authenticator *auth.DigestAuth) error {
// Register the handler for the containers page.
if authenticator != nil {
mux.HandleFunc(ContainersPage, authenticator.Wrap(containerHandler(containerManager)))
Expand All @@ -110,7 +110,7 @@ func RegisterHandlersDigest(mux utils.Mux, containerManager manager.Manager, aut
return nil
}

func RegisterHandlersBasic(mux utils.Mux, containerManager manager.Manager, authenticator *auth.BasicAuth) error {
func RegisterHandlersBasic(mux httpMux.Mux, containerManager manager.Manager, authenticator *auth.BasicAuth) error {
// Register the handler for the containers and docker age.
if authenticator != nil {
mux.HandleFunc(ContainersPage, authenticator.Wrap(containerHandler(containerManager)))
Expand Down

0 comments on commit d357c34

Please sign in to comment.