serving published content

pull/8/head
Stephen Searles 8 years ago
parent daf73bc534
commit 10600fbbda
  1. 50
      caddyhugo.go

@ -100,20 +100,30 @@ func (ch *CaddyHugo) Setup(c *caddy.Controller) error {
// (it's more common for a directive to call this rather than a standalone plugin) // (it's more common for a directive to call this rather than a standalone plugin)
ch.Site.AddMiddleware(ch.Middleware(c)) ch.Site.AddMiddleware(ch.Middleware(c))
return nil return ch.Publish()
} }
func (ch CaddyHugo) Middleware(c *caddy.Controller) httpserver.Middleware { func (ch *CaddyHugo) ServeHTTPWithNext(next httpserver.Handler, c *caddy.Controller, w http.ResponseWriter, r *http.Request) (int, error) {
return func(h httpserver.Handler) httpserver.Handler {
return httpserver.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) {
if !ch.Match(r) { if !ch.Match(r) {
return h.ServeHTTP(w, r) p := path.Join(ch.Site.Root, "public", r.URL.Path)
http.ServeFile(w, r, p)
return 200, nil
} }
if !ch.Auth(r) { if !ch.Auth(r) {
return http.StatusUnauthorized, errors.New("not authorized") return http.StatusUnauthorized, errors.New("not authorized")
} }
if strings.HasPrefix(r.URL.Path, "/hugo/publish") {
err := ch.Publish()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return http.StatusInternalServerError, nil
}
http.Redirect(w, r, "/", http.StatusFound)
return http.StatusFound, nil
}
if strings.HasPrefix(r.URL.Path, "/hugo/admin") { if strings.HasPrefix(r.URL.Path, "/hugo/admin") {
return ch.Admin().ServeHTTP(w, r) return ch.Admin().ServeHTTP(w, r)
} }
@ -127,7 +137,14 @@ func (ch CaddyHugo) Middleware(c *caddy.Controller) httpserver.Middleware {
return ch.serveDraft(w, r) return ch.serveDraft(w, r)
} }
return http.StatusNotFound, errors.New("not found") http.NotFound(w, r)
return 404, nil
}
func (ch CaddyHugo) Middleware(c *caddy.Controller) httpserver.Middleware {
return func(next httpserver.Handler) httpserver.Handler {
return httpserver.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) {
return ch.ServeHTTPWithNext(next, c, w, r)
}) })
} }
} }
@ -289,7 +306,12 @@ func (ch *CaddyHugo) doc(r *http.Request) (*docref, error) {
idleTicks++ idleTicks++
idleTime := time.Duration(idleTicks) * WebsocketFileTicker idleTime := time.Duration(idleTicks) * WebsocketFileTicker
if idleTime >= IdleWebsocketTimeout { if idleTime >= IdleWebsocketTimeout {
fmt.Printf("idle for %v, quitting\n", idleTime) err := ch.Publish()
fmt.Printf("idle for %v, quitting", idleTime)
if err != nil {
fmt.Printf(", error publishing: %v\n", err)
}
os.RemoveAll(tmpdir) os.RemoveAll(tmpdir)
delete(ch.docs, name) delete(ch.docs, name)
ch.mtx.Unlock() ch.mtx.Unlock()
@ -306,6 +328,14 @@ func (ch *CaddyHugo) doc(r *http.Request) (*docref, error) {
return ch.docs[name], nil return ch.docs[name], nil
} }
func (ch *CaddyHugo) Publish() error {
cmd := exec.Command("hugo")
cmd.Dir = ch.Site.Root
_, err := cmd.CombinedOutput()
return err
}
func (ch *CaddyHugo) DeltaWebsocket(w http.ResponseWriter, r *http.Request) (int, error) { func (ch *CaddyHugo) DeltaWebsocket(w http.ResponseWriter, r *http.Request) (int, error) {
var upgrader = websocket.Upgrader{ var upgrader = websocket.Upgrader{
ReadBufferSize: 1024, ReadBufferSize: 1024,
@ -387,5 +417,9 @@ func (ch CaddyHugo) NewContent(w http.ResponseWriter, r *http.Request) (int, err
} }
func (ch CaddyHugo) TmplData(r *http.Request, docref *docref) interface{} { func (ch CaddyHugo) TmplData(r *http.Request, docref *docref) interface{} {
return tmplData{ch.Site, r, ch, docref.doc, docref} var doc *acedoc.Document
if docref != nil {
doc = docref.doc
}
return tmplData{ch.Site, r, ch, doc, docref}
} }

Loading…
Cancel
Save