|
|
@ -14,10 +14,11 @@ import ( |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
type editSession struct { |
|
|
|
type editSession struct { |
|
|
|
clients uint |
|
|
|
clients uint |
|
|
|
name string |
|
|
|
docname string |
|
|
|
doc *acedoc.Document |
|
|
|
filename string |
|
|
|
tmpdir string |
|
|
|
doc *acedoc.Document |
|
|
|
|
|
|
|
tmpdir string |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (ch *CaddyHugo) newEditSession(docName string) (*editSession, error) { |
|
|
|
func (ch *CaddyHugo) newEditSession(docName string) (*editSession, error) { |
|
|
@ -32,25 +33,32 @@ func (ch *CaddyHugo) newEditSession(docName string) (*editSession, error) { |
|
|
|
draftPrefix := fmt.Sprintf("draft-%s", base64.RawURLEncoding.EncodeToString([]byte(docName))) |
|
|
|
draftPrefix := fmt.Sprintf("draft-%s", base64.RawURLEncoding.EncodeToString([]byte(docName))) |
|
|
|
tmpdir := path.Join(os.TempDir(), draftPrefix) |
|
|
|
tmpdir := path.Join(os.TempDir(), draftPrefix) |
|
|
|
|
|
|
|
|
|
|
|
ref := &editSession{ |
|
|
|
es := &editSession{ |
|
|
|
name: docName, |
|
|
|
docname: docName, |
|
|
|
doc: acedoc.NewString(string(contents)), |
|
|
|
filename: filename, |
|
|
|
tmpdir: tmpdir, |
|
|
|
doc: acedoc.NewString(string(contents)), |
|
|
|
|
|
|
|
tmpdir: tmpdir, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
err = ref.doc.LogToFile(path.Join(ch.Dir, "logs", docName)) |
|
|
|
err = es.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[filename] = ref |
|
|
|
ch.docs[filename] = es |
|
|
|
|
|
|
|
|
|
|
|
hugoCmd := exec.Command("hugo", "--watch", "-D", "-d", ref.tmpdir) |
|
|
|
ch.renderDraft(es) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return es, nil |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (ch *CaddyHugo) renderDraft(es *editSession) error { |
|
|
|
|
|
|
|
hugoCmd := exec.Command("hugo", "--watch", "-D", "-d", es.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 fmt.Errorf("error starting hugo: %v", err) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
go func() { |
|
|
|
go func() { |
|
|
@ -72,12 +80,12 @@ func (ch *CaddyHugo) newEditSession(docName string) (*editSession, error) { |
|
|
|
<-ticker.C |
|
|
|
<-ticker.C |
|
|
|
ch.mtx.Lock() |
|
|
|
ch.mtx.Lock() |
|
|
|
|
|
|
|
|
|
|
|
err := ioutil.WriteFile(filename, []byte(ref.doc.Contents()), 0644) |
|
|
|
err := ioutil.WriteFile(es.filename, []byte(es.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 es.clients == 0 { |
|
|
|
idleTicks++ |
|
|
|
idleTicks++ |
|
|
|
idleTime := time.Duration(idleTicks) * WebsocketFileTicker |
|
|
|
idleTime := time.Duration(idleTicks) * WebsocketFileTicker |
|
|
|
if idleTime >= IdleWebsocketTimeout { |
|
|
|
if idleTime >= IdleWebsocketTimeout { |
|
|
@ -87,11 +95,13 @@ func (ch *CaddyHugo) newEditSession(docName string) (*editSession, error) { |
|
|
|
fmt.Printf(", error publishing: %v\n", err) |
|
|
|
fmt.Printf(", error publishing: %v\n", err) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
ref.doc.Close() |
|
|
|
es.doc.Close() |
|
|
|
os.RemoveAll(tmpdir) |
|
|
|
os.RemoveAll(es.tmpdir) |
|
|
|
delete(ch.docs, filename) |
|
|
|
delete(ch.docs, es.filename) |
|
|
|
ch.mtx.Unlock() |
|
|
|
ch.mtx.Unlock() |
|
|
|
return |
|
|
|
return |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
fmt.Println("idle for", idleTime) |
|
|
|
} |
|
|
|
} |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
idleTicks = 0 |
|
|
|
idleTicks = 0 |
|
|
@ -100,7 +110,7 @@ func (ch *CaddyHugo) newEditSession(docName string) (*editSession, error) { |
|
|
|
} |
|
|
|
} |
|
|
|
}() |
|
|
|
}() |
|
|
|
|
|
|
|
|
|
|
|
return ref, nil |
|
|
|
return nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (ch *CaddyHugo) hasEditSession(docName string) (*editSession, bool) { |
|
|
|
func (ch *CaddyHugo) hasEditSession(docName string) (*editSession, bool) { |
|
|
|