package caddyhugo import ( "encoding/base64" "fmt" "net" "net/http" "os" "path" "path/filepath" "strings" "git.stephensearles.com/stephen/caddy-hugo2/assets" "github.com/mholt/caddy" "github.com/mholt/caddy/caddyhttp/httpserver" "github.com/spf13/afero" ) func (ch *CaddyHugo) ServeHTTPWithNext(next httpserver.Handler, w http.ResponseWriter, r *http.Request) (int, error) { if !ch.Match(r) { p := path.Join(ch.Dir, "public", r.URL.Path) http.ServeFile(w, r, p) return 200, nil } if ch.Comments != nil && strings.HasSuffix(r.URL.Path, "/comments") { docName := docNameFromCommentRequest(r) err := ch.Comments.ServeComments(docName, w, r) if err != nil { return 500, fmt.Errorf("couldn't load comments:", err) } return 200, nil } if r.URL.Path == "/login" { return ch.commentsLogin(r, w) } if strings.HasPrefix(r.URL.Path, "/hugo/publish") { err := ch.Publish() if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return http.StatusInternalServerError, nil } http.Redirect(w, r, "/", http.StatusFound) return http.StatusFound, nil } if strings.HasPrefix(r.URL.Path, "/hugo/simplemde.css") { w.Write(assets.MustAsset("simplemde/dist/simplemde.min.css")) return http.StatusOK, nil } if strings.HasPrefix(r.URL.Path, "/hugo/simplemde.js") { w.Write(assets.MustAsset("simplemde/debug/simplemde.js")) return http.StatusOK, nil } if strings.HasPrefix(r.URL.Path, "/hugo/vue.js") { w.Write(assets.MustAsset("js/vue.js")) return http.StatusOK, nil } if strings.HasPrefix(r.URL.Path, "/hugo/moment.js") { w.Write(assets.MustAsset("js/moment.js")) return http.StatusOK, nil } if strings.HasPrefix(r.URL.Path, "/hugo/font-awesome.css") { w.Write(assets.MustAsset("css/font-awesome.min.css")) return http.StatusOK, nil } 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().ServeHTTP(w, r) } if strings.HasPrefix(r.URL.Path, "/hugo/draft/") { return ch.serveDraft(w, r) } if strings.HasPrefix(r.URL.Path, "/hugo/media") { return ch.serveMediaPage(w, r) } if strings.HasPrefix(r.URL.Path, "/hugo/upload") { return ch.uploadMedia(w, r) } if strings.HasPrefix(r.URL.Path, "/media/") { return ch.serveMedia(w, r) } return next.ServeHTTP(w, r) } func (ch *CaddyHugo) ServeNewContent(w http.ResponseWriter, r *http.Request) (int, error) { name := r.FormValue("name") ctype := r.FormValue("type") filename, err := ch.NewContent(name, ctype) if err != nil { fmt.Println("error creating new content:", err) return http.StatusInternalServerError, err } // serve redirect http.Redirect(w, r, filepath.Join("/hugo/edit/", "content", filename), http.StatusFound) return http.StatusFound, nil } func (ch *CaddyHugo) Middleware(c *caddy.Controller) httpserver.Middleware { return func(next httpserver.Handler) httpserver.Handler { host := ch.Site.Addr.Host hostport := net.JoinHostPort(ch.Site.Addr.Host, ch.Site.Addr.Port) return httpserver.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) { if r.Host != host && r.Host != hostport { return next.ServeHTTP(w, r) } return ch.ServeHTTPWithNext(next, w, r) }) } } func (ch *CaddyHugo) Auth(r *http.Request) bool { // this is handled upstream by the caddy configuration return true } func (ch *CaddyHugo) Match(r *http.Request) bool { host := ch.Site.Addr.Host hostport := net.JoinHostPort(ch.Site.Addr.Host, ch.Site.Addr.Port) if r.Host != host && r.Host != hostport { return false } if strings.HasPrefix(r.URL.Path, "/media/") { return true } if strings.HasSuffix(r.URL.Path, "/comments") { return true } if r.URL.Path == "/login" { return true } if r.URL.Path == "/hugo" { return true } return strings.HasPrefix(r.URL.Path, "/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) { td := ch.TmplData(r, nil) err := ch.authorTmpl.Execute(w, td) if err != nil { fmt.Println(err) return http.StatusInternalServerError, err } return http.StatusOK, nil }) } func (ch *CaddyHugo) Edit() httpserver.Handler { return httpserver.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) { if r.URL.Path == "/hugo/edit/new" { return ch.ServeNewContent(w, r) } if r.Header.Get("Upgrade") == "websocket" { return ch.DeltaWebsocket(w, r) } doc, err := ch.editSession(docNameFromEditRequest(r)) if err != nil { fmt.Println(err) return http.StatusNotFound, err } err = ch.editTmpl.Execute(w, ch.TmplData(r, doc)) if err != nil { fmt.Println(err) return http.StatusInternalServerError, err } return http.StatusOK, nil }) } func (ch *CaddyHugo) serveDraft(w http.ResponseWriter, r *http.Request) (int, error) { pathSegments := strings.SplitN(r.URL.Path, "/", 5) if len(pathSegments) < 4 { return http.StatusNotFound, nil } encoded := pathSegments[3] nameBytes, err := base64.RawURLEncoding.DecodeString(encoded) if err != nil { return http.StatusNotFound, err } name := string(nameBytes) ch.mtx.Lock() defer ch.mtx.Unlock() docref, ok := ch.docs[ch.docFilename(name)] if !ok { return http.StatusNotFound, fmt.Errorf("draft not found") } r.URL.Path = strings.ToLower(r.URL.Path) prefix := "/hugo/draft/" + encoded r.URL.Path = path.Join("public", r.URL.Path[len(prefix):]) page := ch.HugoSites.GetContentPage(ch.docFilename(name)) if page == nil { fmt.Fprintf(w, "can't find %q to display a draft", name) return 404, nil } r.URL.Path = page.RelPermalink() http.FileServer(aferoHTTP{afero.NewBasePathFs(docref.tmpfs, path.Join(ch.Dir, "public"))}).ServeHTTP(w, r) return 200, nil } type aferoHTTP struct { afero.Fs } func (a aferoHTTP) Open(name string) (http.File, error) { af, err := a.Fs.Open(name) if os.IsExist(err) { err = os.ErrExist } if os.IsNotExist(err) { err = os.ErrNotExist } return af, err } func (ch *CaddyHugo) commentsLogin(r *http.Request, w http.ResponseWriter) (int, error) { if ch.Comments == nil { return 200, nil } _, ok := ch.Comments.User(r) if !ok { w.Header().Set("WWW-Authenticate", `Basic realm="Log in with your name and the password. Ask Dan or Stephen for the password."`) w.WriteHeader(401) fmt.Fprintf(w, "Log in with your name and the password. Ask Dan or Stephen for the password. go back", r.Referer()) return 200, nil } http.Redirect(w, r, r.Referer(), http.StatusFound) return 200, nil }