-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
122 lines (100 loc) · 2.93 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
110
111
112
113
114
115
116
117
118
119
120
121
122
package main
import (
"context"
"expvar"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/adeniyistephen/fast-speedtest/app"
"github.com/ardanlabs/conf"
"github.com/pkg/errors"
)
// build is the git version of this program. It is set using build flags in the makefile.
var build = "develop"
func main() {
log := log.New(os.Stdout, "SPEEDY : ", log.LstdFlags|log.Lmicroseconds|log.Lshortfile)
if err := run(log); err != nil {
log.Println("main: error:", err)
os.Exit(1)
}
}
func run(log *log.Logger) error {
var cfg struct {
conf.Version
Web struct {
APIHost string `conf:"default:0.0.0.0:3000"`
ReadTimeout time.Duration `conf:"default:5m"`
WriteTimeout time.Duration `conf:"default:5m"`
ShutdownTimeout time.Duration `conf:"default:5m"`
}
}
cfg.Version.SVN = build
if err := conf.Parse(os.Args[1:], "SPEEDY", &cfg); err != nil {
switch err {
case conf.ErrHelpWanted:
usage, err := conf.Usage("SPEEDY", &cfg)
if err != nil {
return errors.Wrap(err, "generating config usage")
}
fmt.Println(usage)
return nil
case conf.ErrVersionWanted:
version, err := conf.VersionString("SPEEDY", &cfg)
if err != nil {
return errors.Wrap(err, "generating config version")
}
fmt.Println(version)
return nil
}
return errors.Wrap(err, "parsing config")
}
// App Starting
expvar.NewString("build").Set(build)
log.Printf("main: Started: Application initializing: version %q", build)
defer log.Println("main: Completed")
out, err := conf.String(&cfg)
if err != nil {
return errors.Wrap(err, "generating config for output")
}
log.Printf("main: Config:\n%v\n", out)
//===================================================
// Start API Service
log.Println("main: Initializing API support")
// Make a channel to listen for an interrupt or terminate signal from the OS.
// Use a buffered channel because the signal package requires it.
shutdown := make(chan os.Signal, 1)
signal.Notify(shutdown, os.Interrupt, syscall.SIGTERM)
api := http.Server{
Addr: cfg.Web.APIHost,
Handler: app.Routes(build, shutdown, log),
ReadTimeout: cfg.Web.ReadTimeout,
WriteTimeout: cfg.Web.WriteTimeout,
}
serverErrors := make(chan error, 1)
// Start the service listening for requests.
go func() {
log.Printf("main: API listening on %s", api.Addr)
serverErrors <- api.ListenAndServe()
}()
// Shutdown
// Blocking main and waiting for shutdown.
select {
case err := <-serverErrors:
return errors.Wrap(err, "server error")
case sig := <-shutdown:
log.Printf("main: %v: Start shutdown", sig)
// Give outstanding requests a deadline for completion.
ctx, cancel := context.WithTimeout(context.Background(), cfg.Web.ShutdownTimeout)
defer cancel()
// Asking listener to shutdown and shed load.
if err := api.Shutdown(ctx); err != nil {
api.Close()
return errors.Wrap(err, "could not stop server gracefully")
}
}
return nil
}