-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
67 lines (60 loc) · 1.46 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
package main
import (
"bytes"
"flag"
"fmt"
"net/http"
"os"
"syscall"
)
var (
flagDebug = flag.Bool("debug", false, "debug mode")
flagBind = flag.String("bind", ":8080", "http listen address")
flagResponse = flag.String("response", "OK\n", "response body")
flagStatusCode = flag.Int("code", 200, "response status code")
flagNoFile = flag.Int("nofile", 0, "ulimit -n: file descriptors")
)
func main() {
flag.Parse()
var (
code = *flagStatusCode
debug = *flagDebug
response = []byte(*flagResponse)
spliter = []byte("\n>>>>> [request] <<<<<\n")
)
if *flagNoFile > 0 {
fmt.Println("set file limit", *flagNoFile)
nofile := uint64(*flagNoFile)
err := syscall.Setrlimit(syscall.RLIMIT_NOFILE, &syscall.Rlimit{nofile, nofile})
if err != nil {
panic(err)
}
}
log := make(chan []byte, 1024)
go func() {
for data := range log {
os.Stdout.Write(data)
}
}()
fmt.Println("listen", *flagBind)
err := http.ListenAndServe(*flagBind, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if debug {
var buf bytes.Buffer
buf.Grow(int(r.ContentLength) + 4096)
buf.Write(spliter)
fmt.Fprintf(&buf, "%s %s%s\n", r.Method, r.Host, r.RequestURI)
for key, _ := range r.Header {
fmt.Fprintf(&buf, "%s: %s\n", key, r.Header.Get(key))
}
fmt.Fprintf(&buf, "\n")
buf.ReadFrom(r.Body)
r.Body.Close()
log <- buf.Bytes()
}
w.WriteHeader(code)
w.Write(response)
}))
if err != nil {
panic(err)
}
}