Skip to content

Commit

Permalink
Merge pull request #6 from Chuiko-GIT/di-container
Browse files Browse the repository at this point in the history
Implemented Dependency Injection container for better modularity
  • Loading branch information
Chuiko-GIT authored Aug 25, 2024
2 parents 02dc7e1 + d909242 commit 1082361
Show file tree
Hide file tree
Showing 6 changed files with 286 additions and 119 deletions.
63 changes: 0 additions & 63 deletions cmd/grpc_client/main.go

This file was deleted.

65 changes: 12 additions & 53 deletions cmd/grpc_server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,69 +2,28 @@ package main

import (
"context"
"flag"
"log"
"net"

"github.com/jackc/pgx/v4/pgxpool"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"

usersAPI "github.com/Chuiko-GIT/auth/internal/api/users"
"github.com/Chuiko-GIT/auth/internal/config"
"github.com/Chuiko-GIT/auth/internal/config/env"
"github.com/Chuiko-GIT/auth/internal/repository/users"
srv "github.com/Chuiko-GIT/auth/internal/service/users"
"github.com/Chuiko-GIT/auth/pkg/user_api"
"github.com/Chuiko-GIT/auth/internal/app"
)

var (
configPath string
)

func init() {
flag.StringVar(&configPath, "config-path", ".env", "path to config file")
}
// var (
// configPath string
// )
//
// func init() {
// flag.StringVar(&configPath, "config-path", ".env", "path to config file")
// }

func main() {
flag.Parse()
ctx := context.Background()

if err := config.Load("local.env"); err != nil {
log.Fatalf("failed to load config: %v", err)
}

pgConfig, err := env.NewPGConfig()
if err != nil {
log.Fatalf("failed to get pg config: %v", err)
}

pool, err := pgxpool.Connect(ctx, pgConfig.DSN())
a, err := app.NewApp(ctx)
if err != nil {
log.Fatalf("failed to connect to database: %v", err)
log.Fatalf("failed to init app: %v", err.Error())
}
defer pool.Close()

repoUsers := users.NewRepository(pool)
serviceUser := srv.NewService(repoUsers)

grpcConfig, err := env.NewGRPCConfig()
if err != nil {
log.Fatalf("failed to get grpc config: %v", err)
}

lis, err := net.Listen("tcp", grpcConfig.Address())
if err != nil {
log.Fatalf("failed to listen: %v", err)
}

s := grpc.NewServer()
reflection.Register(s)
user_api.RegisterUserAPIServer(s, usersAPI.NewImplementation(serviceUser))

log.Printf("server listening at %v", lis.Addr())

if err = s.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err)
if err = a.Run(); err != nil {
log.Fatalf("failed to run app: %v", err.Error())
}
}
3 changes: 0 additions & 3 deletions internal/api/users/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ import (
"github.com/Chuiko-GIT/auth/pkg/user_api"
)

// В курсе решили назвать название этого файла тоже service.go
// В курсе так же не придумали название данному слою, поэтому Implementation

type Implementation struct {
user_api.UnimplementedUserAPIServer
userService service.Users
Expand Down
91 changes: 91 additions & 0 deletions internal/app/app.go
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
}
105 changes: 105 additions & 0 deletions internal/app/service_provider.go
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
}
Loading

0 comments on commit 1082361

Please sign in to comment.