tAdd logs and a verbose mode - partage - File upload system
 (HTM) git clone git://git.z3bra.org/partage.git
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit 8f777a6d331e2e1f53a561d6cf32746c290d7333
 (DIR) parent 9143f6b3f876591bd91ac2a883d1704be39da3a6
 (HTM) Author: Willy Goiffon <dev@z3bra.org>
       Date:   Tue, 19 Oct 2021 11:45:01 +0200
       
       Add logs and a verbose mode
       
       Diffstat:
         M partage.go                          |      39 +++++++++++++++++++++++++++++++
       
       1 file changed, 39 insertions(+), 0 deletions(-)
       ---
 (DIR) diff --git a/partage.go b/partage.go
       t@@ -5,6 +5,7 @@ import (
                "flag"
                "io"
                "io/ioutil"
       +        "log"
                "net/http"
                "os"
                "os/user"
       t@@ -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
       t@@ -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
       t@@ -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)
       t@@ -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())
       t@@ -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)
       t@@ -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)
                }
        
       t@@ -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)
        }