|
|
|
@ -8,8 +8,6 @@ import ( |
|
|
|
|
"os" |
|
|
|
|
"os/exec" |
|
|
|
|
"path" |
|
|
|
|
"path/filepath" |
|
|
|
|
"strings" |
|
|
|
|
"time" |
|
|
|
|
|
|
|
|
|
"git.stephensearles.com/stephen/acedoc" |
|
|
|
@ -22,93 +20,109 @@ type docref struct { |
|
|
|
|
tmpdir string |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (ch *CaddyHugo) doc(r *http.Request) (*docref, error) { |
|
|
|
|
ch.mtx.Lock() |
|
|
|
|
defer ch.mtx.Unlock() |
|
|
|
|
|
|
|
|
|
name := r.URL.Path[len("/hugo/edit/"):] |
|
|
|
|
name = filepath.Join(ch.Dir, name) |
|
|
|
|
name = strings.ToLower(name) |
|
|
|
|
func (ch *CaddyHugo) newClient(docName string) (*docref, error) { |
|
|
|
|
filename := ch.docFilename(docName) |
|
|
|
|
|
|
|
|
|
_, ok := ch.docs[ch.docname(name)] |
|
|
|
|
if !ok { |
|
|
|
|
fmt.Println("opening", name) |
|
|
|
|
contents, err := ioutil.ReadFile(name) |
|
|
|
|
if err != nil { |
|
|
|
|
return nil, err |
|
|
|
|
} |
|
|
|
|
fmt.Println("opening", filename) |
|
|
|
|
contents, err := ioutil.ReadFile(filename) |
|
|
|
|
if err != nil { |
|
|
|
|
return nil, err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
draftPrefix := fmt.Sprintf("draft-%s", base64.RawURLEncoding.EncodeToString([]byte(name))) |
|
|
|
|
tmpdir := path.Join(os.TempDir(), draftPrefix) |
|
|
|
|
draftPrefix := fmt.Sprintf("draft-%s", base64.RawURLEncoding.EncodeToString([]byte(docName))) |
|
|
|
|
tmpdir := path.Join(os.TempDir(), draftPrefix) |
|
|
|
|
|
|
|
|
|
ref := &docref{ |
|
|
|
|
name: name, |
|
|
|
|
doc: acedoc.NewString(string(contents)), |
|
|
|
|
tmpdir: tmpdir, |
|
|
|
|
} |
|
|
|
|
ref := &docref{ |
|
|
|
|
name: docName, |
|
|
|
|
doc: acedoc.NewString(string(contents)), |
|
|
|
|
tmpdir: tmpdir, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
err = ref.doc.LogToFile(path.Join(ch.Dir, "logs", r.URL.Path[len("/hugo/edit/"):])) |
|
|
|
|
if err != nil { |
|
|
|
|
fmt.Println(err) |
|
|
|
|
return nil, err |
|
|
|
|
} |
|
|
|
|
err = ref.doc.LogToFile(path.Join(ch.Dir, "logs", docName)) |
|
|
|
|
if err != nil { |
|
|
|
|
fmt.Println(err) |
|
|
|
|
return nil, err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
ch.docs[ch.docname(name)] = ref |
|
|
|
|
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) |
|
|
|
|
} |
|
|
|
|
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 |
|
|
|
|
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) |
|
|
|
|
} |
|
|
|
|
}() |
|
|
|
|
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() |
|
|
|
|
for { |
|
|
|
|
<-ticker.C |
|
|
|
|
ch.mtx.Lock() |
|
|
|
|
|
|
|
|
|
err := ioutil.WriteFile(name, []byte(ref.doc.Contents()), 0644) |
|
|
|
|
if err != nil { |
|
|
|
|
fmt.Println("error saving document contents:", err) |
|
|
|
|
} |
|
|
|
|
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, ch.docname(name)) |
|
|
|
|
ch.mtx.Unlock() |
|
|
|
|
return |
|
|
|
|
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) |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
idleTicks = 0 |
|
|
|
|
|
|
|
|
|
ref.doc.Close() |
|
|
|
|
os.RemoveAll(tmpdir) |
|
|
|
|
delete(ch.docs, filename) |
|
|
|
|
ch.mtx.Unlock() |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
ch.mtx.Unlock() |
|
|
|
|
} 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 ch.docs[ch.docname(name)], nil |
|
|
|
|
return dr, err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (ch *CaddyHugo) doc(r *http.Request) (*docref, error) { |
|
|
|
|
name := r.URL.Path[len("/hugo/edit/"):] |
|
|
|
|
return ch.client(name) |
|
|
|
|
} |
|
|
|
|