|
|
|
@ -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)
|
|
|
|
|
ch.Site.AddMiddleware(ch.Middleware(c)) |
|
|
|
|
|
|
|
|
|
return nil |
|
|
|
|
return ch.Publish() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (ch CaddyHugo) Middleware(c *caddy.Controller) httpserver.Middleware { |
|
|
|
|
return func(h httpserver.Handler) httpserver.Handler { |
|
|
|
|
return httpserver.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) { |
|
|
|
|
func (ch *CaddyHugo) ServeHTTPWithNext(next httpserver.Handler, c *caddy.Controller, w http.ResponseWriter, r *http.Request) (int, error) { |
|
|
|
|
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) { |
|
|
|
|
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") { |
|
|
|
|
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 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++ |
|
|
|
|
idleTime := time.Duration(idleTicks) * WebsocketFileTicker |
|
|
|
|
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) |
|
|
|
|
delete(ch.docs, name) |
|
|
|
|
ch.mtx.Unlock() |
|
|
|
@ -306,6 +328,14 @@ func (ch *CaddyHugo) doc(r *http.Request) (*docref, error) { |
|
|
|
|
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) { |
|
|
|
|
var upgrader = websocket.Upgrader{ |
|
|
|
|
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{} { |
|
|
|
|
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} |
|
|
|
|
} |
|
|
|
|