Use MountableRouter when Basepath is set

This breaks compatibility with the old behavior of --basepath, so this
requires that proxies be configured to not modify routes forwarded to
backends. The old behavior will be supported in a subsequent commit.
pull/1168/head
Tim Raymond 2017-03-31 16:14:46 -04:00
parent 84cc43cc77
commit f35de37257
2 changed files with 22 additions and 3 deletions

View File

@ -31,7 +31,7 @@ type MuxOpts struct {
// NewMux attaches all the route handlers; handler returned servers chronograf.
func NewMux(opts MuxOpts, service Service) http.Handler {
router := httprouter.New()
hr := httprouter.New()
/* React Application */
assets := Assets(AssetsOpts{
@ -42,13 +42,29 @@ func NewMux(opts MuxOpts, service Service) http.Handler {
// Prefix any URLs found in the React assets with any configured basepath
prefixedAssets := NewDefaultURLPrefixer(basepath, assets, opts.Logger)
// Set route prefix for all routes if basepath is present
var router chronograf.Router
if opts.Basepath != "" {
router = &MountableRouter{
Prefix: opts.Basepath,
Delegate: hr,
}
} else {
router = hr
}
// Compress the assets with gzip if an accepted encoding
compressed := gziphandler.GzipHandler(prefixedAssets)
// The react application handles all the routing if the server does not
// know about the route. This means that we never have unknown
// routes on the server.
router.NotFound = compressed
hr.NotFound = http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
if opts.Basepath != "" {
r.URL.Path = r.URL.Path[len(opts.Basepath):]
}
compressed.ServeHTTP(rw, r)
})
/* Documentation */
router.GET("/swagger.json", Spec())
@ -178,7 +194,7 @@ func NewMux(opts MuxOpts, service Service) http.Handler {
// AuthAPI adds the OAuth routes if auth is enabled.
// TODO: this function is not great. Would be good if providers added their routes.
func AuthAPI(opts MuxOpts, router *httprouter.Router) (http.Handler, AuthRoutes) {
func AuthAPI(opts MuxOpts, router chronograf.Router) (http.Handler, AuthRoutes) {
auth := oauth2.NewJWT(opts.TokenSecret)
routes := AuthRoutes{}
for _, pf := range opts.ProviderFuncs {

View File

@ -14,7 +14,9 @@ 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"
@ -217,6 +219,7 @@ func (s *Server) Serve(ctx context.Context) error {
Logger: logger,
UseAuth: s.useAuth(),
ProviderFuncs: providerFuncs,
Basepath: basepath,
}, service)
// Add chronograf's version header to all requests