-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
99 lines (85 loc) · 2.22 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
package main
import (
"flag"
"fmt"
"net/http"
"os"
"path"
"sort"
"strconv"
"strings"
)
var (
Port *string
Root *string
)
func main() {
Port = flag.String("port", "8080", "server port")
Root = flag.String("path", "./", "server root path")
flag.Parse()
fmt.Printf("start HTTP server @ 0.0.0.0:%s\nload storage @ %s", *Port, *Root)
http.HandleFunc("/", fileServer)
err := http.ListenAndServe(":"+*Port, nil)
if err != nil {
fmt.Printf("start error: %v", err)
os.Exit(1)
}
}
func fileServer(w http.ResponseWriter, req *http.Request) {
fPath := path.Join(*Root, req.URL.Path)
fObj, err := os.Open(fPath)
if err != nil {
http.NotFound(w, req)
return
}
defer fObj.Close()
fInfo, _ := fObj.Stat()
if fInfo.IsDir() {
fList, _ := fObj.Readdir(-1)
sort.SliceStable(fList, func(i, j int) bool {
if fList[i].IsDir() == fList[j].IsDir() {
return fList[i].Name() < fList[j].Name()
}
return fList[i].IsDir() && !fList[j].IsDir()
})
var oLastIndex string
oIndex := req.URL.Path
if oIndex == "/" {
oLastIndex = oIndex
} else {
oLastIndex = path.Dir(oIndex)
oIndex += "/"
}
head := fmt.Sprintf("<html><h1>Index of %s</h1><hr/><pre><a href=\"%s\">../</a>\n", oIndex, oLastIndex)
_, _ = w.Write([]byte(head))
for _, _f := range fList {
var li string
mName := _f.Name()
mUrl := path.Join(req.URL.Path, mName)
mTime := _f.ModTime().Format("2006-01-02 15:04:05")
mNameLength := max(50-len(mName), 1)
const mSizeLength = 19
if _f.IsDir() {
sn := strings.Repeat(" ", mNameLength)
sl := strings.Repeat(" ", mSizeLength)
li = fmt.Sprintf("<a href=\"%s\">%s/</a>%s%s%s-\n", mUrl, mName, sn, mTime, sl)
} else {
var mSize string
_size := _f.Size()
if _size > 10240 {
_size >>= 10
mSize = strconv.FormatInt(_size, 10) + "kb"
} else {
mSize = strconv.FormatInt(_size, 10)
}
sn := strings.Repeat(" ", mNameLength+1)
sl := strings.Repeat(" ", max(mSizeLength-len(mSize), 1)+1)
li = fmt.Sprintf("<a href=\"%s\">%s</a>%s%s%s%s\n", mUrl, mName, sn, mTime, sl, mSize)
}
_, _ = w.Write([]byte(li))
}
_, _ = w.Write([]byte("</pre><hr/></html>"))
} else {
http.ServeContent(w, req, fInfo.Name(), fInfo.ModTime(), fObj)
}
}