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