Changeset 11 in code for trunk


Ignore:
Timestamp:
Oct 13, 2021, 2:57:27 PM (4 years ago)
Author:
dev
Message:

Add templating system for pages

Location:
trunk
Files:
2 added
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/go.mod

    r1 r11  
    22
    33go 1.17
     4
     5require github.com/dustin/go-humanize v1.0.0 // indirect
  • trunk/partage.go

    r10 r11  
    99        "path"
    1010        "path/filepath"
     11        "html/template"
     12
     13        "github.com/dustin/go-humanize"
    1114)
     15
     16type templatedata struct {
     17        Maxsize string
     18}
    1219
    1320var conf struct {
    1421        bind     string
     22        baseuri  string
    1523        filepath string
    1624        rootdir  string
    17         baseuri string
     25        templatedir string
    1826        filectx  string
    1927        maxsize  int64
    2028}
     29
    2130
    2231func contenttype(f *os.File) string {
     
    93102}
    94103
     104func servetemplate(w http.ResponseWriter, f string, d templatedata) {
     105        t, err := template.ParseFiles(conf.templatedir + "/" + f)
     106        if err != nil {
     107                w.WriteHeader(http.StatusInternalServerError)
     108                return
     109        }
     110
     111        err = t.Execute(w, d)
     112        if err != nil {
     113                fmt.Println(err)
     114        }
     115}
     116
    95117func uploaderPut(w http.ResponseWriter, r *http.Request) {
    96118        // Max 15 Gb uploads
     
    120142        // r.URL.Path is sanitized regarding "." and ".."
    121143        filename := r.URL.Path
    122         if r.URL.Path == "/" {
    123                 filename = "/index.html"
     144        if r.URL.Path == "/" || r.URL.Path == "/index" {
     145                data := templatedata{ Maxsize: humanize.IBytes(uint64(conf.maxsize))}
     146                servetemplate(w, "/index.html", data)
     147                return
    124148        }
    125149
     
    146170func main() {
    147171        conf.bind = "0.0.0.0:8080"
    148         conf.maxsize = 28 * 1024 * 1024
     172        conf.maxsize = 30064771072 // 28Gib
    149173        conf.filepath = "/tmp"
    150174        conf.rootdir = "./static"
    151175        conf.baseuri = "http://192.168.0.3:8080"
    152176        conf.filectx = "/f/"
     177        conf.templatedir = "./templates"
    153178
    154179        http.HandleFunc("/", uploader)
Note: See TracChangeset for help on using the changeset viewer.