Commit 261beb04 authored by Matthew Holt's avatar Matthew Holt

Moved rewrite middleware into its own package

parent b8c43e55
package middleware // Package rewrite is middleware for rewriting requests internally to
import "net/http"
// Rewrite is middleware for rewriting requests internally to
// a different path. // a different path.
func Rewrite(p parser) Middleware { package rewrite
// Rewrite describes an internal location rewrite rule. import (
type rewrite struct { "net/http"
From string
To string "github.com/mholt/caddy/middleware"
} )
// New instantiates a new rewrite middleware.
func New(c middleware.Controller) (middleware.Middleware, error) {
var rewrites []rewrite var rewrites []rewrite
for p.Next() { for c.Next() {
var rule rewrite var rule rewrite
if !p.NextArg() { if !c.NextArg() {
return p.ArgErr() return nil, c.ArgErr()
} }
rule.From = p.Val() rule.From = c.Val()
if !p.NextArg() { if !c.NextArg() {
return p.ArgErr() return nil, c.ArgErr()
} }
rule.To = p.Val() rule.To = c.Val()
rewrites = append(rewrites, rule) rewrites = append(rewrites, rule)
} }
...@@ -40,5 +38,11 @@ func Rewrite(p parser) Middleware { ...@@ -40,5 +38,11 @@ func Rewrite(p parser) Middleware {
} }
next(w, r) next(w, r)
} }
} }, nil
}
// rewrite describes an internal location rewrite rule.
type rewrite struct {
From string
To string
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment