diff --git a/caddyhugo.go b/caddyhugo.go index de60830..d7bd9ac 100644 --- a/caddyhugo.go +++ b/caddyhugo.go @@ -41,7 +41,7 @@ type CaddyHugo struct { Media *MediaSource - docs map[string]*docref + docs map[string]*editSession mtx sync.Mutex authorTmpl, adminTmpl, editTmpl *template.Template @@ -86,7 +86,7 @@ func (ch *CaddyHugo) Publish() error { } // TmplData collects data for template execution -func (ch *CaddyHugo) TmplData(r *http.Request, docref *docref) interface{} { +func (ch *CaddyHugo) TmplData(r *http.Request, docref *editSession) interface{} { var doc *acedoc.Document if docref != nil { doc = docref.doc diff --git a/client.go b/client.go index bc00621..e004083 100644 --- a/client.go +++ b/client.go @@ -13,14 +13,14 @@ import ( "git.stephensearles.com/stephen/acedoc" ) -type docref struct { +type editSession struct { clients uint name string doc *acedoc.Document tmpdir string } -func (ch *CaddyHugo) newClient(docName string) (*docref, error) { +func (ch *CaddyHugo) newEditSession(docName string) (*editSession, error) { filename := ch.docFilename(docName) fmt.Println("opening", filename) @@ -32,7 +32,7 @@ func (ch *CaddyHugo) newClient(docName string) (*docref, error) { draftPrefix := fmt.Sprintf("draft-%s", base64.RawURLEncoding.EncodeToString([]byte(docName))) tmpdir := path.Join(os.TempDir(), draftPrefix) - ref := &docref{ + ref := &editSession{ name: docName, doc: acedoc.NewString(string(contents)), tmpdir: tmpdir, @@ -103,26 +103,25 @@ func (ch *CaddyHugo) newClient(docName string) (*docref, error) { return ref, nil } -func (ch *CaddyHugo) hasdocref(docName string) (*docref, bool) { +func (ch *CaddyHugo) hasEditSession(docName string) (*editSession, bool) { dr, ok := ch.docs[ch.docFilename(docName)] return dr, ok } -func (ch *CaddyHugo) client(docName string) (*docref, error) { +func (ch *CaddyHugo) editSession(docName string) (*editSession, error) { ch.mtx.Lock() defer ch.mtx.Unlock() var err error - dr, ok := ch.hasdocref(docName) + dr, ok := ch.hasEditSession(docName) if !ok { - dr, err = ch.newClient(docName) + dr, err = ch.newEditSession(docName) } return dr, err } -func (ch *CaddyHugo) doc(r *http.Request) (*docref, error) { - name := r.URL.Path[len("/hugo/edit/"):] - return ch.client(name) +func docNameFromEditRequest(r *http.Request) string { + return r.URL.Path[len("/hugo/edit/"):] } diff --git a/deltas.go b/deltas.go index afe47fd..7ab91fd 100644 --- a/deltas.go +++ b/deltas.go @@ -53,13 +53,13 @@ func (ch *CaddyHugo) DeltaWebsocket(w http.ResponseWriter, r *http.Request) (int conn.SetReadDeadline(time.Time{}) - doc, err := ch.doc(r) + doc, err := ch.editSession(docNameFromEditRequest(r)) if err != nil { fmt.Println(err) return http.StatusBadRequest, err } - return ch.handleConn(conn, doc) + return ch.handleDeltaConn(conn, doc) } func (ch *CaddyHugo) Message(deltas ...acedoc.Delta) Message { @@ -69,7 +69,7 @@ func (ch *CaddyHugo) Message(deltas ...acedoc.Delta) Message { } } -func (ch *CaddyHugo) handleConn(conn DeltaConn, doc *docref) (int, error) { +func (ch *CaddyHugo) handleDeltaConn(conn DeltaConn, doc *editSession) (int, error) { const idlePing = 15 * time.Second const idlePingShort = 1 * time.Millisecond diff --git a/doc_test.go b/doc_test.go index 12d15d7..fd72a33 100644 --- a/doc_test.go +++ b/doc_test.go @@ -62,7 +62,7 @@ func TestEdits(t *testing.T) { var mtx sync.Mutex received := []acedoc.Delta{} - doc, err := w.CH.client(contentPath) + doc, err := w.CH.editSession(contentPath) if err != nil { t.Fatal("error creating document client:", err) } @@ -75,7 +75,7 @@ func TestEdits(t *testing.T) { return nil })) - _, ok := w.CH.hasdocref(contentPath) + _, ok := w.CH.hasEditSession(contentPath) if !ok { t.Fatal("expected there to be an established client") } @@ -158,12 +158,12 @@ func TestDeltasSingle(t *testing.T) { client := new(WebsocketTester) - doc, err := w.CH.client("content/" + title + ".md") + doc, err := w.CH.editSession("content/" + title + ".md") if err != nil { t.Fatal("couldn't establish docref for client 0:", err) } - go w.CH.handleConn(client, doc) + go w.CH.handleDeltaConn(client, doc) a := acedoc.Insert(0, 0, "a") @@ -199,13 +199,13 @@ func TestDeltasDouble(t *testing.T) { clientA := new(WebsocketTester) clientB := new(WebsocketTester) - doc, err := w.CH.client("content/" + title + ".md") + doc, err := w.CH.editSession("content/" + title + ".md") if err != nil { t.Fatal("couldn't establish docref for client 0:", err) } - go w.CH.handleConn(clientA, doc) - go w.CH.handleConn(clientB, doc) + go w.CH.handleDeltaConn(clientA, doc) + go w.CH.handleDeltaConn(clientB, doc) // send the first message, simulating the browser on clientA clientA.ReceiveJSON(w.CH.Message(acedoc.Insert(0, 0, "a"))) @@ -266,14 +266,14 @@ func TestDeltasMulti(t *testing.T) { clients := []*WebsocketTester{{}, {}, {}} - doc, err := w.CH.client("content/" + title + ".md") + doc, err := w.CH.editSession("content/" + title + ".md") if err != nil { t.Fatal("couldn't establish docref:", err) } - go w.CH.handleConn(clients[0], doc) - go w.CH.handleConn(clients[1], doc) - go w.CH.handleConn(clients[2], doc) + go w.CH.handleDeltaConn(clients[0], doc) + go w.CH.handleDeltaConn(clients[1], doc) + go w.CH.handleDeltaConn(clients[2], doc) a := acedoc.Insert(0, 0, "a") b := acedoc.Insert(0, 0, "b") diff --git a/http.go b/http.go index 0549833..2a17c96 100644 --- a/http.go +++ b/http.go @@ -155,7 +155,7 @@ func (ch *CaddyHugo) Edit(c *caddy.Controller) httpserver.Handler { return ch.DeltaWebsocket(w, r) } - doc, err := ch.doc(r) + doc, err := ch.editSession(docNameFromEditRequest(r)) if err != nil { fmt.Println(err) return http.StatusNotFound, err diff --git a/setup.go b/setup.go index 7c65788..af4354f 100644 --- a/setup.go +++ b/setup.go @@ -25,7 +25,7 @@ func (ch *CaddyHugo) Setup(dir string) error { var err error ch.Dir = dir - ch.docs = make(map[string]*docref) + ch.docs = make(map[string]*editSession) ch.HugoCfg = &deps.DepsCfg{} diff --git a/templates.go b/templates.go index a4f3c49..851c611 100644 --- a/templates.go +++ b/templates.go @@ -67,7 +67,7 @@ type tmplData struct { R *http.Request *CaddyHugo Doc *acedoc.Document - docref *docref + docref *editSession } func (t *tmplData) LoadContent() (string, error) {