package caddyhugo import ( "errors" "fmt" "html/template" "io/ioutil" "net/http" "os" "path" "path/filepath" "strings" "sync" "sync/atomic" "time" "git.stephensearles.com/stephen/acedoc" "github.com/gorilla/websocket" "github.com/mholt/caddy" "github.com/mholt/caddy/caddyhttp/httpserver" ) func init() { plugin := CaddyHugo{} // register a "generic" plugin, like a directive or middleware caddy.RegisterPlugin("hugo", caddy.Plugin{ ServerType: "http", Action: plugin.Setup, }) // ... there are others. See the godoc. } type docref struct { clients uint doc *acedoc.Document } type CaddyHugo struct { ServerType string Site *httpserver.SiteConfig docs map[string]*docref mtx sync.Mutex authorTmpl, adminTmpl, editTmpl *template.Template ltime uint64 } func (ch *CaddyHugo) ObserveLTime(ltime uint64) uint64 { ch.mtx.Lock() defer ch.mtx.Unlock() if ch.ltime < ltime { ch.ltime = ltime } return ch.LTime() } func (ch *CaddyHugo) LTime() uint64 { return atomic.AddUint64(&ch.ltime, 1) } func (ch *CaddyHugo) Setup(c *caddy.Controller) error { ch.docs = make(map[string]*docref) ch.Site = httpserver.GetConfig(c) var err error ch.authorTmpl, err = template.New("").Parse(AuthorPage) if err != nil { return fmt.Errorf("author template invalid: %v", err) } ch.adminTmpl, err = template.New("").Parse(AdminPage) if err != nil { return fmt.Errorf("admin template invalid: %v", err) } ch.editTmpl, err = template.New("").Parse(EditPage) if err != nil { return fmt.Errorf("edit template invalid: %v", err) } // add a function that wraps listeners for the HTTP server // (it's more common for a directive to call this rather than a standalone plugin) ch.Site.AddMiddleware(ch.Middleware(c)) return nil } func (ch CaddyHugo) Middleware(c *caddy.Controller) httpserver.Middleware { return func(h httpserver.Handler) httpserver.Handler { return httpserver.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) { if !ch.Match(r) { return h.ServeHTTP(w, r) } if !ch.Auth(r) { return http.StatusUnauthorized, errors.New("not authorized") } if strings.HasPrefix(r.URL.Path, "/hugo/admin") { return ch.Admin().ServeHTTP(w, r) } if strings.HasPrefix(r.URL.Path, "/hugo/author") { return ch.AuthorHome().ServeHTTP(w, r) } if strings.HasPrefix(r.URL.Path, "/hugo/edit/") { return ch.Edit(c).ServeHTTP(w, r) } return http.StatusNotFound, errors.New("not found") }) } } func (ch CaddyHugo) Auth(r *http.Request) bool { return true } func (ch CaddyHugo) Match(r *http.Request) bool { if r.URL.Path == "/hugo" { return true } return strings.HasPrefix(r.URL.Path, "/hugo/") } func (ch CaddyHugo) BasePath() string { return "/hugo" } func (ch CaddyHugo) Admin() httpserver.Handler { return httpserver.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) { err := ch.adminTmpl.Execute(w, ch.TmplData(r, nil)) if err != nil { fmt.Println(err) return http.StatusInternalServerError, err } return http.StatusOK, nil }) } func (ch CaddyHugo) AuthorHome() httpserver.Handler { return httpserver.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) { err := ch.authorTmpl.Execute(w, ch.TmplData(r, nil)) if err != nil { fmt.Println(err) return http.StatusInternalServerError, err } return http.StatusOK, nil }) } func (ch *CaddyHugo) Edit(c *caddy.Controller) httpserver.Handler { return httpserver.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) { if r.URL.Path == "/hugo/edit/new" { return ch.NewContent(w, r) } if r.Header.Get("Upgrade") == "websocket" { return ch.DeltaWebsocket(w, r) } doc, err := ch.doc(r) if err != nil { fmt.Println(err) return http.StatusNotFound, err } err = ch.editTmpl.Execute(w, ch.TmplData(r, doc.doc)) if err != nil { fmt.Println(err) return http.StatusInternalServerError, err } return http.StatusOK, nil }) } func (ch *CaddyHugo) doc(r *http.Request) (*docref, error) { ch.mtx.Lock() defer ch.mtx.Unlock() name := r.URL.Path[len("/hugo/edit/"):] _, ok := ch.docs[name] if !ok { fmt.Println("opening", name) contents, err := ioutil.ReadFile(name) if err != nil { return nil, err } ref := &docref{doc: acedoc.NewString(string(contents))} ch.docs[name] = ref go func() { ticker := time.NewTicker(1 * time.Minute) idleMinutes := 0 for { <-ticker.C ch.mtx.Lock() fmt.Println("saving", name) err := ioutil.WriteFile(name, []byte(ref.doc.Contents()), 0644) if err != nil { fmt.Println("error saving document contents:", err) } if ref.clients == 0 { idleMinutes++ if idleMinutes > 10 { fmt.Println("idle for", idleMinutes, "minutes, quitting") delete(ch.docs, name) ch.mtx.Unlock() return } fmt.Println("idle for", idleMinutes, "minutes") } else { idleMinutes = 0 } ch.mtx.Unlock() } }() } return ch.docs[name], nil } func (ch *CaddyHugo) DeltaWebsocket(w http.ResponseWriter, r *http.Request) (int, error) { var upgrader = websocket.Upgrader{ ReadBufferSize: 1024, WriteBufferSize: 1024, } conn, err := upgrader.Upgrade(w, r, nil) if err != nil { fmt.Println(err) return http.StatusBadRequest, err } doc, err := ch.doc(r) if err != nil { fmt.Println(err) return http.StatusBadRequest, err } client := doc.doc.Client(acedoc.DeltaHandlerFunc(func(ds []acedoc.Delta) error { err := conn.WriteJSON(Message{ Deltas: ds, LTime: ch.LTime(), }) return err })) ch.mtx.Lock() doc.clients++ ch.mtx.Unlock() defer func() { ch.mtx.Lock() doc.clients-- ch.mtx.Unlock() }() for { var message Message err := conn.ReadJSON(&message) if err != nil { return http.StatusBadRequest, err } ch.ObserveLTime(message.LTime) err = client.PushDeltas(message.Deltas...) if err != nil { return http.StatusBadRequest, err } } } type Message struct { Deltas []acedoc.Delta `json:"deltas"` LTime uint64 `json:"ltime"` } func (ch CaddyHugo) NewContent(w http.ResponseWriter, r *http.Request) (int, error) { name := r.FormValue("name") ctype := r.FormValue("type") if filepath.Ext(name) != ".md" { name += ".md" } filename := path.Join(ch.Site.Root, "content", ctype, name) dir := filepath.Dir(filename) if _, err := os.Stat(dir); err != nil && os.IsNotExist(err) { err = os.MkdirAll(dir, 0755) if err != nil { fmt.Println(err) return http.StatusInternalServerError, err } } // create content f, err := os.Create(filename) if err != nil && !os.IsExist(err) { fmt.Println(err) return http.StatusInternalServerError, err } // we only needed to make the file though err = f.Close() if err != nil { fmt.Println(err) return http.StatusInternalServerError, err } // serve redirect http.Redirect(w, r, filepath.Join("/hugo/edit/", filename), http.StatusFound) return http.StatusFound, nil } func (ch CaddyHugo) TmplData(r *http.Request, doc *acedoc.Document) interface{} { return tmplData{ch.Site, r, ch, doc} }