-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
132 lines (108 loc) · 3.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
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
package main
import (
"errors"
"fmt"
"golang.org/x/net/context"
"net/http"
"os"
"os/signal"
config "receipt-wrangler/api/internal/env"
"receipt-wrangler/api/internal/logging"
"receipt-wrangler/api/internal/repositories"
"receipt-wrangler/api/internal/routers"
"receipt-wrangler/api/internal/wranglerasynq"
"syscall"
"time"
"github.com/go-chi/chi/v5"
"gopkg.in/gographics/imagick.v3/imagick"
)
func main() {
err := logging.InitLog()
if err != nil {
fmt.Println("Failed to initialize log")
os.Exit(1)
}
logging.LogStd(logging.LOG_LEVEL_INFO, "Initializing...")
err = config.SetConfigs()
if err != nil {
logging.LogStd(logging.LOG_LEVEL_FATAL, err.Error())
}
config.CheckRequiredEnvironmentVariables()
err = repositories.Connect()
if err != nil {
logging.LogStd(logging.LOG_LEVEL_FATAL, err.Error())
}
err = repositories.MakeMigrations()
if err != nil {
logging.LogStd(logging.LOG_LEVEL_FATAL, err.Error())
}
err = repositories.InitDB()
if err != nil {
logging.LogStd(logging.LOG_LEVEL_FATAL, err.Error())
}
err = repositories.ConnectToRedis()
if err != nil {
logging.LogStd(logging.LOG_LEVEL_FATAL, fmt.Errorf("redis connection error: "+err.Error()))
}
defer repositories.ShutdownAsynqClient()
err = wranglerasynq.StartEmbeddedAsynqServer()
if err != nil {
logging.LogStd(logging.LOG_LEVEL_FATAL, fmt.Errorf("asynq worker error: "+err.Error()))
}
defer wranglerasynq.ShutDownEmbeddedAsynqServer()
err = wranglerasynq.StartEmbeddedAsynqScheduler()
if err != nil {
logging.LogStd(logging.LOG_LEVEL_FATAL, fmt.Errorf("asynq server error: "+err.Error()))
}
defer wranglerasynq.ShutDownEmbeddedAsynqScheduler()
logging.LogStd(logging.LOG_LEVEL_INFO, "Initializing Imagick...")
imagick.Initialize()
defer imagick.Terminate()
systemSettingsRepository := repositories.NewSystemSettingsRepository(nil)
systemSettings, err := systemSettingsRepository.GetSystemSettings()
if err != nil {
logging.LogStd(logging.LOG_LEVEL_FATAL, err.Error())
}
if systemSettings.EmailPollingInterval > 0 &&
systemSettings.ReceiptProcessingSettingsId != nil {
err = wranglerasynq.StartEmailPolling()
if err != nil {
logging.LogStd(logging.LOG_LEVEL_FATAL, err.Error())
}
}
err = wranglerasynq.StartSystemCleanUpTasks()
if err != nil {
logging.LogStd(logging.LOG_LEVEL_FATAL, err.Error())
}
stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt, syscall.SIGTERM)
router := routers.BuildRootRouter()
httpServer := startHttpServer(router)
<-stop
wranglerasynq.ShutDownEmbeddedAsynqServer()
wranglerasynq.ShutDownEmbeddedAsynqScheduler()
repositories.ShutdownAsynqClient()
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
err = httpServer.Shutdown(ctx)
if err != nil {
logging.LogStd(logging.LOG_LEVEL_FATAL, err.Error())
}
}
func startHttpServer(router *chi.Mux) *http.Server {
srv := &http.Server{
Handler: router,
Addr: "0.0.0.0:8081",
WriteTimeout: 5 * time.Minute,
ReadTimeout: 5 * time.Minute,
}
logging.LogStd(logging.LOG_LEVEL_INFO, "Initialize completed")
logging.LogStd(logging.LOG_LEVEL_INFO, "Listening on port 8081")
go func() {
err := srv.ListenAndServe()
if err != nil && !errors.Is(err, http.ErrServerClosed) {
logging.LogStd(logging.LOG_LEVEL_FATAL, err.Error())
}
}()
return srv
}