package caddyhugo import ( "encoding/base64" "fmt" "io/ioutil" "net/http" "os" "os/exec" "path" "time" "git.stephensearles.com/stephen/acedoc" ) type docref struct { clients uint name string doc *acedoc.Document tmpdir string } func (ch *CaddyHugo) newClient(docName string) (*docref, error) { filename := ch.docFilename(docName) fmt.Println("opening", filename) contents, err := ioutil.ReadFile(filename) if err != nil { return nil, err } draftPrefix := fmt.Sprintf("draft-%s", base64.RawURLEncoding.EncodeToString([]byte(docName))) tmpdir := path.Join(os.TempDir(), draftPrefix) ref := &docref{ name: docName, doc: acedoc.NewString(string(contents)), tmpdir: tmpdir, } err = ref.doc.LogToFile(path.Join(ch.Dir, "logs", docName)) if err != nil { fmt.Println(err) return nil, err } ch.docs[filename] = ref hugoCmd := exec.Command("hugo", "--watch", "-D", "-d", ref.tmpdir) hugoCmd.Dir = ch.Dir err = hugoCmd.Start() if err != nil { return nil, fmt.Errorf("error starting hugo: %v", err) } go func() { ticker := time.NewTicker(WebsocketFileTicker) idleTicks := 0 defer func() { err := hugoCmd.Process.Signal(os.Interrupt) if err != nil { fmt.Println("error signaling to hugo:", err) } err = hugoCmd.Wait() if err != nil { fmt.Println("error waiting for hugo:", err) } }() for { <-ticker.C ch.mtx.Lock() err := ioutil.WriteFile(filename, []byte(ref.doc.Contents()), 0644) if err != nil { fmt.Println("error saving document contents:", err) } if ref.clients == 0 { idleTicks++ idleTime := time.Duration(idleTicks) * WebsocketFileTicker if idleTime >= IdleWebsocketTimeout { err := ch.Publish() fmt.Printf("idle for %v, quitting\n", idleTime) if err != nil { fmt.Printf(", error publishing: %v\n", err) } ref.doc.Close() os.RemoveAll(tmpdir) delete(ch.docs, filename) ch.mtx.Unlock() return } } else { idleTicks = 0 } ch.mtx.Unlock() } }() return ref, nil } func (ch *CaddyHugo) hasdocref(docName string) (*docref, bool) { dr, ok := ch.docs[ch.docFilename(docName)] return dr, ok } func (ch *CaddyHugo) client(docName string) (*docref, error) { ch.mtx.Lock() defer ch.mtx.Unlock() var err error dr, ok := ch.hasdocref(docName) if !ok { dr, err = ch.newClient(docName) } return dr, err } func (ch *CaddyHugo) doc(r *http.Request) (*docref, error) { name := r.URL.Path[len("/hugo/edit/"):] return ch.client(name) }