diff options
author | sanine <sanine.not@pm.me> | 2023-01-12 17:29:28 -0600 |
---|---|---|
committer | sanine <sanine.not@pm.me> | 2023-01-12 17:29:28 -0600 |
commit | f9f8a3206298363059601f6c389584426962e5e4 (patch) | |
tree | b0ed2fd393a1064c01aabdce548e9f84215a8c57 /server/md-page.go | |
parent | 655cd79991ec9204afb9f5acf52495c13af14d25 (diff) |
wrong branch :c
Diffstat (limited to 'server/md-page.go')
-rw-r--r-- | server/md-page.go | 95 |
1 files changed, 0 insertions, 95 deletions
diff --git a/server/md-page.go b/server/md-page.go deleted file mode 100644 index 8b378c5..0000000 --- a/server/md-page.go +++ /dev/null @@ -1,95 +0,0 @@ -package main - -import ( - "os" - "strings" - "net/http" - log "github.com/sirupsen/logrus" - md "github.com/russross/blackfriday/v2" -) - - -func ServeForbidden(w http.ResponseWriter, r *http.Request) { - w.WriteHeader(403); - w.Write([]byte("403 forbidden")); -} - - -func ServeNotFound(w http.ResponseWriter, r *http.Request) { - http.NotFound(w, r); -} - - -func RenderMarkdown(path string) ([]byte, error) { - data, err := os.ReadFile(path); - if err != nil { - return []byte{}, err; - } - return md.Run(data), nil; -} - - -func ServeMarkdown(w http.ResponseWriter, r *http.Request, path string) (int, error) { - page, err := RenderMarkdown(path); - if err != nil { - return 404, err; - } - w.WriteHeader(200); - w.Write(page); - return 200, nil; -} - -func ServeFile(w http.ResponseWriter, r *http.Request, path string) (int, error) { - if strings.Contains(r.URL.Path, "..") { - // reject requests with ".." in the URL - return 403, nil; - } - data, err := os.ReadFile(path); - if err != nil { - return 404, err; - } - - w.WriteHeader(200); - w.Write(data); - return 200, nil; -} - - -func IsMarkdown(path string) bool { - return strings.HasSuffix(path, ".md"); -} - - -func Serve(w http.ResponseWriter, r *http.Request, path string) { - var status int; - var err error; - - if IsMarkdown(path) { - // render and serve markdown content - status, err = ServeMarkdown(w, r, path); - } else { - // serve raw file - status, err = ServeFile(w, r, path); - } - - if status == 200 { - log.Infof( - "%v 200\t%v <- %v", - r.Method, r.RemoteAddr, r.URL.Path, - ); - } else { - log.Errorf( - "%v %v\t%v <- %v: %v", - r.Method, status, r.RemoteAddr, r.URL.Path, err, - ); - switch status { - case 403: - ServeForbidden(w, r); - case 404: - ServeNotFound(w, r); - default: - w.WriteHeader(status); - w.Write([]byte("error")); - } - } -} |