diff --git a/server/mux.go b/server/mux.go index 8f5bee049..844c1b76b 100644 --- a/server/mux.go +++ b/server/mux.go @@ -20,11 +20,12 @@ const ( // MuxOpts are the options for the router. Mostly related to auth. type MuxOpts struct { - Logger chronograf.Logger - Develop bool // Develop loads assets from filesystem instead of bindata - Basepath string // URL path prefix under which all chronograf routes will be mounted - UseAuth bool // UseAuth turns on Github OAuth and JWT - TokenSecret string + Logger chronograf.Logger + Develop bool // Develop loads assets from filesystem instead of bindata + Basepath string // URL path prefix under which all chronograf routes will be mounted + PrefixRoutes bool // Mounts all backend routes under route specified by the Basepath + UseAuth bool // UseAuth turns on Github OAuth and JWT + TokenSecret string ProviderFuncs []func(func(oauth2.Provider, oauth2.Mux)) } @@ -44,7 +45,7 @@ func NewMux(opts MuxOpts, service Service) http.Handler { // Set route prefix for all routes if basepath is present var router chronograf.Router - if opts.Basepath != "" { + if opts.Basepath != "" && opts.PrefixRoutes { router = &MountableRouter{ Prefix: opts.Basepath, Delegate: hr, @@ -60,7 +61,9 @@ func NewMux(opts MuxOpts, service Service) http.Handler { // know about the route. This means that we never have unknown // routes on the server. hr.NotFound = http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { - if opts.Basepath != "" { + // The assets handler is always unaware of basepaths, so it needs to always + // be removed before sending requests to it + if opts.Basepath != "" && opts.PrefixRoutes { r.URL.Path = r.URL.Path[len(opts.Basepath):] } compressed.ServeHTTP(rw, r) diff --git a/server/server.go b/server/server.go index a7a942655..bb77d6006 100644 --- a/server/server.go +++ b/server/server.go @@ -14,9 +14,7 @@ import ( "github.com/influxdata/chronograf" "github.com/influxdata/chronograf/bolt" - "github.com/influxdata/chronograf/canned" "github.com/influxdata/chronograf/influx" - "github.com/influxdata/chronograf/layouts" clog "github.com/influxdata/chronograf/log" "github.com/influxdata/chronograf/oauth2" "github.com/influxdata/chronograf/uuid" @@ -71,6 +69,7 @@ type Server struct { ReportingDisabled bool `short:"r" long:"reporting-disabled" description:"Disable reporting of usage stats (os,arch,version,cluster_id,uptime) once every 24hr" env:"REPORTING_DISABLED"` LogLevel string `short:"l" long:"log-level" value-name:"choice" choice:"debug" choice:"info" choice:"error" default:"info" description:"Set the logging level" env:"LOG_LEVEL"` Basepath string `short:"p" long:"basepath" description:"A URL path prefix under which all chronograf routes will be mounted" env:"BASE_PATH"` + PrefixRoutes bool `long:"prefix-routes" description:"Force chronograf server to require that all requests to it are prefixed with the value set in --basepath" env:"PREFIX_ROUTES"` ShowVersion bool `short:"v" long:"version" description:"Show Chronograf version info"` BuildInfo BuildInfo Listener net.Listener @@ -220,6 +219,7 @@ func (s *Server) Serve(ctx context.Context) error { UseAuth: s.useAuth(), ProviderFuncs: providerFuncs, Basepath: basepath, + PrefixRoutes: s.PrefixRoutes, }, service) // Add chronograf's version header to all requests