Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions handler/filerender.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
17 changes: 15 additions & 2 deletions handler/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"path/filepath"
"strings"
"time"
"errors"

"github.com/fsnotify/fsnotify"
"github.com/gohxs/httpServe/binAssets"
Expand Down Expand Up @@ -83,7 +84,14 @@ func fileServe(w http.ResponseWriter, r *http.Request) {

fstat, err := os.Stat(path)
if err != nil {
webu.WriteStatus(w, http.StatusNotFound)
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
}
raw := r.URL.Query().Get("raw")
Expand Down Expand Up @@ -201,7 +209,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())
}
Expand Down