-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblackmirror.go
90 lines (77 loc) · 1.91 KB
/
blackmirror.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
package main
import (
"context"
"fmt"
"log"
"net/http"
"net/http/httputil"
"os"
"os/signal"
"syscall"
"github.com/aodin/config"
cli "gopkg.in/urfave/cli.v2"
)
var version string // Set by build, e.g. -ldflags "-X main.version=0.0.1"
var conf config.Config
func main() {
conf.Version = version
app := cli.NewApp()
app.Name = "blackmirror"
app.Usage = "reflect HTTP requests back as a response"
if app.Version = conf.Version; app.Version == "" {
conf.Version = "unversioned"
app.Version = conf.Version
}
app.Flags = []cli.Flag{
cli.IntFlag{
Name: "port, p",
Value: 8080,
Usage: "server port",
EnvVars: []string{"PORT"},
},
cli.StringFlag{
Name: "host, h",
Value: "",
Usage: "server host",
EnvVars: []string{"HOST"},
},
}
app.Action = server
app.Run(os.Args)
}
func server(ctx *cli.Context) error {
// The server will gracefully exit on any interrupt or SIGTERM
stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt, syscall.SIGTERM)
conf.Port = ctx.Int("port")
conf.Domain = ctx.String("host")
// Serve the dump handler on all paths
srv := &http.Server{
Addr: conf.Address(),
Handler: http.HandlerFunc(dump),
}
go func() {
log.Printf(
"blackmirror (%s): starting on %s", conf.Version, conf.Address(),
)
if err := srv.ListenAndServe(); err != nil {
log.Fatal(err)
}
}()
<-stop
log.Printf("blackmirror (%s): shutting down", conf.Version)
srv.Shutdown(context.Background())
return nil
}
func dump(w http.ResponseWriter, r *http.Request) {
// Add the version to the log and response
log.Printf("blackmirror (%s): %s %s", conf.Version, r.Method, r.URL.Path)
dump, err := httputil.DumpRequest(r, true)
if err != nil {
http.Error(w, fmt.Sprint(err), http.StatusInternalServerError)
return
}
w.Header().Set("X-Blackmirror-Version", conf.Version)
w.Header().Set("Content-Type", "text/plain")
fmt.Fprintf(w, "%s", dump)
}