diff --git a/Makefile b/Makefile index 8ea6729..e622282 100755 --- a/Makefile +++ b/Makefile @@ -6,6 +6,7 @@ build: embed: echo "embedding css and js into binary" cp src/main.go gossa.go + perl -pe 's/template_will_be_here/`cat src\/page.tmpl`/ge' -i gossa.go perl -pe 's/css_will_be_here/`cat src\/style.css`/ge' -i gossa.go perl -pe 's/js_will_be_here/`cat src\/script.js`/ge' -i gossa.go perl -pe 's/favicon_will_be_here/`base64 -w0 src\/favicon.png`/ge' -i gossa.go @@ -20,7 +21,7 @@ ci: cd src && sleep 5 && go test ci-watch: - ls src/* | entr -rc make ci + ls src/* | entr -rc make ci watch: ls src/* | entr -rc make run diff --git a/src/main.go b/src/main.go index 4d7dd08..2799388 100755 --- a/src/main.go +++ b/src/main.go @@ -6,6 +6,7 @@ import ( "flag" "fmt" "html" + "html/template" "io" "io/ioutil" "log" @@ -21,14 +22,23 @@ var host = flag.String("h", "127.0.0.1", "host to listen to") var port = flag.String("p", "8001", "port to listen to") var verb = flag.Bool("verb", true, "verbosity") var skipHidden = flag.Bool("k", true, "skip hidden files") - var initPath = "" -var css = `css_will_be_here` // js will be embedded here -var js = `js_will_be_here` // id. css -var favicon = "data:image/png;base64,favicon_will_be_here" // id. b64 favicon -var units = [8]string{"k", "M", "G", "T", "P", "E", "Z", "Y"} var fs http.Handler +var page, _ = template.New("pageTemplate").Parse(`template_will_be_here`) + +type rowTemplate struct { + Name string + Href template.HTML + Size string + Ext string +} + +type pageTemplate struct { + Title template.HTML + RowsFiles []rowTemplate + RowsFolders []rowTemplate +} type rpcCall struct { Call string `json:"call"` @@ -47,84 +57,52 @@ func logVerb(s ...interface{}) { } } -func sizeToString(bytes float64) string { - if bytes == 0 { - return "0" - } - var u = -1 +func sizeToString(bytes int64) string { + units := [9]string{"B", "k", "M", "G", "T", "P", "E", "Z", "Y"} + b := float64(bytes) + u := 0 for { - bytes = bytes / 1024 - u++ - if bytes < 1024 { - return strconv.FormatFloat(bytes, 'f', 1, 64) + units[u] + if b < 1024 { + return strconv.FormatFloat(b, 'f', 1, 64) + units[u] } + b = b / 1024 + u++ } } -func row(name string, href string, size float64, ext string) string { - if strings.HasPrefix(href, "/") { - href = strings.Replace(href, "/", "", 1) - } - - return ` - - ` + sizeToString(size) + ` - - ` + name + ` - ` -} - func replyList(w http.ResponseWriter, path string) { if !strings.HasSuffix(path, "/") { path += "/" } - var head = ` - - - - ` + html.EscapeString(path) + ` - - - - - -
Drop here to upload
-

.` + html.EscapeString(path) + `

-
-
- - ` - _files, err := ioutil.ReadDir(initPath + path) check(err) + p := pageTemplate{} if path != "/" { - head += row("../", "../", 0, "folder") + p.RowsFolders = append(p.RowsFolders, rowTemplate{"../", "../", "", "folder"}) } - var dirs = "" - var files = "" - for _, el := range _files { - var name = el.Name() + name := el.Name() + href := url.PathEscape(name) if *skipHidden && strings.HasPrefix(name, ".") { continue } - + if el.IsDir() && strings.HasPrefix(href, "/") { + href = strings.Replace(href, "/", "", 1) + } if el.IsDir() { - dirs += row(name+"/", name, 0, "folder") + p.RowsFolders = append(p.RowsFolders, rowTemplate{name + "/", template.HTML(href), "", "folder"}) } else { - var sl = strings.Split(name, ".") - var ext = sl[len(sl)-1] - files += row(name, name, float64(el.Size()), ext) + sl := strings.Split(name, ".") + ext := strings.ToLower(sl[len(sl)-1]) + p.RowsFiles = append(p.RowsFiles, rowTemplate{name, template.HTML(href), sizeToString(el.Size()), ext}) } } - w.Write([]byte(head + dirs + files + `
-
Gossa 🎶
- - `)) + p.Title = template.HTML(html.EscapeString(path)) + page.Execute(w, p) } func doContent(w http.ResponseWriter, r *http.Request) { @@ -206,7 +184,6 @@ func checkPath(p string) (string, error) { func main() { flag.Parse() - if len(flag.Args()) == 0 { initPath = "." } else { @@ -217,11 +194,11 @@ func main() { initPath, err = filepath.Abs(initPath) check(err) - var hostString = *host + ":" + *port + hostString := *host + ":" + *port fmt.Println("Gossa startig on directory " + initPath) fmt.Println("Listening on http://" + hostString) - var root = http.Dir(initPath) + root := http.Dir(initPath) fs = http.StripPrefix("/", http.FileServer(root)) http.HandleFunc("/rpc", rpc) diff --git a/src/page.tmpl b/src/page.tmpl new file mode 100644 index 0000000..508a895 --- /dev/null +++ b/src/page.tmpl @@ -0,0 +1,51 @@ + + + + + + {{.Title}} + + + + + + +
Drop here to upload
+ +

.{{.Title}}

+ +
+ +
+
+ + + + + {{range .RowsFolders}} + + + + + + + {{end}} + {{range .RowsFiles}} + + + + + + + {{end}} +
{{.Size}}{{.Name}}
{{.Size}}{{.Name}}
+ + + + \ No newline at end of file