-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhttp-server.go
99 lines (85 loc) · 1.79 KB
/
http-server.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 (
"context"
"flag"
"fmt"
"log"
"net"
"net/http"
"os"
"os/signal"
"syscall"
"time"
)
//localIP 输出local ip
func localIP() []net.IP {
ips := make([]net.IP, 0)
if ifaces, err := net.Interfaces(); err == nil {
// handle err
for _, i := range ifaces {
addrs, _ := i.Addrs()
for _, addr := range addrs {
var ip net.IP
switch v := addr.(type) {
case *net.IPNet:
ip = v.IP
case *net.IPAddr:
ip = v.IP
}
if ip.To4() != nil {
ips = append(ips, ip)
}
}
}
}
return ips
}
//loggingHandler 输出log
func loggingHandler(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
//fmt.Println(*r.URL)
start := time.Now()
h.ServeHTTP(w, r)
// log request by who(IP address)
requesterIP := r.RemoteAddr
ua := r.UserAgent()
log.Printf(
"%s\t%s\t%s\t%s\t%v",
r.Method,
r.RequestURI,
requesterIP,
ua,
time.Since(start),
)
})
}
func main() {
var port int
flag.IntVar(&port, "p", 8080, "http 端口号 -p=8080")
flag.Parse()
sigs := make(chan os.Signal, 1)
done := make(chan bool)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
_, cancel := context.WithCancel(context.Background())
go func() {
ips := ""
for _, ip := range localIP() {
ips = ips + fmt.Sprintf(" http://%s:%v\n", ip, port)
}
fmt.Println("Starting up http-server, serving ./")
fmt.Println("Available on:")
fmt.Printf("%s", ips)
fmt.Println("Hit CTRL-C to stop the server")
fs := http.FileServer(http.Dir("./"))
http.Handle("/", loggingHandler(fs))
//TODO 检测端口是否被暂用
log.Println(http.ListenAndServe(fmt.Sprintf(":%v", port), nil))
}()
go func() {
<-sigs
cancel()
done <- true
}()
<-done
log.Println("http service stop.")
}