From 2d50164213cd36ae9e9d53a389840c6ada3c4ca6 Mon Sep 17 00:00:00 2001 From: FernandoJVideira <03.pleaser-minster@icloud.com> Date: Fri, 30 Jan 2026 01:25:45 +0000 Subject: [PATCH] feat: added App struct --- cmd/api/main.go | 52 +++++++++++++++++++++++++++++++++++-------------- 1 file changed, 37 insertions(+), 15 deletions(-) diff --git a/cmd/api/main.go b/cmd/api/main.go index 3a194be..a15e91b 100644 --- a/cmd/api/main.go +++ b/cmd/api/main.go @@ -45,6 +45,26 @@ var interruptSignals = []os.Signal{ syscall.SIGINT, } +type App struct { + config config.Config + store db.Store + taskDistributor worker.TaskDistributor + tokenMaker auth.Maker + userService service.UserService + authService service.AuthService +} + +func NewApp(config config.Config, store db.Store, taskDistributor worker.TaskDistributor, tokenMaker auth.Maker, userService service.UserService, authService service.AuthService) *App { + return &App{ + config: config, + store: store, + taskDistributor: taskDistributor, + tokenMaker: tokenMaker, + userService: userService, + authService: authService, + } +} + // @title Simple Bank API // @version 1.0 // @description API for Simple Bank application @@ -103,10 +123,12 @@ func main() { wg, ctx := errgroup.WithContext(ctx) - runGinServer(ctx, wg, config, store, taskDist, tokenMaker, userService, authService) - runTaskProcessor(ctx, wg, redisOpt, store, config) - //runGatewayServer(ctx, wg, config, store, taskDist, tokenMaker, userService, authService) - //runGrpcServer(ctx, wg, config, store, taskDist, tokenMaker, userService, authService) + app := NewApp(config, store, taskDist, tokenMaker, userService, authService) + + runGinServer(ctx, wg, app) + runTaskProcessor(ctx, wg, redisOpt, app.store, app.config) + //runGatewayServer(ctx, wg, app) + //runGrpcServer(ctx, wg, app) err = wg.Wait() if err != nil { @@ -144,14 +166,14 @@ func runDBMigration(migrationURL, dbSource string) { log.Info().Msg("db migrated successfully") } -func runGinServer(ctx context.Context, wg *errgroup.Group, config config.Config, store db.Store, td worker.TaskDistributor, tokenMaker auth.Maker, userService service.UserService, authService service.AuthService) { - s, err := api.NewServer(config, store, td, tokenMaker, userService, authService) +func runGinServer(ctx context.Context, wg *errgroup.Group, app *App) { + s, err := api.NewServer(app.config, app.store, app.taskDistributor, app.tokenMaker, app.userService, app.authService) if err != nil { log.Fatal().Err(err).Msg("cannot create server") } wg.Go(func() error { - err = s.Start(config.HTTPServerAddr) + err = s.Start(app.config.HTTPServerAddr) if err != nil { log.Fatal().Err(err).Msg("cannot start server:") return err @@ -172,8 +194,8 @@ func runGinServer(ctx context.Context, wg *errgroup.Group, config config.Config, }) } -func runGrpcServer(ctx context.Context, wg *errgroup.Group, config config.Config, store db.Store, taskDistributor worker.TaskDistributor, tokenMaker auth.Maker, userService service.UserService, authService service.AuthService) { - server, err := gapi.NewServer(config, store, taskDistributor, tokenMaker, userService, authService) +func runGrpcServer(ctx context.Context, wg *errgroup.Group, app *App) { + server, err := gapi.NewServer(app.config, app.store, app.taskDistributor, app.tokenMaker, app.userService, app.authService) if err != nil { log.Fatal().Err(err).Msg("cannot create server:") } @@ -184,7 +206,7 @@ func runGrpcServer(ctx context.Context, wg *errgroup.Group, config config.Config pb.RegisterSimpleBankServer(grpcServer, server) reflection.Register(grpcServer) - listener, err := net.Listen("tcp", config.GrpcServerAddr) + listener, err := net.Listen("tcp", app.config.GrpcServerAddr) if err != nil { log.Fatal().Err(err).Msg("cannot create listener") } @@ -211,8 +233,8 @@ func runGrpcServer(ctx context.Context, wg *errgroup.Group, config config.Config }) } -func runGatewayServer(ctx context.Context, wg *errgroup.Group, config config.Config, store db.Store, taskDistributor worker.TaskDistributor, tokenMaker auth.Maker, userService service.UserService, authService service.AuthService) { - server, err := gapi.NewServer(config, store, taskDistributor, tokenMaker, userService, authService) +func runGatewayServer(ctx context.Context, wg *errgroup.Group, app *App) { + server, err := gapi.NewServer(app.config, app.store, app.taskDistributor, app.tokenMaker, app.userService, app.authService) if err != nil { log.Fatal().Err(err).Msg("cannot create server:") } @@ -246,7 +268,7 @@ func runGatewayServer(ctx context.Context, wg *errgroup.Group, config config.Con c := cors.New( cors.Options{ - AllowedOrigins: config.AllowedOrigins, + AllowedOrigins: app.config.AllowedOrigins, AllowedMethods: []string{ http.MethodGet, http.MethodPost, @@ -262,13 +284,13 @@ func runGatewayServer(ctx context.Context, wg *errgroup.Group, config config.Con "X-CSRF-Token", }, AllowCredentials: true, - Debug: config.Environment == "development", + Debug: app.config.Environment == "development", }, ) handler := c.Handler(gapi.HTTPLogger(mux)) httpServer := &http.Server{ Handler: handler, - Addr: config.HTTPServerAddr, + Addr: app.config.HTTPServerAddr, } wg.Go(func() error {