-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
68 lines (56 loc) · 1.65 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
package main
import (
"Mining-Profitability/pkg/appcontext"
"Mining-Profitability/pkg/applog"
"Mining-Profitability/pkg/config"
"Mining-Profitability/pkg/miningprofitability/imagedownload"
"Mining-Profitability/pkg/miningprofitability/statsgenerator"
"context"
"flag"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
)
var (
shutdownTimeout = 5 * time.Second
)
func main() {
configFile := flag.String("config", "config.yaml", "path to config file")
flag.Parse()
cfg, err := config.New(*configFile)
if err != nil {
log.Fatalf("error getting the config: %s", err)
}
logger := applog.New(cfg)
router := http.NewServeMux()
server := &http.Server{Addr: cfg.Address, Handler: router}
appContext, appCtxCancel, err := appcontext.New(cfg, logger)
if err != nil {
logger.Fatalf("error creating the app context: %s", err)
}
router.Handle("/chart", imagedownload.NewImageHandler(appContext))
router.Handle("/data", statsgenerator.NewDataHandler(appContext))
signals := make(chan os.Signal, 1)
signal.Notify(signals, syscall.SIGINT, syscall.SIGKILL)
shutdownComplete := make(chan struct{}, 1)
go func() {
<-signals
ctx, cancel := context.WithTimeout(context.Background(), shutdownTimeout)
defer cancel()
logger.Info("shutting down the server")
if err := server.Shutdown(ctx); err != nil {
logger.Fatalf("error shutting down the server: %s", err)
}
close(shutdownComplete)
}()
logger.Infof("starting the server on %s", cfg.Address)
if err := server.ListenAndServe(); err != http.ErrServerClosed {
logger.Fatalf("error starting the server: %s", err)
}
appCtxCancel()
<-shutdownComplete
logger.Info("server shutdown successfully")
}