This repository was archived by the owner on Jun 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathmain.go
More file actions
64 lines (56 loc) · 1.34 KB
/
main.go
File metadata and controls
64 lines (56 loc) · 1.34 KB
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
package main
import (
"context"
"flag"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"time"
)
const (
// How long we are willing to wait for the HTTP server to shut down gracefully
serverShutdownTime = time.Second * 4
)
// stopServer issues a time-limited server shutdown
func stopServer(srv *http.Server) error {
ctx, cancel := context.WithTimeout(context.Background(), serverShutdownTime)
defer cancel()
return srv.Shutdown(ctx)
}
func init() {
// Customize the flag.Usage function's output
flag.Usage = func() {
_, _ = fmt.Fprintf(os.Stderr, "Usage: %s [options] script [args..]\n\n", os.Args[0])
flag.PrintDefaults()
}
}
func main() {
// Determine configuration for service
c, err := readConfig()
if err != nil {
log.Fatalf("Couldn't determine configuration: %v", err)
}
s := NewServer(c)
defer s.fingerCount.Stop()
// Listen for signals telling us to stop
signals := make(chan os.Signal, 1)
signal.Notify(signals, os.Interrupt)
// Start the http server
srv, srvResult := s.Start()
select {
case err := <-srvResult:
if err != nil {
log.Fatalf("Failed to serve for %s: %v", c.ListenAddr, err)
} else {
log.Println("HTTP server shut down")
}
case s := <-signals:
log.Println("Shutting down due to signal:", s)
err := stopServer(srv)
if err != nil {
log.Printf("Failed to shut down HTTP server: %v", err)
}
}
}