serving published content

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

@ -100,34 +100,51 @@ 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 { if !ch.Match(r) {
return httpserver.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) { p := path.Join(ch.Site.Root, "public", r.URL.Path)
if !ch.Match(r) { http.ServeFile(w, r, p)
return h.ServeHTTP(w, r) 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/admin") { if strings.HasPrefix(r.URL.Path, "/hugo/publish") {
return ch.Admin().ServeHTTP(w, r) err := ch.Publish()
} if err != nil {
if strings.HasPrefix(r.URL.Path, "/hugo/author") { http.Error(w, err.Error(), http.StatusInternalServerError)
return ch.AuthorHome().ServeHTTP(w, r) return http.StatusInternalServerError, nil
} }
if strings.HasPrefix(r.URL.Path, "/hugo/edit/") {
return ch.Edit(c).ServeHTTP(w, r) http.Redirect(w, r, "/", http.StatusFound)
} return http.StatusFound, nil
if strings.HasPrefix(r.URL.Path, "/hugo/draft/") { }
return ch.serveDraft(w, r) if strings.HasPrefix(r.URL.Path, "/hugo/admin") {
} return ch.Admin().ServeHTTP(w, r)
}
if strings.HasPrefix(r.URL.Path, "/hugo/author") {
return ch.AuthorHome().ServeHTTP(w, r)
}
if strings.HasPrefix(r.URL.Path, "/hugo/edit/") {
return ch.Edit(c).ServeHTTP(w, r)
}
if strings.HasPrefix(r.URL.Path, "/hugo/draft/") {
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