add -ro for read-only tag

master v0.13.0
bel 2022-07-09 15:21:11 -06:00
parent 7b04c49f4a
commit c8e09a989d
1 changed files with 13 additions and 9 deletions

20
main.go
View File

@ -32,6 +32,7 @@ func main() {
fs = args.NewArgSet() fs = args.NewArgSet()
fs.Append(args.STRING, "p", "port to serve", "8100") 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, "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-css", "css to load for md", "/dev/null")
fs.Append(args.STRING, "md-class", "class to wrap md", "phb") fs.Append(args.STRING, "md-class", "class to wrap md", "phb")
fs.Append(args.STRING, "d", "static path to serve", "./public") fs.Append(args.STRING, "d", "static path to serve", "./public")
@ -41,6 +42,7 @@ func main() {
d := fs.Get("d").GetString() d := fs.Get("d").GetString()
md := fs.Get("md").GetBool() md := fs.Get("md").GetBool()
ro := fs.Get("ro").GetBool()
mdCss := fs.Get("md-css").GetString() mdCss := fs.Get("md-css").GetString()
mdClass := fs.Get("md-class").GetString() mdClass := fs.Get("md-class").GetString()
if mdCss != "" { if mdCss != "" {
@ -89,15 +91,15 @@ func main() {
} }
p := strings.TrimPrefix(fs.Get("p").GetString(), ":") 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.Printf("Serving %s on HTTP port: %s\n", d, p)
log.Fatal(http.ListenAndServe(":"+p, nil)) log.Fatal(http.ListenAndServe(":"+p, nil))
} }
func handler(d string, md bool, mdCss, mdClass string) http.HandlerFunc { func handler(ro bool, d string, md bool, mdCss, mdClass string) http.HandlerFunc {
return gzip(endpoints(withDel(withMD(d, md, mdCss, mdClass, fserve(d))))) return gzip(endpoints(ro, withDel(ro, withMD(d, md, mdCss, mdClass, fserve(d)))))
} }
func writeMeta(w http.ResponseWriter) { 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) { return func(w http.ResponseWriter, r *http.Request) {
if isDir(r) { if isDir(r) {
writeMeta(w) writeMeta(w)
if !ro {
writeForm(w) writeForm(w)
} }
if isUploaded(r) { }
if !ro && isUploaded(r) {
if err := upload(w, r); err != nil { if err := upload(w, r); err != nil {
fmt.Fprintln(w, err.Error()) fmt.Fprintln(w, err.Error())
} }
} else if isDeleted(r) { } else if !ro && isDeleted(r) {
if err := del(w, r); err != nil { if err := del(w, r); err != nil {
fmt.Fprintln(w, err.Error()) 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) { return func(w http.ResponseWriter, r *http.Request) {
if !isDir(r) { if !isDir(r) {
foo(w, r) foo(w, r)
@ -210,7 +214,7 @@ func withDel(foo http.HandlerFunc) http.HandlerFunc {
b := bytes.Split(w2.Body.Bytes(), []byte("\n")) b := bytes.Split(w2.Body.Bytes(), []byte("\n"))
buff := bytes.NewBuffer(nil) buff := bytes.NewBuffer(nil)
for i := range b { for i := range b {
if bytes.Contains(b[i], []byte("<a href=")) { if !ro && bytes.Contains(b[i], []byte("<a href=")) {
re := regexp.MustCompile(`href="[^"]*"`) re := regexp.MustCompile(`href="[^"]*"`)
match := re.Find(b[i]) match := re.Find(b[i])
if len(match) > 0 { if len(match) > 0 {