-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
104 lines (90 loc) · 2.08 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package main
import (
"compress/gzip"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"path/filepath"
)
type FileInfo struct {
P string `json:"p"` // path
D string `json:"d"` // dir
F string `json:"f"` // filename
S string `json:"s"` // size
}
func main() {
http.HandleFunc("/filelist", files)
http.HandleFunc("/styles.css", styles)
http.HandleFunc("/script.js", script)
http.HandleFunc("/", index)
fs := http.FileServer(http.Dir("./files"))
http.Handle("/files/", http.StripPrefix("/files/", fs))
log.Println("Server started on port 3000...")
err := http.ListenAndServe(":3000", nil)
if err != nil {
log.Println(err)
}
}
func files(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Content-Encoding", "gzip")
gz := gzip.NewWriter(w)
defer gz.Close()
_, err := gz.Write(fileJSON())
if err != nil {
log.Println(err)
}
}
func styles(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/css")
http.ServeFile(w, r, "styles.css")
}
func script(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/javascript")
http.ServeFile(w, r, "script.js")
}
func index(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "index.html")
}
func fileJSON() []byte {
files := []FileInfo{}
err := filepath.Walk("files/",
func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
dir, file := filepath.Split(path)
files = append(files, FileInfo{
P: path,
D: dir,
F: file,
S: humanizeBytes(info.Size()),
})
return nil
})
if err != nil {
log.Println(err)
}
filesJson, err := json.MarshalIndent(files, "", " ")
if err != nil {
log.Println(err)
}
return filesJson
}
func humanizeBytes(bytes int64) string {
const unit = 1024
if bytes < unit {
return fmt.Sprintf("%dB", bytes)
}
div, exp := int64(unit), 0
for n := bytes / unit; n >= unit; n /= unit {
div *= unit
exp++
}
return fmt.Sprintf("%.1f%cB", float64(bytes)/float64(div), "KMGTPE"[exp])
}