-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfileserver.go
59 lines (49 loc) · 1.29 KB
/
fileserver.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
package main
import (
"flag"
"log"
"net/http"
"os"
"strings"
)
const usageText = `Usage: fileserver [-a host:port] [pattern:]dir ...
The program serves files from a local dist using dir argument as root.
An optional pattern specified the URL base path. For instance,
to serve /tmp dir at /temporary, one can use the following:
fileserver /temporary:/tmp
The dir argument can be specified multiple times.
If no dir argument is provided, current directory is used.
`
var address *string = flag.String("a", "localhost:8000", "Address (host:port) to listen on")
func main() {
flag.Usage = func() {
os.Stderr.WriteString(usageText)
flag.PrintDefaults()
}
flag.Parse()
roots := flag.Args()
if len(roots) == 0 {
roots = append(roots, ".")
}
for _, r := range roots {
var p string
if i := strings.IndexRune(r, ':'); i >= 0 {
p = r[:i]
r = r[i+1:]
}
if !strings.HasPrefix(p, "/") {
p = "/" + p
}
fs := http.FileServer(http.Dir(r))
if len(p) > 1 {
fs = http.StripPrefix(p, fs)
}
http.Handle(p, fs)
}
log.Printf("serving %s on http://%s", roots, *address)
log.Fatal(http.ListenAndServe(*address, http.HandlerFunc(handle)))
}
func handle(w http.ResponseWriter, r *http.Request) {
log.Printf("%s %s", r.Method, r.RequestURI)
http.DefaultServeMux.ServeHTTP(w, r)
}