-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
145 lines (123 loc) · 3.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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package main
import (
"context"
"database/sql"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"sync"
"syscall"
"time"
"github.com/go-chi/chi"
"github.com/maxim-nazarenko/qonto-interview/internal/qonto"
"github.com/maxim-nazarenko/qonto-interview/internal/qonto/api"
"github.com/maxim-nazarenko/qonto-interview/internal/qonto/app"
"github.com/maxim-nazarenko/qonto-interview/internal/qonto/core"
"github.com/maxim-nazarenko/qonto-interview/internal/qonto/storage"
"github.com/maxim-nazarenko/qonto-interview/internal/qonto/utils"
)
func main() {
if err := run(os.Args[1:]); err != nil {
log.Fatal(err)
os.Exit(1)
}
os.Exit(0)
}
func run(args []string) error {
appCtx, cancel := context.WithCancel(context.Background())
defer cancel()
appLogger := qonto.NewInstanceLogger(os.Stdout, "Qonto")
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, os.Interrupt, syscall.SIGQUIT, syscall.SIGHUP, syscall.SIGTERM)
go func() {
<-signalChan
appLogger.Info("received interruption request, closing the app")
cancel()
}()
config, err := app.ConfigurationFromEnv(os.Getenv)
if err != nil {
return err
}
mysqlConfig := storage.NewMysqlConfig()
mysqlConfig.User = config.DB.User
mysqlConfig.Passwd = config.DB.Password
mysqlConfig.DBName = config.DB.Name
mysqlConfig.Net = "tcp"
mysqlConfig.Addr = config.DB.Address
mysqlStorage, err := storage.NewMysqlStorage(mysqlConfig)
if err != nil {
return err
}
defer func() {
if err := mysqlStorage.Close(); err != nil {
appLogger.Error("could not close database connection: %v", err)
}
}()
if err := dbConnect(appCtx, mysqlStorage, appLogger); err != nil {
return err
}
projectRoot := utils.ProjectRootDir()
if err := storage.Migrate("file://"+projectRoot+"/migrations/", mysqlStorage.DB()); err != nil {
return fmt.Errorf("migrations failed: %v", err)
}
appLogger.Info("migration completed")
qontoAPI := api.NewAPI(core.NewQontoTransferManager(mysqlStorage))
router := chi.NewRouter()
router.Post("/v1/transfers", qontoAPI.HandleTransfers)
server := http.Server{
Addr: config.ListenAddress,
ReadTimeout: 30 * time.Second,
WriteTimeout: 30 * time.Second,
Handler: router,
}
wg := sync.WaitGroup{}
// starting HTTP server in background
wg.Add(1)
go func(srv *http.Server, logger qonto.Logger) {
defer wg.Done()
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
logger.Error(err.Error())
}
}(&server, appLogger.SubLogger("http server"))
// app shutdown watcher that will stop HTTP server
wg.Add(1)
go func(ctx context.Context, srv *http.Server, logger qonto.Logger) {
defer wg.Done()
<-ctx.Done()
timeout := 10 * time.Second
shutdownCtx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
logger.Info("trying gracefully shutdown in %v", timeout)
if err := srv.Shutdown(shutdownCtx); err != nil {
logger.Error(err.Error())
}
logger.Info("done")
}(appCtx, &server, appLogger.SubLogger("http server"))
wg.Wait()
appLogger.Info("all tasks stopped, exiting application")
return nil
}
func dbConnect(ctx context.Context, mysqlStorage storage.Storage, appLogger qonto.Logger) error {
dbPingCtx, dbPingCancel := context.WithTimeout(ctx, 30*time.Second)
defer dbPingCancel()
dbUpWaitFunc := func(db *sql.DB) (bool, error) {
for {
select {
case <-dbPingCtx.Done():
return false, dbPingCtx.Err()
case <-time.After(1 * time.Second):
if err := db.PingContext(dbPingCtx); err != nil {
appLogger.Info("db ping failed: %v", err)
return true, err
}
return false, nil
}
}
}
if err := mysqlStorage.Wait(dbUpWaitFunc); err != nil {
return err
}
return nil
}