-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
150 lines (130 loc) · 3.28 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package main
import (
"flag"
"log"
"net/http"
"os"
"path/filepath"
"sort"
"strings"
"time"
"github.com/hashicorp/golang-lru"
"github.com/karrick/godirwalk"
)
type server struct {
root string
cache *lru.Cache
quiet bool
}
// Search a directory `path` for a file `childQuery` while ignoring case, which satisfies `predicate`.
// Returns an empty string if no file is found.
func (s *server) resolveComponent(dirPath, childQuery string, predicate func(os.FileInfo) bool) (string, error) {
var err error
var children []string
key := strings.ToLower(dirPath)
if s.cache != nil && s.cache.Contains(key) {
entry, _ := s.cache.Get(key)
children = entry.([]string)
} else {
children, err = godirwalk.ReadDirnames(dirPath, nil)
if err != nil {
return "", err
}
sort.Strings(children)
if s.cache != nil {
s.cache.Add(key, children)
}
}
normSegment := ""
for _, child := range children {
if strings.EqualFold(child, childQuery) {
childPath := filepath.Join(dirPath, child)
fi, err := os.Stat(childPath)
if err != nil {
return "", err
}
if predicate(fi) {
normSegment = child
break
}
}
}
if normSegment == "" {
return "", nil
}
return filepath.Join(dirPath, normSegment), nil
}
func (s *server) serveFile(w http.ResponseWriter, r *http.Request) {
var err error
start := time.Now()
path := filepath.Clean(r.URL.Path)
allDirs, file := filepath.Split(path)
dirs := strings.Split(strings.TrimSuffix(allDirs, "/"), string(filepath.Separator))[1:]
if file == "" {
file = "index.html"
}
resolvedPath := s.root
for _, dirChild := range dirs {
resolvedPath, err = s.resolveComponent(resolvedPath, dirChild, func(fi os.FileInfo) bool {
return fi.IsDir()
})
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
log.Printf("[500] %s (%v)", err, time.Now().Sub(start))
return
}
if resolvedPath == "" {
w.WriteHeader(http.StatusNotFound)
log.Printf("[404] %s (%v)", filepath.Join(s.root, path), time.Now().Sub(start))
return
}
}
resolvedPath, err = s.resolveComponent(resolvedPath, file, func(fi os.FileInfo) bool {
return !fi.IsDir()
})
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
log.Printf("[500] %s (%v)", err, time.Now().Sub(start))
return
}
if resolvedPath == "" {
w.WriteHeader(http.StatusNotFound)
log.Printf("[404] %s (%v)", filepath.Join(s.root, path), time.Now().Sub(start))
return
}
if !s.quiet {
log.Printf("[200] %s (%v)", resolvedPath, time.Now().Sub(start))
}
http.ServeFile(w, r, resolvedPath)
}
func main() {
var err error
var address = flag.String("address", ":8000", "address to serve on")
var cacheSize = flag.Int("cache-size", 128, "number of directory entries cached, disabled=0")
var quiet = flag.Bool("quiet", false, "don't output any information")
flag.Parse()
s := &server{
root: ".",
cache: nil,
quiet: *quiet,
}
if flag.Arg(0) != "" {
s.root = flag.Arg(0)
}
if *cacheSize != 0 {
s.cache, err = lru.New(*cacheSize)
if err != nil {
log.Fatal(err)
}
}
fi, err := os.Stat(s.root)
if err != nil {
log.Fatal(err)
}
if !fi.IsDir() {
log.Fatal(err)
}
log.Printf("info: serving %s on %s with %d-item cache", s.root, *address, *cacheSize)
http.HandleFunc("/", s.serveFile)
log.Fatal(http.ListenAndServe(*address, nil))
}