2015-09-12 16:52:41 +08:00
|
|
|
package hugo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
2015-09-12 18:33:39 +08:00
|
|
|
"github.com/spf13/hugo/commands"
|
|
|
|
|
2015-09-12 16:52:41 +08:00
|
|
|
"github.com/mholt/caddy/config/setup"
|
|
|
|
"github.com/mholt/caddy/middleware"
|
2015-09-13 03:02:26 +08:00
|
|
|
|
|
|
|
"github.com/hacdias/caddy-hugo/routing"
|
2015-09-12 16:52:41 +08:00
|
|
|
)
|
|
|
|
|
2015-09-12 20:05:31 +08:00
|
|
|
// Setup function
|
2015-09-12 16:52:41 +08:00
|
|
|
func Setup(c *setup.Controller) (middleware.Middleware, error) {
|
2015-09-12 20:05:31 +08:00
|
|
|
commands.Execute()
|
2015-09-12 18:33:39 +08:00
|
|
|
|
2015-09-12 16:52:41 +08:00
|
|
|
return func(next middleware.Handler) middleware.Handler {
|
2015-09-13 01:58:10 +08:00
|
|
|
return &handler{Next: next}
|
2015-09-12 16:52:41 +08:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2015-09-12 21:08:48 +08:00
|
|
|
type handler struct{ Next middleware.Handler }
|
2015-09-12 16:52:41 +08:00
|
|
|
|
|
|
|
func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
|
2015-09-12 20:05:31 +08:00
|
|
|
if middleware.Path(r.URL.Path).Matches("/admin") {
|
2015-09-13 03:02:26 +08:00
|
|
|
return routing.Route(w, r)
|
2015-09-12 20:05:31 +08:00
|
|
|
}
|
|
|
|
|
2015-09-12 21:16:15 +08:00
|
|
|
return h.Next.ServeHTTP(w, r)
|
2015-09-12 20:05:31 +08:00
|
|
|
}
|