2018-11-14 09:18:03 +00:00
|
|
|
package addprefix
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
2019-08-03 01:58:23 +00:00
|
|
|
"github.com/containous/traefik/v2/pkg/config/dynamic"
|
|
|
|
"github.com/containous/traefik/v2/pkg/middlewares"
|
|
|
|
"github.com/containous/traefik/v2/pkg/tracing"
|
2018-11-14 09:18:03 +00:00
|
|
|
"github.com/opentracing/opentracing-go/ext"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
typeName = "AddPrefix"
|
|
|
|
)
|
|
|
|
|
|
|
|
// AddPrefix is a middleware used to add prefix to an URL request.
|
|
|
|
type addPrefix struct {
|
|
|
|
next http.Handler
|
|
|
|
prefix string
|
|
|
|
name string
|
|
|
|
}
|
|
|
|
|
|
|
|
// New creates a new handler.
|
2019-07-10 07:26:04 +00:00
|
|
|
func New(ctx context.Context, next http.Handler, config dynamic.AddPrefix, name string) (http.Handler, error) {
|
2018-11-14 09:18:03 +00:00
|
|
|
middlewares.GetLogger(ctx, name, typeName).Debug("Creating middleware")
|
|
|
|
var result *addPrefix
|
|
|
|
|
|
|
|
if len(config.Prefix) > 0 {
|
|
|
|
result = &addPrefix{
|
|
|
|
prefix: config.Prefix,
|
|
|
|
next: next,
|
|
|
|
name: name,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return nil, fmt.Errorf("prefix cannot be empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ap *addPrefix) GetTracingInformation() (string, ext.SpanKindEnum) {
|
|
|
|
return ap.name, tracing.SpanKindNoneEnum
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ap *addPrefix) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
|
|
|
logger := middlewares.GetLogger(req.Context(), ap.name, typeName)
|
|
|
|
|
|
|
|
oldURLPath := req.URL.Path
|
|
|
|
req.URL.Path = ap.prefix + req.URL.Path
|
|
|
|
logger.Debugf("URL.Path is now %s (was %s).", req.URL.Path, oldURLPath)
|
|
|
|
|
|
|
|
if req.URL.RawPath != "" {
|
|
|
|
oldURLRawPath := req.URL.RawPath
|
|
|
|
req.URL.RawPath = ap.prefix + req.URL.RawPath
|
|
|
|
logger.Debugf("URL.RawPath is now %s (was %s).", req.URL.RawPath, oldURLRawPath)
|
|
|
|
}
|
|
|
|
req.RequestURI = req.URL.RequestURI()
|
|
|
|
|
|
|
|
ap.next.ServeHTTP(rw, req)
|
|
|
|
}
|