-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
68 lines (54 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
68
package main
import (
"fmt"
"net/http"
"net/http/httputil"
"os"
"strings"
"github.com/Akagi201/light"
log "github.com/Sirupsen/logrus"
"github.com/dre1080/recover"
flags "github.com/jessevdk/go-flags"
"github.com/pressly/lg"
"github.com/rs/cors"
)
var opts struct {
ListenAddr string `long:"listen" default:"0.0.0.0:8327" description:"HTTP address to listen at"`
LogLevel string `long:"log_level" default:"info" description:"log level"`
}
func handler(w http.ResponseWriter, r *http.Request) {
dumpReq, err := httputil.DumpRequest(r, true)
if err != nil {
log.Fatal(err)
}
log.Debugf("DUMP request: %v", string(dumpReq))
w.Header().Set("Httpdump-Author", "Akagi201")
r.Write(w)
}
func main() {
parser := flags.NewParser(&opts, flags.HelpFlag|flags.PassDoubleDash|flags.IgnoreUnknown)
_, err := parser.Parse()
if err != nil {
fmt.Printf("%v", err)
os.Exit(-1)
}
if level, err := log.ParseLevel(strings.ToLower(opts.LogLevel)); err != nil {
log.SetLevel(level)
}
log.SetFormatter(&log.TextFormatter{
FullTimestamp: true,
TimestampFormat: "2006-01-02 15:04:05",
})
logger := log.New()
root := light.New()
root.Use(lg.RequestLogger(logger))
recovery := recover.New(&recover.Options{
Log: log.Print,
})
handleCORS := cors.Default().Handler
root.Use(handleCORS)
root.Use(recovery)
root.HandleAll("/*path", http.HandlerFunc(handler))
log.Infof("httpdump listening at: %v", opts.ListenAddr)
http.ListenAndServe(opts.ListenAddr, root)
}