feat: added App struct

This commit is contained in:
FernandoJVideira committed 2026-01-30 01:25:45 +00:00
1 parent 961bdd81d4
commit 2d50164213
1 file changed
+37 -15
+37 -15
View File
@@ -45,6 +45,26 @@ var interruptSignals = []os.Signal{
syscall.SIGINT, 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 // @title Simple Bank API
// @version 1.0 // @version 1.0
// @description API for Simple Bank application // @description API for Simple Bank application
@@ -103,10 +123,12 @@ func main() {
wg, ctx := errgroup.WithContext(ctx) wg, ctx := errgroup.WithContext(ctx)
runGinServer(ctx, wg, config, store, taskDist, tokenMaker, userService, authService) app := NewApp(config, store, taskDist, tokenMaker, userService, authService)
runTaskProcessor(ctx, wg, redisOpt, store, config)
//runGatewayServer(ctx, wg, config, store, taskDist, tokenMaker, userService, authService) runGinServer(ctx, wg, app)
//runGrpcServer(ctx, wg, config, store, taskDist, tokenMaker, userService, authService) runTaskProcessor(ctx, wg, redisOpt, app.store, app.config)
//runGatewayServer(ctx, wg, app)
//runGrpcServer(ctx, wg, app)
err = wg.Wait() err = wg.Wait()
if err != nil { if err != nil {
@@ -144,14 +166,14 @@ func runDBMigration(migrationURL, dbSource string) {
log.Info().Msg("db migrated successfully") 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) { func runGinServer(ctx context.Context, wg *errgroup.Group, app *App) {
s, err := api.NewServer(config, store, td, tokenMaker, userService, authService) s, err := api.NewServer(app.config, app.store, app.taskDistributor, app.tokenMaker, app.userService, app.authService)
if err != nil { if err != nil {
log.Fatal().Err(err).Msg("cannot create server") log.Fatal().Err(err).Msg("cannot create server")
} }
wg.Go(func() error { wg.Go(func() error {
err = s.Start(config.HTTPServerAddr) err = s.Start(app.config.HTTPServerAddr)
if err != nil { if err != nil {
log.Fatal().Err(err).Msg("cannot start server:") log.Fatal().Err(err).Msg("cannot start server:")
return err 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) { func runGrpcServer(ctx context.Context, wg *errgroup.Group, app *App) {
server, err := gapi.NewServer(config, store, taskDistributor, tokenMaker, userService, authService) server, err := gapi.NewServer(app.config, app.store, app.taskDistributor, app.tokenMaker, app.userService, app.authService)
if err != nil { if err != nil {
log.Fatal().Err(err).Msg("cannot create server:") 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) pb.RegisterSimpleBankServer(grpcServer, server)
reflection.Register(grpcServer) reflection.Register(grpcServer)
listener, err := net.Listen("tcp", config.GrpcServerAddr) listener, err := net.Listen("tcp", app.config.GrpcServerAddr)
if err != nil { if err != nil {
log.Fatal().Err(err).Msg("cannot create listener") 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) { func runGatewayServer(ctx context.Context, wg *errgroup.Group, app *App) {
server, err := gapi.NewServer(config, store, taskDistributor, tokenMaker, userService, authService) server, err := gapi.NewServer(app.config, app.store, app.taskDistributor, app.tokenMaker, app.userService, app.authService)
if err != nil { if err != nil {
log.Fatal().Err(err).Msg("cannot create server:") 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( c := cors.New(
cors.Options{ cors.Options{
AllowedOrigins: config.AllowedOrigins, AllowedOrigins: app.config.AllowedOrigins,
AllowedMethods: []string{ AllowedMethods: []string{
http.MethodGet, http.MethodGet,
http.MethodPost, http.MethodPost,
@@ -262,13 +284,13 @@ func runGatewayServer(ctx context.Context, wg *errgroup.Group, config config.Con
"X-CSRF-Token", "X-CSRF-Token",
}, },
AllowCredentials: true, AllowCredentials: true,
Debug: config.Environment == "development", Debug: app.config.Environment == "development",
}, },
) )
handler := c.Handler(gapi.HTTPLogger(mux)) handler := c.Handler(gapi.HTTPLogger(mux))
httpServer := &http.Server{ httpServer := &http.Server{
Handler: handler, Handler: handler,
Addr: config.HTTPServerAddr, Addr: app.config.HTTPServerAddr,
} }
wg.Go(func() error { wg.Go(func() error {