-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
220 lines (175 loc) · 6.59 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package main
import (
"fmt"
"net/http"
"os"
"receipt-wrangler/api/internal/corspolicy"
"receipt-wrangler/api/internal/email"
config "receipt-wrangler/api/internal/env"
"receipt-wrangler/api/internal/logging"
"receipt-wrangler/api/internal/middleware"
"receipt-wrangler/api/internal/repositories"
"receipt-wrangler/api/internal/routers"
"receipt-wrangler/api/internal/services"
"time"
jwtmiddleware "github.com/auth0/go-jwt-middleware/v2"
"github.com/go-chi/chi/v5"
"gopkg.in/gographics/imagick.v2/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())
}
logging.LogStd(logging.LOG_LEVEL_INFO, "Initializing Imagick...")
imagick.Initialize()
defer imagick.Terminate()
if config.GetDeployEnv() != "test" {
userRepository := repositories.NewUserRepository(nil)
err = userRepository.CreateUserIfNoneExist()
}
if err != nil {
logging.LogStd(logging.LOG_LEVEL_FATAL, err.Error())
}
err = tryStartEmailPolling()
if err != nil {
logging.LogStd(logging.LOG_LEVEL_FATAL, err.Error())
}
router := initRoutes()
serve(router)
}
func serve(router *chi.Mux) {
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")
logging.LogStd(logging.LOG_LEVEL_FATAL, srv.ListenAndServe())
}
func initRoutes() *chi.Mux {
tokenValidator, err := services.InitTokenValidator()
if err != nil {
logging.LogStd(logging.LOG_LEVEL_FATAL, err.Error())
}
tokenValidatorMiddleware := jwtmiddleware.New(tokenValidator.ValidateToken)
env := config.GetDeployEnv()
rootRouter := chi.NewRouter()
// TODO: this policy is not ready for production yet. Need to add more configuration options to make sure we aren't using less secure options
if env == "dev" {
cors := corspolicy.GetCorsPolicy()
rootRouter.Use(cors.Handler)
}
// Token Refresh Router
refreshRouter := routers.BuildTokenRefreshRouter(tokenValidatorMiddleware)
rootRouter.Mount("/api/token", refreshRouter)
// Signup Router
signUpRouter := routers.BuildSignUpRouter(tokenValidatorMiddleware)
rootRouter.Mount("/api/signUp", signUpRouter)
// Login Router
loginRouter := routers.BuildLoginRouter(tokenValidatorMiddleware)
rootRouter.Mount("/api/login", loginRouter)
// Logout router
logoutRouter := routers.BuildLogoutRouter(tokenValidatorMiddleware)
rootRouter.Mount("/api/logout", logoutRouter)
// Receipt Router
receiptRouter := routers.BuildReceiptRouter(tokenValidatorMiddleware)
rootRouter.Mount("/api/receipt", receiptRouter)
// Receipt Image Router
receiptImageRouter := routers.BuildReceiptImageRouter(tokenValidatorMiddleware)
rootRouter.Mount("/api/receiptImage", receiptImageRouter)
// Comment Router
commentRouter := routers.BuildCommentRouter(tokenValidatorMiddleware)
rootRouter.Mount("/api/comment", commentRouter)
// Tag Router
tagRouter := routers.BuildTagRouter(tokenValidatorMiddleware)
rootRouter.Mount("/api/tag", tagRouter)
// Category Router
categoryRouter := routers.BuildCategoryRouter(tokenValidatorMiddleware)
rootRouter.Mount("/api/category", categoryRouter)
// User Router
userRouter := routers.BuildUserRouter(tokenValidatorMiddleware)
rootRouter.Mount("/api/user", userRouter)
// Add validaiton on update group that at least one user has owner, and that must have at least 1 user
// Group Router
groupRouter := routers.BuildGroupRouter(tokenValidatorMiddleware)
rootRouter.Mount("/api/group", groupRouter)
// Feature Config Router
featureConfigRouter := routers.BuildFeatureConfigRouter(tokenValidatorMiddleware)
rootRouter.Mount("/api/featureConfig", featureConfigRouter)
// Migration router
migrationRouter := chi.NewRouter()
migrationRouter.Use(middleware.MoveJWTCookieToHeader, tokenValidatorMiddleware.CheckJWT)
rootRouter.Mount("/api/migrate", migrationRouter)
// Search router
searchRouter := routers.BuildSearchRouter(tokenValidatorMiddleware)
rootRouter.Mount("/api/search", searchRouter)
// Notification router
notificationRouter := routers.BuildNotificationRouter(tokenValidatorMiddleware)
rootRouter.Mount("/api/notifications", notificationRouter)
//User Preferences router
userPreferencesRouter := routers.BuildUserPreferencesRouter(tokenValidatorMiddleware)
rootRouter.Mount("/api/userPreferences", userPreferencesRouter)
// Dashboard router
dashboardRouter := routers.BuildDashboardRouter(tokenValidatorMiddleware)
rootRouter.Mount("/api/dashboard", dashboardRouter)
// System email router
systemEmailRouter := routers.BuildSystemEmailRouter(tokenValidatorMiddleware)
rootRouter.Mount("/api/systemEmail", systemEmailRouter)
// System Task router
systemTaskRouter := routers.BuildSystemTaskRouter(tokenValidatorMiddleware)
rootRouter.Mount("/api/systemTask", systemTaskRouter)
// Receipt Processing Settings router
receiptProcessingSettingsRouter := routers.BuildReceiptProcessingSettingsRouter(tokenValidatorMiddleware)
rootRouter.Mount("/api/receiptProcessingSettings", receiptProcessingSettingsRouter)
// Prompt router
promptRouter := routers.BuildPromptRouter(tokenValidatorMiddleware)
rootRouter.Mount("/api/prompt", promptRouter)
// System Settings router
systemSettingsRouter := routers.BuildSystemSettingsRouter(tokenValidatorMiddleware)
rootRouter.Mount("/api/systemSettings", systemSettingsRouter)
// Import router
importRouter := routers.BuildImportRouter(tokenValidatorMiddleware)
rootRouter.Mount("/api/import", importRouter)
return rootRouter
}
func tryStartEmailPolling() error {
systemSettingsRepository := repositories.NewSystemSettingsRepository(nil)
systemSettings, err := systemSettingsRepository.GetSystemSettings()
if err != nil {
return err
}
systemSettingsService := services.NewSystemSettingsService(nil)
featureConfig, err := systemSettingsService.GetFeatureConfig()
if err != nil {
return err
}
if systemSettings.EmailPollingInterval > 0 && featureConfig.AiPoweredReceipts {
err = email.StartEmailPolling()
if err != nil {
return err
}
}
return nil
}