2015-01-30 13:06:19 +08:00
|
|
|
// Package rewrite is middleware for rewriting requests internally to
|
|
|
|
// a different path.
|
|
|
|
package rewrite
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/mholt/caddy/middleware"
|
|
|
|
)
|
|
|
|
|
2015-04-12 06:58:34 +08:00
|
|
|
// Rewrite is middleware to rewrite request locations internally before being handled.
|
|
|
|
type Rewrite struct {
|
|
|
|
Next middleware.Handler
|
2015-05-04 20:53:54 +08:00
|
|
|
Rules []Rule
|
2015-04-12 06:58:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// ServeHTTP implements the middleware.Handler interface.
|
|
|
|
func (rw Rewrite) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
|
|
|
|
for _, rule := range rw.Rules {
|
|
|
|
if r.URL.Path == rule.From {
|
|
|
|
r.URL.Path = rule.To
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rw.Next.ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
|
2015-05-04 20:53:54 +08:00
|
|
|
// A Rule describes an internal location rewrite rule.
|
|
|
|
type Rule struct {
|
2015-04-12 06:58:34 +08:00
|
|
|
From, To string
|
2015-01-30 13:06:19 +08:00
|
|
|
}
|