/
main.go
55 lines (45 loc) · 1.01 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
/**
* Created on 23/2/13 by cos
*/
package main
import (
"flag"
"fmt"
"log"
"net/http"
"net/url"
)
var root string
var port int
func main() {
flag.StringVar(&root, "h", "", "home directory")
flag.IntVar(&port, "p", 8080, "port")
flag.Parse()
if len(root) <= 0 {
log.Fatal("usage: server -h <Home directory> -p <PORT>")
}
http.HandleFunc("/", router)
log.Print("start listen at ", port)
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", port), nil))
}
func router(w http.ResponseWriter, r *http.Request) {
log.Printf("serving %s", r.RequestURI)
path := r.URL.Path
log.Print("path = ", path)
if "/" == path {
// log.Print("list dir")
http.ServeFile(w, r, root)
return
}
if path[0] == '/' {
local, err := url.QueryUnescape(root + "/" + path[1:])
if err != nil {
http.Error(w, path[1:], http.StatusNotFound)
return
}
log.Printf("serve file: %s", local)
http.ServeFile(w, r, local)
return
}
http.Error(w, fmt.Sprintf("Page not found: %s", r.RequestURI), http.StatusNotFound)
}