-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
72 lines (58 loc) · 1.43 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
package main // import "github.com/xperimental/goecho"
import (
"context"
"log"
"net/http"
"os"
"os/signal"
"sort"
"syscall"
"time"
)
var (
// Version is set to the build version when building using the build script.
Version = "unknown"
)
func main() {
cfg, err := parseConfig()
if err != nil {
log.Fatalf("Error in configuration: %s", err)
}
hostname, err := os.Hostname()
if err != nil {
log.Printf("Error getting hostname: %s", err)
}
env := []string{}
if cfg.AllowEnv {
env = os.Environ()
sort.Strings(env)
}
server, unreadyFunc := createServer(cfg.Addr, Version, hostname, env)
shutdownErrCh := make(chan error)
go func() {
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, syscall.SIGTERM, syscall.SIGINT)
<-sigCh
signal.Reset()
// Make readiness check fail
unreadyFunc()
if cfg.GracefulDelay > 0 {
log.Printf("Waiting %s for shutdown...", cfg.GracefulDelay)
time.Sleep(cfg.GracefulDelay)
}
log.Println("Shutting down...")
shutdownErrCh <- server.Shutdown(context.Background())
}()
log.Printf("Listening on %s\n", cfg.Addr)
if cfg.TLS.CertFile != "" && cfg.TLS.KeyFile != "" {
err = server.ListenAndServeTLS(cfg.TLS.CertFile, cfg.TLS.KeyFile)
} else {
err = server.ListenAndServe()
}
if err != nil && err != http.ErrServerClosed {
log.Fatalf("Error during listen: %s", err)
}
if err := <-shutdownErrCh; err != nil {
log.Fatalf("Error during shutdown: %s", err)
}
}