-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
42 lines (34 loc) · 906 Bytes
/
Copy pathmain.go
File metadata and controls
42 lines (34 loc) · 906 Bytes
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
package main
import (
"flag"
"fmt"
"net/http"
"time"
)
func logmiddlewareHandler(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
path := r.URL.Path
raw := r.URL.RawQuery
if raw != "" {
path = path + "?" + raw
}
next.ServeHTTP(w, r)
end := time.Now()
ip := r.RemoteAddr
method := r.Method
fmt.Printf("%s %s %vms %s \n", ip, method, (end.UnixNano()-start.UnixNano())/1000/1000, path)
})
}
func main() {
port := flag.Int("port", 8090, "-port=8090")
dir := flag.String("rootdir", ".", "-rootdir=./dirname")
flag.Parse()
fmt.Println("http服务根目录:", *dir)
fmt.Printf("访问地址: 127.0.0.1:%d \n", *port)
http.Handle("/", logmiddlewareHandler(http.FileServer(http.Dir(*dir))))
err := http.ListenAndServe(fmt.Sprintf(":%d", *port), nil)
if err != nil {
panic(err.Error())
}
}