tAdd templating system for pages - 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 bd7d7c3397dafb954e0f452f75179227cbec4226
 (DIR) parent bd7a30667ba7fc6c187d4eb085c7d2bac36ed0ec
 (HTM) Author: Willy Goiffon <dev@z3bra.org>
       Date:   Wed, 13 Oct 2021 16:57:27 +0200
       
       Add templating system for pages
       
       Diffstat:
         M go.mod                              |       2 ++
         M partage.go                          |      33 +++++++++++++++++++++++++++----
         A templates/index.html                |      21 +++++++++++++++++++++
       
       3 files changed, 52 insertions(+), 4 deletions(-)
       ---
 (DIR) diff --git a/go.mod b/go.mod
       t@@ -1,3 +1,5 @@
        module git.z3bra.org/partage
        
        go 1.17
       +
       +require github.com/dustin/go-humanize v1.0.0 // indirect
 (DIR) diff --git a/partage.go b/partage.go
       t@@ -8,17 +8,26 @@ import (
                "os"
                "path"
                "path/filepath"
       +        "html/template"
       +
       +        "github.com/dustin/go-humanize"
        )
        
       +type templatedata struct {
       +        Maxsize string
       +}
       +
        var conf struct {
                bind     string
       +        baseuri  string
                filepath string
                rootdir  string
       -        baseuri  string
       +        templatedir string
                filectx  string
                maxsize  int64
        }
        
       +
        func contenttype(f *os.File) string {
                buffer := make([]byte, 512)
        
       t@@ -92,6 +101,19 @@ func servefile(f *os.File, w http.ResponseWriter) {
                }
        }
        
       +func servetemplate(w http.ResponseWriter, f string, d templatedata) {
       +        t, err := template.ParseFiles(conf.templatedir + "/" + f)
       +        if err != nil {
       +                w.WriteHeader(http.StatusInternalServerError)
       +                return
       +        }
       +
       +        err = t.Execute(w, d)
       +        if err != nil {
       +                fmt.Println(err)
       +        }
       +}
       +
        func uploaderPut(w http.ResponseWriter, r *http.Request) {
                // Max 15 Gb uploads
                if r.ContentLength > conf.maxsize {
       t@@ -119,8 +141,10 @@ func uploaderPut(w http.ResponseWriter, r *http.Request) {
        func uploaderGet(w http.ResponseWriter, r *http.Request) {
                // r.URL.Path is sanitized regarding "." and ".."
                filename := r.URL.Path
       -        if r.URL.Path == "/" {
       -                filename = "/index.html"
       +        if r.URL.Path == "/" || r.URL.Path == "/index" {
       +                data := templatedata{ Maxsize: humanize.IBytes(uint64(conf.maxsize))}
       +                servetemplate(w, "/index.html", data)
       +                return
                }
        
                f, err := os.Open(conf.rootdir + filename)
       t@@ -145,11 +169,12 @@ func uploader(w http.ResponseWriter, r *http.Request) {
        
        func main() {
                conf.bind = "0.0.0.0:8080"
       -        conf.maxsize = 28 * 1024 * 1024
       +        conf.maxsize = 30064771072 // 28Gib
                conf.filepath = "/tmp"
                conf.rootdir = "./static"
                conf.baseuri = "http://192.168.0.3:8080"
                conf.filectx = "/f/"
       +        conf.templatedir = "./templates"
        
                http.HandleFunc("/", uploader)
                http.Handle(conf.filectx, http.StripPrefix(conf.filectx, http.FileServer(http.Dir(conf.filepath))))
 (DIR) diff --git a/templates/index.html b/templates/index.html
       t@@ -0,0 +1,21 @@
       +<!DOCTYPE HTML>
       +<html>
       +<head>
       +        <meta charset="utf-8">
       +        <meta name="author" content="z3bra">
       +        <meta name="viewport" content="width=device-width">
       +        <link rel="icon" type="image/png" href="/favicon.png" />
       +        <title>Partage</title>
       +</head>
       +<body>
       +        <h1>Partage</h1>
       +        <p>
       +                Use the box below to upload and share files. File size is
       +                limited to {{.Maxsize}}.
       +        </p>
       +        <form>
       +                <label for="uck">Select file(s) to share</label>
       +                <input id="uck" type="file" multiple=true/>
       +                <input type="submit" value="Upload!"/>
       +        </form>
       +</body>