diff options
Diffstat (limited to 'pipkin.go')
-rw-r--r-- | pipkin.go | 32 |
1 files changed, 30 insertions, 2 deletions
@@ -11,11 +11,13 @@ import ( "flag" "fmt" "html/template" + "math" "io" "log" "net/http" "os" "path" + "strconv" "strings" "syscall" "time" @@ -254,8 +256,16 @@ func (pk *Pipkin) fetch(w http.ResponseWriter, r *http.Request) { index.Contents[i].Key = prefixTrim } - t, _ := template.ParseFiles(path.Join(pk.templates, - "index.html")) + t, err := template.New("index.html").Funcs(template.FuncMap{ + "prettyByteSize": prettyByteSize, + "formatLastModified": formatLastModified, + }).ParseFiles(path.Join(pk.templates, "index.html")) + if err != nil { + pk.log.Errorf("Failed to render template: %s %s %s: %s\n", + hostStr, host.Bucket, key, err.Error()) + http.Error(w, http.StatusText(http.StatusNotFound), + http.StatusNotFound) + } if err := t.Execute(w, index); err != nil { pk.log.Errorf("Failed to render template: %s %s %s: %s\n", hostStr, host.Bucket, key, err.Error()) @@ -409,6 +419,24 @@ func (pk *Pipkin) presignQuery(method string, host Host, key string) (string, er return signedURL.String(), nil } +func prettyByteSize(b string) string { + + bf, _ := strconv.ParseFloat(b, 0) + for _, unit := range []string{"", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi"} { + if math.Abs(bf) < 1024.0 { + return fmt.Sprintf("%3.1f%sB", bf, unit) + } + bf /= 1024.0 + } + return fmt.Sprintf("%.1fYiB", bf) +} + +func formatLastModified(value string) string { + + t, _ := time.Parse("2006-01-02T15:04:05.000Z", value) + return t.Format(time.DateTime) +} + func main() { // We should re-use socket connections between requests. |