From c8e09a989dc607e6cd1fa6cb088d8b56f276ed55 Mon Sep 17 00:00:00 2001 From: bel Date: Sat, 9 Jul 2022 15:21:11 -0600 Subject: [PATCH] add -ro for read-only tag --- main.go | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/main.go b/main.go index 50762ba..d23f54b 100755 --- a/main.go +++ b/main.go @@ -32,6 +32,7 @@ func main() { fs = args.NewArgSet() fs.Append(args.STRING, "p", "port to serve", "8100") fs.Append(args.BOOL, "md", "whether to render markdown as html", true) + fs.Append(args.BOOL, "ro", "read only mode", false) fs.Append(args.STRING, "md-css", "css to load for md", "/dev/null") fs.Append(args.STRING, "md-class", "class to wrap md", "phb") fs.Append(args.STRING, "d", "static path to serve", "./public") @@ -41,6 +42,7 @@ func main() { d := fs.Get("d").GetString() md := fs.Get("md").GetBool() + ro := fs.Get("ro").GetBool() mdCss := fs.Get("md-css").GetString() mdClass := fs.Get("md-class").GetString() if mdCss != "" { @@ -89,15 +91,15 @@ func main() { } p := strings.TrimPrefix(fs.Get("p").GetString(), ":") - http.Handle("/", http.HandlerFunc(handler(d, md, mdCss, mdClass))) + http.Handle("/", http.HandlerFunc(handler(ro, d, md, mdCss, mdClass))) log.Printf("Serving %s on HTTP port: %s\n", d, p) log.Fatal(http.ListenAndServe(":"+p, nil)) } -func handler(d string, md bool, mdCss, mdClass string) http.HandlerFunc { - return gzip(endpoints(withDel(withMD(d, md, mdCss, mdClass, fserve(d))))) +func handler(ro bool, d string, md bool, mdCss, mdClass string) http.HandlerFunc { + return gzip(endpoints(ro, withDel(ro, withMD(d, md, mdCss, mdClass, fserve(d))))) } func writeMeta(w http.ResponseWriter) { @@ -129,17 +131,19 @@ func gzip(foo http.HandlerFunc) http.HandlerFunc { } } -func endpoints(foo http.HandlerFunc) http.HandlerFunc { +func endpoints(ro bool, foo http.HandlerFunc) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { if isDir(r) { writeMeta(w) - writeForm(w) + if !ro { + writeForm(w) + } } - if isUploaded(r) { + if !ro && isUploaded(r) { if err := upload(w, r); err != nil { fmt.Fprintln(w, err.Error()) } - } else if isDeleted(r) { + } else if !ro && isDeleted(r) { if err := del(w, r); err != nil { fmt.Fprintln(w, err.Error()) } @@ -198,7 +202,7 @@ func withMD(dir string, enabled bool, mdCss, mdClass string, foo http.HandlerFun } } -func withDel(foo http.HandlerFunc) http.HandlerFunc { +func withDel(ro bool, foo http.HandlerFunc) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { if !isDir(r) { foo(w, r) @@ -210,7 +214,7 @@ func withDel(foo http.HandlerFunc) http.HandlerFunc { b := bytes.Split(w2.Body.Bytes(), []byte("\n")) buff := bytes.NewBuffer(nil) for i := range b { - if bytes.Contains(b[i], []byte(" 0 {