|
|
@ -1,12 +1,14 @@ |
|
|
|
package caddyhugo |
|
|
|
package caddyhugo |
|
|
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
import ( |
|
|
|
|
|
|
|
"encoding/base64" |
|
|
|
"errors" |
|
|
|
"errors" |
|
|
|
"fmt" |
|
|
|
"fmt" |
|
|
|
"html/template" |
|
|
|
"html/template" |
|
|
|
"io/ioutil" |
|
|
|
"io/ioutil" |
|
|
|
"net/http" |
|
|
|
"net/http" |
|
|
|
"os" |
|
|
|
"os" |
|
|
|
|
|
|
|
"os/exec" |
|
|
|
"path" |
|
|
|
"path" |
|
|
|
"path/filepath" |
|
|
|
"path/filepath" |
|
|
|
"strings" |
|
|
|
"strings" |
|
|
@ -21,6 +23,11 @@ import ( |
|
|
|
"github.com/mholt/caddy/caddyhttp/httpserver" |
|
|
|
"github.com/mholt/caddy/caddyhttp/httpserver" |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const ( |
|
|
|
|
|
|
|
IdleWebsocketTimeout = 10 * time.Minute |
|
|
|
|
|
|
|
WebsocketFileTicker = 1 * time.Second |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
func init() { |
|
|
|
func init() { |
|
|
|
plugin := CaddyHugo{} |
|
|
|
plugin := CaddyHugo{} |
|
|
|
|
|
|
|
|
|
|
@ -35,7 +42,9 @@ func init() { |
|
|
|
|
|
|
|
|
|
|
|
type docref struct { |
|
|
|
type docref struct { |
|
|
|
clients uint |
|
|
|
clients uint |
|
|
|
|
|
|
|
name string |
|
|
|
doc *acedoc.Document |
|
|
|
doc *acedoc.Document |
|
|
|
|
|
|
|
tmpdir string |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
type CaddyHugo struct { |
|
|
|
type CaddyHugo struct { |
|
|
@ -114,6 +123,9 @@ func (ch CaddyHugo) Middleware(c *caddy.Controller) httpserver.Middleware { |
|
|
|
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(c).ServeHTTP(w, r) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if strings.HasPrefix(r.URL.Path, "/hugo/draft/") { |
|
|
|
|
|
|
|
return ch.serveDraft(w, r) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return http.StatusNotFound, errors.New("not found") |
|
|
|
return http.StatusNotFound, errors.New("not found") |
|
|
|
}) |
|
|
|
}) |
|
|
@ -177,7 +189,7 @@ func (ch *CaddyHugo) Edit(c *caddy.Controller) httpserver.Handler { |
|
|
|
return http.StatusNotFound, err |
|
|
|
return http.StatusNotFound, err |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
err = ch.editTmpl.Execute(w, ch.TmplData(r, doc.doc)) |
|
|
|
err = ch.editTmpl.Execute(w, ch.TmplData(r, doc)) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
|
|
|
|
|
|
|
|
fmt.Println(err) |
|
|
|
fmt.Println(err) |
|
|
@ -188,6 +200,44 @@ func (ch *CaddyHugo) Edit(c *caddy.Controller) httpserver.Handler { |
|
|
|
}) |
|
|
|
}) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (ch *CaddyHugo) serveDraft(w http.ResponseWriter, r *http.Request) (int, error) { |
|
|
|
|
|
|
|
pathSegments := strings.SplitN(r.URL.Path, "/", 5) |
|
|
|
|
|
|
|
if len(pathSegments) < 4 { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return http.StatusNotFound, nil |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
encoded := pathSegments[3] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
nameBytes, err := base64.RawURLEncoding.DecodeString(encoded) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return http.StatusNotFound, err |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
name := string(nameBytes) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ch.mtx.Lock() |
|
|
|
|
|
|
|
defer ch.mtx.Unlock() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
docref, ok := ch.docs[name] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if !ok { |
|
|
|
|
|
|
|
fmt.Println("DRAFT", name, ch.docs) |
|
|
|
|
|
|
|
return http.StatusNotFound, fmt.Errorf("draft not found") |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
filename := path.Join(docref.tmpdir, name) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_, err = os.Stat(filename) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
fmt.Println("DRAFT", filename) |
|
|
|
|
|
|
|
return http.StatusNotFound, fmt.Errorf("draft not found") |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
http.FileServer(http.Dir(docref.tmpdir)).ServeHTTP(w, r) |
|
|
|
|
|
|
|
return 200, nil |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (ch *CaddyHugo) doc(r *http.Request) (*docref, error) { |
|
|
|
func (ch *CaddyHugo) doc(r *http.Request) (*docref, error) { |
|
|
|
ch.mtx.Lock() |
|
|
|
ch.mtx.Lock() |
|
|
|
defer ch.mtx.Unlock() |
|
|
|
defer ch.mtx.Unlock() |
|
|
@ -202,12 +252,19 @@ func (ch *CaddyHugo) doc(r *http.Request) (*docref, error) { |
|
|
|
return nil, err |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
ref := &docref{doc: acedoc.NewString(string(contents))} |
|
|
|
draftPrefix := fmt.Sprintf("draft-%s", base64.RawURLEncoding.EncodeToString([]byte(name))) |
|
|
|
|
|
|
|
tmpdir := path.Join(os.TempDir(), draftPrefix) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ref := &docref{ |
|
|
|
|
|
|
|
name: name, |
|
|
|
|
|
|
|
doc: acedoc.NewString(string(contents)), |
|
|
|
|
|
|
|
tmpdir: tmpdir, |
|
|
|
|
|
|
|
} |
|
|
|
ch.docs[name] = ref |
|
|
|
ch.docs[name] = ref |
|
|
|
|
|
|
|
|
|
|
|
go func() { |
|
|
|
go func() { |
|
|
|
ticker := time.NewTicker(1 * time.Minute) |
|
|
|
ticker := time.NewTicker(WebsocketFileTicker) |
|
|
|
idleMinutes := 0 |
|
|
|
idleTicks := 0 |
|
|
|
|
|
|
|
|
|
|
|
for { |
|
|
|
for { |
|
|
|
<-ticker.C |
|
|
|
<-ticker.C |
|
|
@ -219,17 +276,25 @@ func (ch *CaddyHugo) doc(r *http.Request) (*docref, error) { |
|
|
|
fmt.Println("error saving document contents:", err) |
|
|
|
fmt.Println("error saving document contents:", err) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fmt.Println("rendering in", ref.tmpdir) |
|
|
|
|
|
|
|
cmd := exec.Command("hugo", "-D", "-d", ref.tmpdir) |
|
|
|
|
|
|
|
cmd.Dir = ch.Site.Root |
|
|
|
|
|
|
|
if out, err := cmd.CombinedOutput(); err != nil { |
|
|
|
|
|
|
|
fmt.Println("error rendering draft:", err, "output:", string(out)) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if ref.clients == 0 { |
|
|
|
if ref.clients == 0 { |
|
|
|
idleMinutes++ |
|
|
|
idleTicks++ |
|
|
|
if idleMinutes > 10 { |
|
|
|
idleTime := time.Duration(idleTicks) * WebsocketFileTicker |
|
|
|
fmt.Println("idle for", idleMinutes, "minutes, quitting") |
|
|
|
if idleTime >= IdleWebsocketTimeout { |
|
|
|
|
|
|
|
fmt.Printf("idle for %v, quitting\n", idleTime) |
|
|
|
|
|
|
|
os.RemoveAll(tmpdir) |
|
|
|
delete(ch.docs, name) |
|
|
|
delete(ch.docs, name) |
|
|
|
ch.mtx.Unlock() |
|
|
|
ch.mtx.Unlock() |
|
|
|
return |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
fmt.Println("idle for", idleMinutes, "minutes") |
|
|
|
|
|
|
|
} else { |
|
|
|
} else { |
|
|
|
idleMinutes = 0 |
|
|
|
idleTicks = 0 |
|
|
|
} |
|
|
|
} |
|
|
|
ch.mtx.Unlock() |
|
|
|
ch.mtx.Unlock() |
|
|
|
} |
|
|
|
} |
|
|
@ -304,36 +369,21 @@ func (ch CaddyHugo) NewContent(w http.ResponseWriter, r *http.Request) (int, err |
|
|
|
name += ".md" |
|
|
|
name += ".md" |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
filename := path.Join(ch.Site.Root, "content", ctype, name) |
|
|
|
filename := path.Join(ctype, name) |
|
|
|
|
|
|
|
|
|
|
|
dir := filepath.Dir(filename) |
|
|
|
|
|
|
|
if _, err := os.Stat(dir); err != nil && os.IsNotExist(err) { |
|
|
|
|
|
|
|
err = os.MkdirAll(dir, 0755) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
fmt.Println(err) |
|
|
|
|
|
|
|
return http.StatusInternalServerError, err |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// create content
|
|
|
|
|
|
|
|
f, err := os.Create(filename) |
|
|
|
|
|
|
|
if err != nil && !os.IsExist(err) { |
|
|
|
|
|
|
|
fmt.Println(err) |
|
|
|
|
|
|
|
return http.StatusInternalServerError, err |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// we only needed to make the file though
|
|
|
|
cmd := exec.Command("hugo", "new", filename) |
|
|
|
err = f.Close() |
|
|
|
cmd.Dir = ch.Site.Root |
|
|
|
|
|
|
|
out, err := cmd.CombinedOutput() |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
fmt.Println(err) |
|
|
|
fmt.Println("error running hugo:\n", string(out)) |
|
|
|
return http.StatusInternalServerError, err |
|
|
|
return http.StatusInternalServerError, err |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// serve redirect
|
|
|
|
// serve redirect
|
|
|
|
http.Redirect(w, r, filepath.Join("/hugo/edit/", filename), http.StatusFound) |
|
|
|
http.Redirect(w, r, filepath.Join("/hugo/edit/", ch.Site.Root, "content", filename), http.StatusFound) |
|
|
|
return http.StatusFound, nil |
|
|
|
return http.StatusFound, nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (ch CaddyHugo) TmplData(r *http.Request, doc *acedoc.Document) interface{} { |
|
|
|
func (ch CaddyHugo) TmplData(r *http.Request, docref *docref) interface{} { |
|
|
|
return tmplData{ch.Site, r, ch, doc} |
|
|
|
return tmplData{ch.Site, r, ch, docref.doc, docref} |
|
|
|
} |
|
|
|
} |
|
|
|