all repos — fsrv @ b0805af60f2c59885ce9aaf44dc935a5ff6b3eed

filehost server for x.icyphox.sh

Serve files from subdirs
Anirudh Oppiliappan x@icyphox.sh
Wed, 07 Dec 2022 22:13:22 +0530
commit

b0805af60f2c59885ce9aaf44dc935a5ff6b3eed

parent

41f91f83668d0c848201adbf8f537ff8a5714cd4

2 files changed, 25 insertions(+), 7 deletions(-)

jump to
A fs.go

@@ -0,0 +1,24 @@

+package main + +import ( + "net/http" + "os" +) + +type nodirFileSystem struct { + fs http.FileSystem +} + +func (nd nodirFileSystem) Open(path string) (http.File, error) { + f, err := nd.fs.Open(path) + if err != nil { + return nil, err + } + + s, err := f.Stat() + if s.IsDir() { + return nil, os.ErrNotExist + } + + return f, nil +}
M main.gomain.go

@@ -91,12 +91,6 @@ func (s *settings) indexpage(w http.ResponseWriter, r *http.Request) {

http.ServeFile(w, r, s.index) } -func (s *settings) servefile(w http.ResponseWriter, r *http.Request) { - f := flow.Param(r.Context(), "file") - log.Printf("serving file: %s", f) - http.ServeFile(w, r, filepath.Join(s.storepath, f)) -} - func (s *settings) readSettings() { flag.StringVar(&s.url, "url", "localhost", "url for fsrv to serve files") flag.StringVar(&s.addr, "addr", "0.0.0.0:9393", "address to listen on")

@@ -117,7 +111,7 @@ st.readSettings()

mux.HandleFunc("/", st.upload, "POST") mux.HandleFunc("/", st.indexpage, "GET") - mux.HandleFunc("/:file", st.servefile, "GET") + mux.Handle("/...", http.FileServer(nodirFileSystem{http.Dir(st.storepath)})) log.Println("listening on " + st.addr) http.ListenAndServe(st.addr, mux)