-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from Chuiko-GIT/di-container
Implemented Dependency Injection container for better modularity
- Loading branch information
Showing
6 changed files
with
286 additions
and
119 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package app | ||
|
||
import ( | ||
"context" | ||
"log" | ||
"net" | ||
|
||
"google.golang.org/grpc" | ||
"google.golang.org/grpc/credentials/insecure" | ||
"google.golang.org/grpc/reflection" | ||
|
||
"github.com/Chuiko-GIT/auth/internal/closer" | ||
"github.com/Chuiko-GIT/auth/internal/config" | ||
"github.com/Chuiko-GIT/auth/pkg/user_api" | ||
) | ||
|
||
type App struct { | ||
serviceProvider *ServiceProvider | ||
grpcServer *grpc.Server | ||
} | ||
|
||
func NewApp(ctx context.Context) (*App, error) { | ||
a := &App{} | ||
|
||
err := a.initDeps(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return a, nil | ||
} | ||
|
||
func (a *App) Run() error { | ||
defer func() { | ||
closer.CloseAll() | ||
closer.Wait() | ||
}() | ||
|
||
return a.runGRPCServer() | ||
} | ||
|
||
func (a *App) initDeps(ctx context.Context) error { | ||
inits := []func(context.Context) error{ | ||
a.initConfig, | ||
a.initServiceProvider, | ||
a.initGRPCServer, | ||
} | ||
|
||
for _, f := range inits { | ||
if err := f(ctx); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (a *App) initConfig(_ context.Context) error { | ||
return config.Load("local.env") | ||
} | ||
|
||
func (a *App) initServiceProvider(_ context.Context) error { | ||
a.serviceProvider = newServiceProvider() | ||
return nil | ||
} | ||
|
||
func (a *App) initGRPCServer(ctx context.Context) error { | ||
a.grpcServer = grpc.NewServer(grpc.Creds(insecure.NewCredentials())) | ||
|
||
reflection.Register(a.grpcServer) | ||
|
||
user_api.RegisterUserAPIServer(a.grpcServer, a.serviceProvider.UsersImpl(ctx)) | ||
|
||
return nil | ||
} | ||
|
||
func (a *App) runGRPCServer() error { | ||
log.Printf("GRPC server is running on %s", a.serviceProvider.GRPCConfig().Address()) | ||
|
||
list, err := net.Listen("tcp", a.serviceProvider.GRPCConfig().Address()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = a.grpcServer.Serve(list) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package app | ||
|
||
import ( | ||
"context" | ||
"log" | ||
|
||
"github.com/jackc/pgx/v4/pgxpool" | ||
|
||
"github.com/Chuiko-GIT/auth/internal/api/users" | ||
uImpl "github.com/Chuiko-GIT/auth/internal/api/users" | ||
"github.com/Chuiko-GIT/auth/internal/closer" | ||
"github.com/Chuiko-GIT/auth/internal/config" | ||
"github.com/Chuiko-GIT/auth/internal/config/env" | ||
"github.com/Chuiko-GIT/auth/internal/repository" | ||
uRepo "github.com/Chuiko-GIT/auth/internal/repository/users" | ||
"github.com/Chuiko-GIT/auth/internal/service" | ||
uServise "github.com/Chuiko-GIT/auth/internal/service/users" | ||
) | ||
|
||
type ServiceProvider struct { | ||
pgConfig config.PGConfig | ||
grpcConfig config.GRPCConfig | ||
|
||
pgPool *pgxpool.Pool | ||
usersRepository repository.Users | ||
|
||
usersService service.Users | ||
|
||
usersImpl *users.Implementation | ||
} | ||
|
||
func newServiceProvider() *ServiceProvider { | ||
return &ServiceProvider{} | ||
} | ||
|
||
func (s *ServiceProvider) PGConfig() config.PGConfig { | ||
if s.pgConfig == nil { | ||
cfg, err := env.NewPGConfig() | ||
if err != nil { | ||
log.Fatalf("failed to get pg config: %s", err.Error()) | ||
} | ||
|
||
s.pgConfig = cfg | ||
} | ||
|
||
return s.pgConfig | ||
} | ||
|
||
func (s *ServiceProvider) GRPCConfig() config.GRPCConfig { | ||
if s.grpcConfig == nil { | ||
cfg, err := env.NewGRPCConfig() | ||
if err != nil { | ||
log.Fatalf("failed to get grpc config: %s", err.Error()) | ||
} | ||
|
||
s.grpcConfig = cfg | ||
} | ||
return s.grpcConfig | ||
} | ||
|
||
func (s *ServiceProvider) PGPool(ctx context.Context) *pgxpool.Pool { | ||
if s.pgPool == nil { | ||
poll, err := pgxpool.Connect(ctx, s.PGConfig().DSN()) | ||
if err != nil { | ||
log.Fatalf("failed to create db client: %v", err) | ||
} | ||
|
||
if err = poll.Ping(ctx); err != nil { | ||
log.Fatalf("ping error: %s", err.Error()) | ||
} | ||
|
||
closer.Add(func() error { | ||
poll.Close() | ||
return nil | ||
}) | ||
|
||
s.pgPool = poll | ||
} | ||
|
||
return s.pgPool | ||
} | ||
|
||
func (s *ServiceProvider) UsersRepository(ctx context.Context) repository.Users { | ||
if s.usersRepository == nil { | ||
s.usersRepository = uRepo.NewRepository(s.PGPool(ctx)) | ||
} | ||
|
||
return s.usersRepository | ||
} | ||
|
||
func (s *ServiceProvider) UsersService(ctx context.Context) service.Users { | ||
if s.usersService == nil { | ||
s.usersService = uServise.NewService(s.UsersRepository(ctx)) | ||
} | ||
|
||
return s.usersService | ||
} | ||
|
||
func (s *ServiceProvider) UsersImpl(ctx context.Context) *users.Implementation { | ||
if s.usersImpl == nil { | ||
s.usersImpl = uImpl.NewImplementation(s.UsersService(ctx)) | ||
} | ||
|
||
return s.usersImpl | ||
} |
Oops, something went wrong.