-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
107 lines (86 loc) · 2.3 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
package main
import (
"context"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"sync"
"syscall"
"time"
"github.com/entain-test-task/configuration"
"github.com/entain-test-task/controller"
"github.com/entain-test-task/middleware"
"github.com/entain-test-task/repository"
)
func main() {
// Load the configuration
cfg, err := configuration.Load()
if err != nil {
log.Fatal(fmt.Errorf("configuration loading failed: %w", err))
}
// Initialize the db store
store := repository.NewStore(cfg)
defer store.Close()
// Initialize the controllers
controllers := controller.NewControllers(cfg, store)
// Initialize the router
router := middleware.Router(controllers)
// Start a goroutine to cancel the latest odd transaction records.
ctx, cancelCtx := context.WithCancel(context.Background())
defer cancelCtx()
var waitGroup sync.WaitGroup
waitGroup.Add(1)
go startCancelLatestOddTransactionRecords(ctx, cfg, controllers, &waitGroup)
// Handle SIGINT and SIGTERM.
ch := make(chan os.Signal, 1)
signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
// Start the server in a separate goroutine.
server := &http.Server{
Addr: ":" + cfg.ServerPort,
Handler: router,
ReadHeaderTimeout: 5 * time.Second,
}
waitGroup.Add(1)
go func() {
defer waitGroup.Done()
log.Println("Starting server on the port " + cfg.ServerPort + "...")
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatal(err)
}
}()
// Wait for a shutdown signal.
<-ch
// Cancel the context.
cancelCtx()
// Shutdown the server.
if err := server.Shutdown(ctx); err != nil {
log.Print(err)
}
// Wait for all goroutines to finish.
waitGroup.Wait()
log.Println("Shutting down...")
}
func startCancelLatestOddTransactionRecords(
ctx context.Context,
cfg *configuration.Config,
controllers *controller.Controllers,
waitGroup *sync.WaitGroup,
) {
defer waitGroup.Done()
if cfg.CancelOddRecordsMinutesInterval == 0 {
log.Println("CancelOddRecordsMinutesInterval is set to 0. Canceling odd records is disabled.")
return
}
ticker := time.NewTicker(time.Duration(cfg.CancelOddRecordsMinutesInterval) * time.Minute)
defer ticker.Stop()
for {
select {
case <-ticker.C:
controllers.Transaction.CancelLatestOddTransactionRecords()
case <-ctx.Done():
return
}
}
}