diff --git a/caddyhugo.go b/caddyhugo.go
index b8b9342..89c227f 100644
--- a/caddyhugo.go
+++ b/caddyhugo.go
@@ -20,11 +20,9 @@ import (
)
func init() {
- plugin := CaddyHugo{}
-
caddy.RegisterPlugin("hugo", caddy.Plugin{
ServerType: "http",
- Action: plugin.SetupCaddy,
+ Action: SetupCaddy,
})
}
diff --git a/http.go b/http.go
index eb83273..707f4a8 100644
--- a/http.go
+++ b/http.go
@@ -3,6 +3,7 @@ package caddyhugo
import (
"encoding/base64"
"fmt"
+ "net"
"net/http"
"os"
"path"
@@ -15,7 +16,7 @@ import (
"github.com/spf13/afero"
)
-func (ch *CaddyHugo) ServeHTTPWithNext(next httpserver.Handler, c *caddy.Controller, w http.ResponseWriter, r *http.Request) (int, error) {
+func (ch *CaddyHugo) ServeHTTPWithNext(next httpserver.Handler, w http.ResponseWriter, r *http.Request) (int, error) {
if !ch.Match(r) {
p := path.Join(ch.Dir, "public", r.URL.Path)
http.ServeFile(w, r, p)
@@ -73,7 +74,7 @@ func (ch *CaddyHugo) ServeHTTPWithNext(next httpserver.Handler, c *caddy.Control
return ch.AuthorHome().ServeHTTP(w, r)
}
if strings.HasPrefix(r.URL.Path, "/hugo/edit/") {
- return ch.Edit(c).ServeHTTP(w, r)
+ return ch.Edit().ServeHTTP(w, r)
}
if strings.HasPrefix(r.URL.Path, "/hugo/draft/") {
return ch.serveDraft(w, r)
@@ -88,8 +89,7 @@ func (ch *CaddyHugo) ServeHTTPWithNext(next httpserver.Handler, c *caddy.Control
return ch.serveMedia(w, r)
}
- http.NotFound(w, r)
- return 404, nil
+ return next.ServeHTTP(w, r)
}
func (ch *CaddyHugo) ServeNewContent(w http.ResponseWriter, r *http.Request) (int, error) {
@@ -106,10 +106,15 @@ func (ch *CaddyHugo) ServeNewContent(w http.ResponseWriter, r *http.Request) (in
http.Redirect(w, r, filepath.Join("/hugo/edit/", "content", filename), http.StatusFound)
return http.StatusFound, nil
}
+
func (ch *CaddyHugo) Middleware(c *caddy.Controller) httpserver.Middleware {
return func(next httpserver.Handler) httpserver.Handler {
+ host := net.JoinHostPort(ch.Site.Addr.Host, ch.Site.Addr.Port)
return httpserver.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) {
- return ch.ServeHTTPWithNext(next, c, w, r)
+ if r.Host != host {
+ return next.ServeHTTP(w, r)
+ }
+ return ch.ServeHTTPWithNext(next, w, r)
})
}
}
@@ -120,6 +125,10 @@ func (ch *CaddyHugo) Auth(r *http.Request) bool {
}
func (ch *CaddyHugo) Match(r *http.Request) bool {
+ host := net.JoinHostPort(ch.Site.Addr.Host, ch.Site.Addr.Port)
+ if r.Host != host {
+ return false
+ }
if strings.HasPrefix(r.URL.Path, "/media/") {
return true
}
@@ -165,7 +174,7 @@ func (ch *CaddyHugo) AuthorHome() httpserver.Handler {
})
}
-func (ch *CaddyHugo) Edit(c *caddy.Controller) httpserver.Handler {
+func (ch *CaddyHugo) Edit() httpserver.Handler {
return httpserver.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) {
if r.URL.Path == "/hugo/edit/new" {
return ch.ServeNewContent(w, r)
diff --git a/setup.go b/setup.go
index 17994cc..cad1fda 100644
--- a/setup.go
+++ b/setup.go
@@ -16,15 +16,16 @@ import (
"github.com/mholt/caddy/caddyhttp/httpserver"
)
-func (ch *CaddyHugo) SetupCaddy(c *caddy.Controller) error {
+var eventHookCounter uint64
+
+func SetupCaddy(c *caddy.Controller) error {
+ ch := &CaddyHugo{}
+
ch.Site = httpserver.GetConfig(c)
err := ch.Setup(ch.Site.Root)
- caddy.RegisterEventHook("caddyhugo-shutdown", func(eventType caddy.EventName, eventInfo interface{}) error {
- if eventType == caddy.ShutdownEvent {
- return ch.persistAllEdits()
- }
- return nil
+ c.OnShutdown(func() error {
+ return ch.persistAllEdits()
})
ch.Site.AddMiddleware(ch.Middleware(c))
diff --git a/testdir/caddyfile b/testdir/caddyfile
index 0bfaa5b..1800280 100644
--- a/testdir/caddyfile
+++ b/testdir/caddyfile
@@ -6,3 +6,9 @@ localhost:8080 {
errors { * }
pprof
}
+
+localhost:8081 {
+ root ./testsite2
+ hugo
+ errors { * }
+}
diff --git a/testdir/testsite2/config.toml b/testdir/testsite2/config.toml
new file mode 100644
index 0000000..e4b7418
--- /dev/null
+++ b/testdir/testsite2/config.toml
@@ -0,0 +1,3 @@
+baseURL = "http://example.org/"
+languageCode = "en-us"
+title = "My New Hugo Site"
diff --git a/testdir/testsite2/public/categories/index.xml b/testdir/testsite2/public/categories/index.xml
new file mode 100644
index 0000000..6e385a4
--- /dev/null
+++ b/testdir/testsite2/public/categories/index.xml
@@ -0,0 +1,14 @@
+
+
+
+ Categories on My New Hugo Site
+ http://example.org/categories/
+ Recent content in Categories on My New Hugo Site
+ Hugo -- gohugo.io
+ en-us
+
+
+
+
+
+
\ No newline at end of file
diff --git a/testdir/testsite2/public/index.xml b/testdir/testsite2/public/index.xml
new file mode 100644
index 0000000..884a3c9
--- /dev/null
+++ b/testdir/testsite2/public/index.xml
@@ -0,0 +1,14 @@
+
+
+
+ My New Hugo Site
+ http://example.org/
+ Recent content on My New Hugo Site
+ Hugo -- gohugo.io
+ en-us
+
+
+
+
+
+
\ No newline at end of file
diff --git a/testdir/testsite2/public/sitemap.xml b/testdir/testsite2/public/sitemap.xml
new file mode 100644
index 0000000..60e0486
--- /dev/null
+++ b/testdir/testsite2/public/sitemap.xml
@@ -0,0 +1,20 @@
+
+
+
+
+ http://example.org/categories/
+ 0
+
+
+
+ http://example.org/
+ 0
+
+
+
+ http://example.org/tags/
+ 0
+
+
+
\ No newline at end of file
diff --git a/testdir/testsite2/public/tags/index.xml b/testdir/testsite2/public/tags/index.xml
new file mode 100644
index 0000000..a8ab33c
--- /dev/null
+++ b/testdir/testsite2/public/tags/index.xml
@@ -0,0 +1,14 @@
+
+
+
+ Tags on My New Hugo Site
+ http://example.org/tags/
+ Recent content in Tags on My New Hugo Site
+ Hugo -- gohugo.io
+ en-us
+
+
+
+
+
+
\ No newline at end of file