-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.go
More file actions
549 lines (459 loc) · 18.3 KB
/
Copy pathapp.go
File metadata and controls
549 lines (459 loc) · 18.3 KB
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
package app
import (
"context"
"fmt"
"kubecloud/internal"
"kubecloud/internal/activities"
"kubecloud/internal/metrics"
"kubecloud/internal/notification"
"kubecloud/middlewares"
"kubecloud/models"
"net"
"net/http"
"os"
"strings"
"time"
substrate "github.com/threefoldtech/tfchain/clients/tfchain-client-go"
"github.com/threefoldtech/tfgrid-sdk-go/grid-client/deployer"
"github.com/threefoldtech/tfgrid-sdk-go/grid-client/graphql"
proxy "github.com/threefoldtech/tfgrid-sdk-go/grid-proxy/pkg/client"
"github.com/xmonader/ewf"
"kubecloud/internal/logger"
"github.com/gin-gonic/gin"
"github.com/stripe/stripe-go/v82"
swaggerFiles "github.com/swaggo/files"
ginSwagger "github.com/swaggo/gin-swagger"
// Import the generated docs package
_ "kubecloud/docs"
)
// App holds all configurations for the app
type App struct {
router *gin.Engine
httpServer *http.Server
config internal.Configuration
handlers Handler
db models.DB
redis *internal.RedisClient
sseManager *internal.SSEManager
notificationService *notification.NotificationService
gridClient deployer.TFPluginClient
appCtx context.Context
appCancel context.CancelFunc
metrics *metrics.Metrics
}
// NewApp create new instance of the app with all configs
func NewApp(ctx context.Context, config internal.Configuration) (*App, error) {
// Disable gin's default logging since we're using zerolog
gin.DisableConsoleColor()
gin.SetMode(gin.ReleaseMode)
// Create router without default middleware
router := gin.New()
// Add recovery middleware
router.Use(gin.Recovery())
// Add our custom logging middleware
router.Use(middlewares.GinLoggerMiddleware())
stripe.Key = config.StripeSecret
tokenHandler := internal.NewTokenHandler(
config.JwtToken.Secret,
time.Duration(config.JwtToken.AccessExpiryMinutes)*time.Minute,
time.Duration(config.JwtToken.RefreshExpiryHours)*time.Hour,
)
db, err := models.NewSqliteDB(config.Database.File)
if err != nil {
logger.GetLogger().Error().Err(err).Msg("Failed to create user storage")
return nil, fmt.Errorf("failed to create user storage: %w", err)
}
gridProxy := proxy.NewRetryingClient(proxy.NewClient(config.GridProxyURL))
manager := substrate.NewManager(config.TFChainURL)
substrateClient, err := manager.Substrate()
if err != nil {
logger.GetLogger().Error().Err(err).Msg("failed to connect to substrate client")
return nil, fmt.Errorf("failed to connect to substrate client: %w", err)
}
graphqlURL := []string{config.GraphqlURL}
graphqlClient, err := graphql.NewGraphQl(graphqlURL...)
if err != nil {
logger.GetLogger().Error().Err(err).Msg("failed to connect to graphql client")
return nil, fmt.Errorf("failed to connect to graphql client: %w", err)
}
firesquidURL := []string{config.FiresquidURL}
firesquidClient, err := graphql.NewGraphQl(firesquidURL...)
if err != nil {
logger.GetLogger().Error().Err(err).Msg("failed to connect to firesquid client")
return nil, fmt.Errorf("failed to connect to firesquid client: %w", err)
}
redisClient, err := internal.NewRedisClient(config.Redis)
if err != nil {
logger.GetLogger().Error().Err(err).Msg("Failed to create Redis client")
return nil, fmt.Errorf("failed to create Redis client: %w", err)
}
sseManager := internal.NewSSEManager()
pluginOpts := []deployer.PluginOpt{
deployer.WithNetwork(config.SystemAccount.Network),
deployer.WithDisableSentry(),
}
if config.Debug {
pluginOpts = append(pluginOpts, deployer.WithLogs())
}
gridClient, err := deployer.NewTFPluginClient(
config.SystemAccount.Mnemonic,
pluginOpts...,
)
if err != nil {
return nil, fmt.Errorf("failed to create TF grid client: %w", err)
}
// create storage for workflows
ewfStore := models.NewGormStore(db.GetDB())
// initialize workflow ewfEngine
ewfEngine, err := ewf.NewEngine(ewfStore)
if err != nil {
logger.GetLogger().Error().Err(err).Msg("failed to init EWF engine")
return nil, fmt.Errorf("failed to init workflow engine: %w", err)
}
metrics := metrics.NewMetrics()
notificationConfig := config.Notification
mailService := internal.NewMailService(config.MailSender.SendGridKey, metrics)
sseNotifier := notification.NewSSENotifier(sseManager)
emailNotifier := notification.NewEmailNotifier(mailService, config.MailSender.Email, notificationConfig.EmailTemplatesDirPath)
err = emailNotifier.ParseTemplates()
if err != nil {
return nil, fmt.Errorf("failed to init notification templates: %w", err)
}
notificationService, err := notification.NewNotificationService(db, ewfEngine, notificationConfig)
if err != nil {
return nil, fmt.Errorf("failed to create notification service: %w", err)
}
notificationService.RegisterNotifier(sseNotifier)
notificationService.RegisterNotifier(emailNotifier)
if err := notificationService.ValidateConfigsChannelsAgainstRegistered(); err != nil {
return nil, fmt.Errorf("failed to validate notification configs channels against registered notifiers: %w", err)
}
// Create an app-level context for coordinating shutdown
systemIdentity, err := substrate.NewIdentityFromSr25519Phrase(config.SystemAccount.Mnemonic)
if err != nil {
return nil, fmt.Errorf("failed to create system identity: %w", err)
}
sshPublicKeyBytes, err := os.ReadFile(config.SSH.PublicKeyPath)
if err != nil {
return nil, fmt.Errorf("failed to read SSH public key from %s: %w", config.SSH.PublicKeyPath, err)
}
sshPublicKey := strings.TrimSpace(string(sshPublicKeyBytes))
appCtx, appCancel := context.WithCancel(ctx)
// Derive sponsor (system) account SS58 address once
sponsorKeyPair, err := internal.KeyPairFromMnemonic(config.SystemAccount.Mnemonic)
if err != nil {
appCancel()
return nil, fmt.Errorf("failed to create sponsor keypair from system account: %w", err)
}
sponsorAddress, err := internal.AccountAddressFromKeypair(sponsorKeyPair)
if err != nil {
appCancel()
return nil, fmt.Errorf("failed to create sponsor address from keypair: %w", err)
}
// Validate KYC configuration
if strings.TrimSpace(config.KYCVerifierAPIURL) == "" {
appCancel()
return nil, fmt.Errorf("KYC verifier API URL is required")
}
if strings.TrimSpace(config.KYCChallengeDomain) == "" {
appCancel()
return nil, fmt.Errorf("KYC challenge domain is required")
}
// Initialize KYC client
kycClient := internal.NewKYCClient(
config.KYCVerifierAPIURL,
config.KYCChallengeDomain,
nil, // Use default http.Client
)
if valid, err := kycClient.IsValidSponsor(appCtx, sponsorAddress); err != nil || !valid {
appCancel()
if err != nil {
return nil, fmt.Errorf("failed to validate sponsor address, %w", err)
}
return nil, fmt.Errorf("the provided sponsor address can't be used as a sponsor")
}
handler := NewHandler(tokenHandler, db, config, mailService, gridProxy,
substrateClient, graphqlClient, firesquidClient, redisClient,
sseManager, ewfEngine, config.SystemAccount.Network, sshPublicKey,
systemIdentity, kycClient, sponsorKeyPair, sponsorAddress, metrics, notificationService, gridClient)
app := &App{
router: router,
config: config,
handlers: *handler,
redis: redisClient,
db: db,
sseManager: sseManager,
notificationService: notificationService,
appCtx: appCtx,
appCancel: appCancel,
gridClient: gridClient,
metrics: metrics,
}
activities.RegisterEWFWorkflows(
ewfEngine,
app.config,
app.db,
app.handlers.mailService,
app.handlers.substrateClient,
app.handlers.kycClient,
sponsorAddress,
sponsorKeyPair,
app.metrics,
app.notificationService,
gridProxy,
)
app.registerHandlers()
return app, nil
}
// registerHandlers registers all routes
func (app *App) registerHandlers() {
app.metrics.RegisterMetricsEndpoint(app.router)
app.router.Use(middlewares.CorsMiddleware())
app.router.Use(app.metrics.Middleware())
app.metrics.StartGORMMetricsCollector(app.db.GetDB(), metrics.MetricsCollectorInterval)
app.metrics.StartGoRuntimeMetricsCollector(metrics.MetricsCollectorInterval)
v1 := app.router.Group("/api/v1")
{
v1.GET("/health", app.handlers.HealthHandler)
v1.GET("/workflow/:workflow_id", app.handlers.GetWorkflowStatus)
v1.GET("/twins/:twin_id/account", app.handlers.GetAccountIDHandler)
v1.GET("/system/maintenance/status", app.handlers.GetMaintenanceModeHandler)
v1.GET("/stats", app.handlers.GetStatsHandler)
v1.GET("/nodes", app.handlers.ListAllGridNodesHandler)
v1.GET("/nodes/:node_id/storage-pool", app.handlers.GetNodeStoragePoolHandler)
adminGroup := v1.Group("")
adminGroup.Use(middlewares.AdminMiddleware(app.handlers.tokenManager))
{
usersGroup := adminGroup.Group("/users")
{
usersGroup.GET("", app.handlers.ListUsersHandler)
usersGroup.GET("/paginated", app.handlers.ListUsersPaginatedHandler)
usersGroup.DELETE("/:user_id", app.handlers.DeleteUsersHandler)
usersGroup.POST("/:user_id/credit", app.handlers.CreditUserHandler)
}
usersGroup.POST("/mail", app.handlers.SendMailToAllUsersHandler)
adminGroup.GET("/invoices", app.handlers.ListAllInvoicesHandler)
adminGroup.GET("/invoices/paginated", app.handlers.ListAllInvoicesPaginatedHandler)
adminGroup.GET("/pending-records", app.handlers.ListPendingRecordsHandler)
adminGroup.GET("/pending-records/paginated", app.handlers.ListPendingRecordsPaginatedHandler)
vouchersGroup := adminGroup.Group("/vouchers")
{
vouchersGroup.POST("/generate", app.handlers.GenerateVouchersHandler)
vouchersGroup.GET("", app.handlers.ListVouchersHandler)
vouchersGroup.GET("/paginated", app.handlers.ListVouchersPaginatedHandler)
}
}
systemGroup := adminGroup.Group("/system")
{
systemGroup.PUT("/maintenance/status", app.handlers.SetMaintenanceModeHandler)
}
userGroup := v1.Group("/user")
{
userGroup.POST("/register", app.handlers.RegisterHandler)
userGroup.POST("/register/verify", app.handlers.VerifyRegisterCode)
userGroup.POST("/login", app.handlers.LoginUserHandler)
userGroup.POST("/refresh", app.handlers.RefreshTokenHandler)
userGroup.POST("/forgot_password", app.handlers.ForgotPasswordHandler)
userGroup.POST("/forgot_password/verify", app.handlers.VerifyForgetPasswordCodeHandler)
authGroup := userGroup.Group("")
authGroup.Use(middlewares.UserMiddleware(app.handlers.tokenManager))
{
authGroup.GET("/", app.handlers.GetUserHandler)
authGroup.PUT("/change_password", app.handlers.ChangePasswordHandler)
authGroup.GET("/nodes", app.handlers.ListNodesHandler)
authGroup.GET("/nodes/rentable", app.handlers.ListRentableNodesHandler)
authGroup.GET("/nodes/rentable/paginated", app.handlers.ListRentableNodesPaginatedHandler)
authGroup.GET("/nodes/rented", app.handlers.ListRentedNodesHandler)
authGroup.GET("/nodes/rented/paginated", app.handlers.ListRentedNodesPaginatedHandler)
authGroup.POST("/nodes/:node_id", app.handlers.ReserveNodeHandler)
authGroup.DELETE("/nodes/unreserve/:contract_id", app.handlers.UnreserveNodeHandler)
authGroup.POST("/balance/charge", app.handlers.ChargeBalance)
authGroup.GET("/balance", app.handlers.GetUserBalance)
authGroup.PUT("/redeem/:voucher_code", app.handlers.RedeemVoucherHandler)
authGroup.GET("/invoice/:invoice_id", app.handlers.DownloadInvoiceHandler)
authGroup.GET("/invoice", app.handlers.ListUserInvoicesHandler)
authGroup.GET("/invoice/paginated", app.handlers.ListUserInvoicesPaginatedHandler)
authGroup.GET("/pending-records", app.handlers.ListUserPendingRecordsHandler)
authGroup.GET("/pending-records/paginated", app.handlers.ListUserPendingRecordsPaginatedHandler)
// SSH Key management
authGroup.GET("/ssh-keys", app.handlers.ListSSHKeysHandler)
authGroup.GET("/ssh-keys/paginated", app.handlers.ListSSHKeysPaginatedHandler)
authGroup.POST("/ssh-keys", app.handlers.AddSSHKeyHandler)
authGroup.DELETE("/ssh-keys/:ssh_key_id", app.handlers.DeleteSSHKeyHandler)
}
}
deployerGroup := v1.Group("")
deployerGroup.Use(middlewares.UserMiddleware(app.handlers.tokenManager))
{
deployerGroup.GET("/events", app.sseManager.HandleSSE)
deploymentGroup := deployerGroup.Group("/deployments")
{
deploymentGroup.POST("", app.handlers.HandleDeployCluster)
deploymentGroup.GET("", app.handlers.HandleListDeployments)
deploymentGroup.GET("/paginated", app.handlers.HandleListDeploymentsPaginated)
deploymentGroup.DELETE("", app.handlers.HandleDeleteAllDeployments)
deploymentGroup.GET("/:name", app.handlers.HandleGetDeployment)
deploymentGroup.GET("/:name/kubeconfig", app.handlers.HandleGetKubeconfig)
deploymentGroup.DELETE("/:name", app.handlers.HandleDeleteCluster)
deploymentGroup.POST("/:name/nodes", app.handlers.HandleAddNode)
deploymentGroup.DELETE("/:name/nodes/:node_name", app.handlers.HandleRemoveNode)
}
notificationGroup := deployerGroup.Group("/notifications")
{
notificationGroup.GET("", app.handlers.GetAllNotificationsHandler)
notificationGroup.GET("/unread", app.handlers.GetUnreadNotificationsHandler)
notificationGroup.PATCH("/read-all", app.handlers.MarkAllNotificationsReadHandler)
notificationGroup.DELETE("", app.handlers.DeleteAllNotificationsHandler)
notificationGroup.PATCH("/:notification_id/read", app.handlers.MarkNotificationReadHandler)
notificationGroup.PATCH("/:notification_id/unread", app.handlers.MarkNotificationUnreadHandler)
notificationGroup.DELETE("/:notification_id", app.handlers.DeleteNotificationHandler)
}
}
}
app.router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
}
func (app *App) StartBackgroundWorkers() {
go app.handlers.MonthlyInvoicesHandler()
go app.handlers.TrackUserDebt(app.gridClient)
go app.handlers.MonitorSystemBalanceAndHandleSettlement()
go app.handlers.TrackClusterHealth()
go app.handlers.TrackReservedNodeHealth(app.notificationService, app.handlers.proxyClient)
}
// Run starts the server
func (app *App) Run() error {
app.StartBackgroundWorkers()
// Start command socket
go app.startCommandSocket()
app.handlers.ewfEngine.ResumeRunningWorkflows()
app.httpServer = &http.Server{
Addr: fmt.Sprintf(":%s", app.config.Server.Port),
Handler: app.router,
}
logger.GetLogger().Info().Msgf("Starting server at %s:%s", app.config.Server.Host, app.config.Server.Port)
if err := app.httpServer.ListenAndServe(); err != nil && err != http.ErrServerClosed {
logger.GetLogger().Error().Err(err).Msg("Failed to start server")
return err
}
return nil
}
// Shutdown gracefully shuts down the server and worker manager
func (app *App) Shutdown(ctx context.Context) error {
// First, cancel the app context to signal all components to stop
if app.appCancel != nil {
app.appCancel()
}
if app.httpServer != nil {
if err := app.httpServer.Shutdown(ctx); err != nil {
logger.GetLogger().Error().Err(err).Msg("Failed to shutdown HTTP server")
}
}
if app.sseManager != nil {
app.sseManager.Stop()
}
if app.redis != nil {
if err := app.redis.Close(); err != nil {
logger.GetLogger().Error().Err(err).Msg("Failed to close Redis connection")
}
}
if app.db != nil {
if err := app.db.Close(); err != nil {
logger.GetLogger().Error().Err(err).Msg("Failed to close database connection")
}
}
if app.handlers.substrateClient != nil {
app.handlers.substrateClient.Close()
}
app.gridClient.Close()
logger.CloseLogger()
return nil
}
func (app *App) startCommandSocket() {
socketPath := "/tmp/myceliumcloud.sock"
os.Remove(socketPath)
listener, err := net.Listen("unix", socketPath)
if err != nil {
logger.GetLogger().Error().Err(err).Msg("failed to create command socket")
return
}
defer listener.Close()
defer os.Remove(socketPath)
logger.GetLogger().Info().Str("socket", socketPath).Msg("Command socket started")
for {
select {
case <-app.appCtx.Done():
logger.GetLogger().Info().Msg("command socket stopping")
return
default:
}
if unixListener, ok := listener.(*net.UnixListener); ok {
if err := unixListener.SetDeadline(time.Now().Add(1 * time.Second)); err != nil {
logger.GetLogger().Error().Err(err).Msg("failed to set deadline on listener")
}
}
conn, err := listener.Accept()
if err == nil {
go app.handleSocketCommand(conn)
continue
}
if netErr, ok := err.(net.Error); ok && netErr.Timeout() {
continue
}
if app.appCtx.Err() != nil {
return
}
logger.GetLogger().Error().Err(err).Msg("socket accept error")
continue
}
}
func (app *App) handleSocketCommand(conn net.Conn) {
defer conn.Close()
if err := conn.SetReadDeadline(time.Now().Add(5 * time.Second)); err != nil {
logger.GetLogger().Error().Err(err).Msg("failed to set read deadline")
return
}
buffer := make([]byte, 1024)
n, err := conn.Read(buffer)
if err != nil {
if _, writeErr := conn.Write([]byte("ERROR: Failed to read command\n")); writeErr != nil {
logger.GetLogger().Error().Err(writeErr).Msg("failed to write error response")
}
return
}
command := strings.TrimSpace(string(buffer[:n]))
logger.GetLogger().Debug().Str("command", command).Msg("Received socket command")
if command == "reload-notifications" {
app.handleReloadNotifications(conn)
return
}
response := fmt.Sprintf("ERROR: Unknown command '%s'\n", command)
if _, err := conn.Write([]byte(response)); err != nil {
logger.GetLogger().Error().Err(err).Msg("failed to write error response")
}
logger.GetLogger().Warn().Str("command", command).Msg("Unknown socket command received")
}
func (app *App) handleReloadNotifications(conn net.Conn) {
err := app.reloadNotificationConfig()
if err != nil {
response := fmt.Sprintf("ERROR: %v\n", err)
if _, writeErr := conn.Write([]byte(response)); writeErr != nil {
logger.GetLogger().Error().Err(writeErr).Msg("failed to write error response")
}
logger.GetLogger().Error().Err(err).Msg("Failed to reload notification config via socket")
return
}
if _, err := conn.Write([]byte("OK: Notification config reloaded successfully\n")); err != nil {
logger.GetLogger().Error().Err(err).Msg("failed to write success response")
return
}
logger.GetLogger().Info().Msg("Notification config reloaded via socket")
}
func (app *App) reloadNotificationConfig() error {
cfg, err := internal.LoadConfig()
if err != nil {
return fmt.Errorf("failed to load config: %w", err)
}
if err = app.notificationService.ReloadNotificationConfig(cfg.Notification); err != nil {
return fmt.Errorf("failed to reload notification config: %w", err)
}
return nil
}