-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
109 lines (89 loc) · 2.33 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
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
100
101
102
103
104
105
106
107
108
109
package main
import (
"context"
"flag"
"fmt"
"html/template"
"log"
"net/http"
_ "net/http/pprof"
"os"
"os/signal"
"sync"
"time"
)
var (
Version = "dev"
pageTemplate *template.Template
)
func init() {
htmlTemplate := `<!DOCTYPE html><html lang="en">
<head><title>blackhole</title></head>
<body><p>{{ range . }}<a href="{{ . }}">{{ . }}</a><br />{{ end }}</p></body>
</html>`
tmpl, err := template.New("page").Parse(htmlTemplate)
if err != nil {
log.Fatalf("parse template: %v", err)
}
pageTemplate = tmpl
}
func main() {
flags, err := ParseFlags()
if err != nil {
failFlags(err)
}
serverWG := &sync.WaitGroup{}
log.Printf("starting blackhole version: %s flags: %+v", Version, flags)
serverWG.Add(1)
serverShutdown := startServer(serverWG, "blackhole", flags.Port, handler(flags))
serverProfileShutdown := func() {}
if flags.ProfilePort != 0 {
serverWG.Add(1)
// pprof registers its handler with default server mux
serverProfileShutdown = startServer(serverWG, "pprof", flags.ProfilePort, http.DefaultServeMux)
}
stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt)
<-stop
serverShutdown()
serverProfileShutdown()
serverWG.Wait()
}
func startServer(wg *sync.WaitGroup, name string, port int, handler http.Handler) (shutdown func()) {
server := &http.Server{
Addr: fmt.Sprintf(":%d", port),
Handler: handler,
ReadTimeout: 5 * time.Second,
WriteTimeout: 5 * time.Second,
}
go func() {
defer wg.Done()
if err := server.ListenAndServe(); err != nil {
log.Printf("[ERROR] listen and serve: %v", err)
}
}()
log.Printf("started %s server on port %d", name, port)
return func() {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := server.Shutdown(ctx); err != nil {
log.Printf("[ERROR] server shutdown: %v", err)
}
log.Printf("shutdown %s server", name)
}
}
func handler(flags Flags) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
if err := pageTemplate.Execute(w, GetLinks(flags)); err != nil {
w.WriteHeader(http.StatusInternalServerError)
}
})
}
func failFlags(err error) {
flag.CommandLine.SetOutput(os.Stderr)
if _, e := fmt.Fprintln(flag.CommandLine.Output(), err.Error()); e != nil {
fmt.Printf("cannot print %v: %v", err, e)
}
flag.Usage()
os.Exit(1)
}