partage

Unnamed repository; edit this file 'description' to name the repository.
git clone git://git.z3bra.org/partage.git
Log | Files | Refs

commit 8f777a6d331e2e1f53a561d6cf32746c290d7333
parent 9143f6b3f876591bd91ac2a883d1704be39da3a6
Author: Willy Goiffon <dev@z3bra.org>
Date:   Tue, 19 Oct 2021 11:45:01 +0200

Add logs and a verbose mode

Diffstat:
Mpartage.go | 39+++++++++++++++++++++++++++++++++++++++
1 file changed, 39 insertions(+), 0 deletions(-)

diff --git a/partage.go b/partage.go @@ -5,6 +5,7 @@ import ( "flag" "io" "io/ioutil" + "log" "net/http" "os" "os/user" @@ -48,6 +49,8 @@ var conf struct { expiry int64 } +var verbose bool + func writefile(f *os.File, s io.ReadCloser, contentlength int64) error { buffer := make([]byte, 4096) eof := false @@ -93,6 +96,10 @@ func writemeta(filename string, expiry int64) error { Expiry: time.Now().Unix() + expiry, } + if verbose { + log.Printf("Saving metadata for %s in %s", meta.Filename, conf.metapath + "/" + meta.Filename + ".json") + } + f, err := os.Create(conf.metapath + "/" + meta.Filename + ".json") if err != nil { return err @@ -116,6 +123,10 @@ func servetemplate(w http.ResponseWriter, f string, d templatedata) { return } + if verbose { + log.Printf("Serving template %s", t.Name()) + } + err = t.Execute(w, d) if err != nil { fmt.Println(err) @@ -136,6 +147,10 @@ func uploaderPut(w http.ResponseWriter, r *http.Request) { } defer f.Close() + if verbose { + log.Printf("Writing %d bytes to %s", r.ContentLength, tmp) + } + if err = writefile(f, r.Body, r.ContentLength); err != nil { http.Error(w, "Internal error", http.StatusInternalServerError) defer os.Remove(tmp.Name()) @@ -206,10 +221,18 @@ func uploaderGet(w http.ResponseWriter, r *http.Request) { return } + if verbose { + log.Printf("Serving file %s", conf.rootdir + filename) + } + http.ServeFile(w, r, conf.rootdir + filename) } func uploader(w http.ResponseWriter, r *http.Request) { + if verbose { + log.Printf("%s: <%s> %s %s %s", r.Host, r.RemoteAddr, r.Method, r.RequestURI, r.Proto) + } + switch r.Method { case "POST": uploaderPost(w, r) @@ -237,7 +260,14 @@ func main() { iniflags.Parse() + if verbose { + log.Printf("Applied configuration:\n%s", conf) + } + if (conf.chroot != "") { + if verbose { + log.Printf("Changing root to %s", conf.chroot) + } syscall.Chroot(conf.chroot) } @@ -260,11 +290,20 @@ func main() { gid, _ = strconv.Atoi(g.Gid) } + if verbose { + log.Printf("Dropping privileges to %s", conf.user) + } + syscall.Setuid(uid) syscall.Setgid(gid) } http.HandleFunc("/", uploader) http.Handle(conf.filectx, http.StripPrefix(conf.filectx, http.FileServer(http.Dir(conf.filepath)))) + + if verbose { + log.Printf("Listening on %s", conf.bind) + } + http.ListenAndServe(conf.bind, nil) }