/
main.go
83 lines (71 loc) · 2.16 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
package main
import (
"context"
"flag"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/redhat-developer/app-service/appserver"
"github.com/redhat-developer/app-service/configuration"
)
func main() {
// Parse flags
var configFilePath string
flag.StringVar(&configFilePath, "config", "", "path to the config file to read (if none is given, defaults will be used)")
flag.Parse()
// Override default -config switch with environment variable only if -config
// switch was not explicitly given via the command line.
configSwitchIsSet := false
flag.Visit(func(f *flag.Flag) {
if f.Name == "config" {
configSwitchIsSet = true
}
})
if !configSwitchIsSet {
if envConfigPath, ok := os.LookupEnv(configuration.EnvPrefix + "_CONFIG_FILE_PATH"); ok {
configFilePath = envConfigPath
}
}
srv, err := appserver.New(configFilePath)
if err != nil {
panic(err.Error())
}
err = srv.SetupRoutes()
if err != nil {
panic(err.Error())
}
routesToPrint, err := srv.GetRegisteredRoutes()
if err != nil {
panic(err.Error())
}
srv.Logger().Print(routesToPrint)
// listen concurrently to allow for graceful shutdown
go func() {
srv.Logger().Printf("Listening on %q...", srv.Config().GetHTTPAddress())
if err := srv.HTTPServer().ListenAndServe(); err != nil {
srv.Logger().Println(err)
}
}()
gracefulShutdown(srv.HTTPServer(), srv.Logger(), srv.Config().GetGracefulTimeout())
}
func gracefulShutdown(hs *http.Server, logger *log.Logger, timeout time.Duration) {
// For a channel used for notification of just one signal value, a buffer of
// size 1 is sufficient.
stop := make(chan os.Signal, 1)
// We'll accept graceful shutdowns when quit via SIGINT (Ctrl+C) or SIGTERM
// (Ctrl+/). SIGKILL, SIGQUIT will not be caught.
signal.Notify(stop, os.Interrupt, syscall.SIGTERM, syscall.SIGINT)
sigReceived := <-stop
logger.Printf("Signal received: %+v", sigReceived)
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
logger.Printf("\nShutdown with timeout: %s\n", timeout)
if err := hs.Shutdown(ctx); err != nil {
logger.Printf("Shutdown error: %v\n", err)
} else {
logger.Println("Server stopped.")
}
}