Add templating system for pages

git-svn-id: file:///srv/svn/repo/marisa/trunk@11 d6811dac-2434-b64a-9ddc-f563ab233461
This commit is contained in:
dev 2021-10-13 14:57:27 +00:00
parent 2ff3ff31f9
commit cdc6f2e416
3 changed files with 52 additions and 4 deletions

2
go.mod
View File

@ -1,3 +1,5 @@
module git.z3bra.org/partage module git.z3bra.org/partage
go 1.17 go 1.17
require github.com/dustin/go-humanize v1.0.0 // indirect

View File

@ -8,17 +8,26 @@ import (
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
"html/template"
"github.com/dustin/go-humanize"
) )
type templatedata struct {
Maxsize string
}
var conf struct { var conf struct {
bind string bind string
baseuri string
filepath string filepath string
rootdir string rootdir string
baseuri string templatedir string
filectx string filectx string
maxsize int64 maxsize int64
} }
func contenttype(f *os.File) string { func contenttype(f *os.File) string {
buffer := make([]byte, 512) buffer := make([]byte, 512)
@ -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) { func uploaderPut(w http.ResponseWriter, r *http.Request) {
// Max 15 Gb uploads // Max 15 Gb uploads
if r.ContentLength > conf.maxsize { if r.ContentLength > conf.maxsize {
@ -119,8 +141,10 @@ func uploaderPut(w http.ResponseWriter, r *http.Request) {
func uploaderGet(w http.ResponseWriter, r *http.Request) { func uploaderGet(w http.ResponseWriter, r *http.Request) {
// r.URL.Path is sanitized regarding "." and ".." // r.URL.Path is sanitized regarding "." and ".."
filename := r.URL.Path filename := r.URL.Path
if r.URL.Path == "/" { if r.URL.Path == "/" || r.URL.Path == "/index" {
filename = "/index.html" data := templatedata{ Maxsize: humanize.IBytes(uint64(conf.maxsize))}
servetemplate(w, "/index.html", data)
return
} }
f, err := os.Open(conf.rootdir + filename) f, err := os.Open(conf.rootdir + filename)
@ -145,11 +169,12 @@ func uploader(w http.ResponseWriter, r *http.Request) {
func main() { func main() {
conf.bind = "0.0.0.0:8080" conf.bind = "0.0.0.0:8080"
conf.maxsize = 28 * 1024 * 1024 conf.maxsize = 30064771072 // 28Gib
conf.filepath = "/tmp" conf.filepath = "/tmp"
conf.rootdir = "./static" conf.rootdir = "./static"
conf.baseuri = "http://192.168.0.3:8080" conf.baseuri = "http://192.168.0.3:8080"
conf.filectx = "/f/" conf.filectx = "/f/"
conf.templatedir = "./templates"
http.HandleFunc("/", uploader) http.HandleFunc("/", uploader)
http.Handle(conf.filectx, http.StripPrefix(conf.filectx, http.FileServer(http.Dir(conf.filepath)))) http.Handle(conf.filectx, http.StripPrefix(conf.filectx, http.FileServer(http.Dir(conf.filepath))))

21
templates/index.html Normal file
View File

@ -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>