|
|
@ -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) |
|
|
|