fixing multisite and other caddy integration issues

pull/12/head
Stephen Searles 7 years ago
parent c9508770a9
commit 6647e54b8d
  1. 4
      caddyhugo.go
  2. 21
      http.go
  3. 11
      setup.go
  4. 6
      testdir/caddyfile
  5. 3
      testdir/testsite2/config.toml
  6. 14
      testdir/testsite2/public/categories/index.xml
  7. 14
      testdir/testsite2/public/index.xml
  8. 20
      testdir/testsite2/public/sitemap.xml
  9. 14
      testdir/testsite2/public/tags/index.xml

@ -20,11 +20,9 @@ import (
) )
func init() { func init() {
plugin := CaddyHugo{}
caddy.RegisterPlugin("hugo", caddy.Plugin{ caddy.RegisterPlugin("hugo", caddy.Plugin{
ServerType: "http", ServerType: "http",
Action: plugin.SetupCaddy, Action: SetupCaddy,
}) })
} }

@ -3,6 +3,7 @@ package caddyhugo
import ( import (
"encoding/base64" "encoding/base64"
"fmt" "fmt"
"net"
"net/http" "net/http"
"os" "os"
"path" "path"
@ -15,7 +16,7 @@ import (
"github.com/spf13/afero" "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) { if !ch.Match(r) {
p := path.Join(ch.Dir, "public", r.URL.Path) p := path.Join(ch.Dir, "public", r.URL.Path)
http.ServeFile(w, r, p) 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) return ch.AuthorHome().ServeHTTP(w, r)
} }
if strings.HasPrefix(r.URL.Path, "/hugo/edit/") { 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/") { if strings.HasPrefix(r.URL.Path, "/hugo/draft/") {
return ch.serveDraft(w, r) return ch.serveDraft(w, r)
@ -88,8 +89,7 @@ func (ch *CaddyHugo) ServeHTTPWithNext(next httpserver.Handler, c *caddy.Control
return ch.serveMedia(w, r) return ch.serveMedia(w, r)
} }
http.NotFound(w, r) return next.ServeHTTP(w, r)
return 404, nil
} }
func (ch *CaddyHugo) ServeNewContent(w http.ResponseWriter, r *http.Request) (int, error) { 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) http.Redirect(w, r, filepath.Join("/hugo/edit/", "content", filename), http.StatusFound)
return http.StatusFound, nil return http.StatusFound, nil
} }
func (ch *CaddyHugo) Middleware(c *caddy.Controller) httpserver.Middleware { func (ch *CaddyHugo) Middleware(c *caddy.Controller) httpserver.Middleware {
return func(next httpserver.Handler) httpserver.Handler { 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 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 { 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/") { if strings.HasPrefix(r.URL.Path, "/media/") {
return true 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) { return httpserver.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) {
if r.URL.Path == "/hugo/edit/new" { if r.URL.Path == "/hugo/edit/new" {
return ch.ServeNewContent(w, r) return ch.ServeNewContent(w, r)

@ -16,15 +16,16 @@ import (
"github.com/mholt/caddy/caddyhttp/httpserver" "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) ch.Site = httpserver.GetConfig(c)
err := ch.Setup(ch.Site.Root) err := ch.Setup(ch.Site.Root)
caddy.RegisterEventHook("caddyhugo-shutdown", func(eventType caddy.EventName, eventInfo interface{}) error { c.OnShutdown(func() error {
if eventType == caddy.ShutdownEvent {
return ch.persistAllEdits() return ch.persistAllEdits()
}
return nil
}) })
ch.Site.AddMiddleware(ch.Middleware(c)) ch.Site.AddMiddleware(ch.Middleware(c))

@ -6,3 +6,9 @@ localhost:8080 {
errors { * } errors { * }
pprof pprof
} }
localhost:8081 {
root ./testsite2
hugo
errors { * }
}

@ -0,0 +1,3 @@
baseURL = "http://example.org/"
languageCode = "en-us"
title = "My New Hugo Site"

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>Categories on My New Hugo Site</title>
<link>http://example.org/categories/</link>
<description>Recent content in Categories on My New Hugo Site</description>
<generator>Hugo -- gohugo.io</generator>
<language>en-us</language>
<atom:link href="http://example.org/categories/index.xml" rel="self" type="application/rss+xml" />
</channel>
</rss>

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>My New Hugo Site</title>
<link>http://example.org/</link>
<description>Recent content on My New Hugo Site</description>
<generator>Hugo -- gohugo.io</generator>
<language>en-us</language>
<atom:link href="http://example.org/index.xml" rel="self" type="application/rss+xml" />
</channel>
</rss>

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:xhtml="http://www.w3.org/1999/xhtml">
<url>
<loc>http://example.org/categories/</loc>
<priority>0</priority>
</url>
<url>
<loc>http://example.org/</loc>
<priority>0</priority>
</url>
<url>
<loc>http://example.org/tags/</loc>
<priority>0</priority>
</url>
</urlset>

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>Tags on My New Hugo Site</title>
<link>http://example.org/tags/</link>
<description>Recent content in Tags on My New Hugo Site</description>
<generator>Hugo -- gohugo.io</generator>
<language>en-us</language>
<atom:link href="http://example.org/tags/index.xml" rel="self" type="application/rss+xml" />
</channel>
</rss>
Loading…
Cancel
Save