From 4f2ca78111869c360dab406de4f10bd95bf7a52c Mon Sep 17 00:00:00 2001 From: Tim Hemel Date: Sun, 17 May 2020 09:19:35 +0200 Subject: [PATCH 1/2] Add watch for non-existing files, by rendering them with the markdown template. --- handler/filerender.go | 12 ++++++++++++ handler/handlers.go | 13 +++++++++++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/handler/filerender.go b/handler/filerender.go index 4aba9ec..fdd6fc5 100644 --- a/handler/filerender.go +++ b/handler/filerender.go @@ -55,6 +55,18 @@ func renderMarkDown(w http.ResponseWriter, r *http.Request, path string) error { return err } +func renderNotFound(w http.ResponseWriter, r *http.Request, path string) error { + var err error + + err = tmpl.ExecuteTemplate(w, "tmpl/markdown.tmpl", map[string]interface{}{ + "rand": rand.Int(), + "css": flagMdCSS, + "path": path, + "outputHTML": template.HTML("File not found"), + }) + return err +} + func renderFolder(w http.ResponseWriter, r *http.Request, path string) error { res, err := ioutil.ReadDir(path) if err != nil { diff --git a/handler/handlers.go b/handler/handlers.go index 792fb5b..25564d8 100644 --- a/handler/handlers.go +++ b/handler/handlers.go @@ -14,6 +14,7 @@ import ( "path/filepath" "strings" "time" + "errors" "github.com/fsnotify/fsnotify" "github.com/gohxs/httpServe/binAssets" @@ -83,7 +84,10 @@ func fileServe(w http.ResponseWriter, r *http.Request) { fstat, err := os.Stat(path) if err != nil { - webu.WriteStatus(w, http.StatusNotFound) + err := renderNotFound(w, r, path) + if err != nil { + webu.WriteStatus(w, http.StatusInternalServerError, err) + } return } raw := r.URL.Query().Get("raw") @@ -201,7 +205,12 @@ func Watcher(w http.ResponseWriter, r *http.Request) { if err != nil { return err } - err = watcher.Add(absFile) // remove root '/' prefix + // if absFile does not exist, watch parent dir (and do this recursively) + err = errors.New("none") + for err != nil && absFile != ".." { + err = watcher.Add(absFile) // remove root '/' prefix + absFile = filepath.Dir(absFile) + } if err != nil { return fmt.Errorf("Error watching '%s (%s)' -- %s", toWatch, u.Path, err.Error()) } From 8b1767d93d4d800c58c30e17333d4fe32fe2d591 Mon Sep 17 00:00:00 2001 From: Tim Hemel Date: Sun, 17 May 2020 09:25:58 +0200 Subject: [PATCH 2/2] Only render "not found" page for markdown files. --- handler/handlers.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/handler/handlers.go b/handler/handlers.go index 25564d8..1b45c89 100644 --- a/handler/handlers.go +++ b/handler/handlers.go @@ -84,9 +84,13 @@ func fileServe(w http.ResponseWriter, r *http.Request) { fstat, err := os.Stat(path) if err != nil { - err := renderNotFound(w, r, path) - if err != nil { - webu.WriteStatus(w, http.StatusInternalServerError, err) + if filepath.Ext(path) == ".md" { + err := renderNotFound(w, r, path) + if err != nil { + webu.WriteStatus(w, http.StatusInternalServerError, err) + } + } else { + webu.WriteStatus(w, http.StatusNotFound) } return }