From 5cf59806cb3ee08b6f9c1648bb4bfb3e6f36cee8 Mon Sep 17 00:00:00 2001 From: optclblast Date: Sat, 1 Jun 2024 19:26:00 +0300 Subject: [PATCH] moved app init into internal/app directory --- cmd/app/main.go | 61 ++----------------------------------- internal/app/app.go | 73 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 59 deletions(-) create mode 100644 internal/app/app.go diff --git a/cmd/app/main.go b/cmd/app/main.go index 6181c96..95c7da9 100644 --- a/cmd/app/main.go +++ b/cmd/app/main.go @@ -2,75 +2,18 @@ package main import ( "context" - "log/slog" "os" "os/signal" "syscall" - "github.com/optclblast/blk/internal/controller/http" - "github.com/optclblast/blk/internal/infrastructure/getblock" - "github.com/optclblast/blk/internal/logger" - "github.com/optclblast/blk/internal/server" - "github.com/optclblast/blk/internal/usecase" -) - -const ( - // API access token - getblockAccessTokenEnv = "BLK_GETBLOCK_ACCESS_TOKEN" - // Log level - logLevelEnv = "BLK_LOG_LEVEL" - // Listen address - httpAddrEnv = "BLK_HTTP_ADDR" + "github.com/optclblast/blk/internal/app" ) func main() { ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM) defer stop() - getblockAccessToken := os.Getenv(getblockAccessTokenEnv) - logLevel := os.Getenv(logLevelEnv) - httpAddr := os.Getenv(httpAddrEnv) - - log := logger.NewBuilder(). - WithLevel(logger.MapLevel(logLevel)). - Build() - - log.Info( - "starting blk server 0w0", - slog.String("address", httpAddr), - slog.String("log level", logLevel), - ) - - getblockClient := getblock.NewClient( - log.WithGroup("getblock-client"), - getblockAccessToken, - ) - - ethInteractor := usecase.NewEthInteractor( - log.WithGroup("eth-interactor"), - getblockClient, - ) - - walletsController := http.NewWalletsController( - log.WithGroup("wallets-controller"), - ethInteractor, - ) - - router := http.NewRouter( - log.WithGroup("router"), - walletsController, - ) - - server := server.New(router, httpAddr) - - select { - case <-ctx.Done(): - log.Info("shutting down blk server. bye bye! =w=") - case err := <-server.Notify(): - log.Error("error listen to net ;_;", logger.Err(err)) - } - - if err := server.Shutdown(); err != nil { + if err := app.Init(ctx); err != nil { panic(err) } } diff --git a/internal/app/app.go b/internal/app/app.go new file mode 100644 index 0000000..ac1a930 --- /dev/null +++ b/internal/app/app.go @@ -0,0 +1,73 @@ +package app + +import ( + "context" + "log/slog" + "os" + + "github.com/optclblast/blk/internal/controller/http" + "github.com/optclblast/blk/internal/infrastructure/getblock" + "github.com/optclblast/blk/internal/logger" + "github.com/optclblast/blk/internal/server" + "github.com/optclblast/blk/internal/usecase" +) + +const ( + // API access token + getblockAccessTokenEnv = "BLK_GETBLOCK_ACCESS_TOKEN" + // Log level + logLevelEnv = "BLK_LOG_LEVEL" + // Listen address + httpAddrEnv = "BLK_HTTP_ADDR" +) + +func Init(ctx context.Context) error { + getblockAccessToken := os.Getenv(getblockAccessTokenEnv) + logLevel := os.Getenv(logLevelEnv) + httpAddr := os.Getenv(httpAddrEnv) + + log := logger.NewBuilder(). + WithLevel(logger.MapLevel(logLevel)). + Build() + + log.Info( + "starting blk server 0w0", + slog.String("address", httpAddr), + slog.String("log level", logLevel), + ) + + getblockClient := getblock.NewClient( + log.WithGroup("getblock-client"), + getblockAccessToken, + ) + + ethInteractor := usecase.NewEthInteractor( + log.WithGroup("eth-interactor"), + getblockClient, + ) + + walletsController := http.NewWalletsController( + log.WithGroup("wallets-controller"), + ethInteractor, + ) + + router := http.NewRouter( + log.WithGroup("router"), + walletsController, + ) + + server := server.New(router, httpAddr) + + select { + case <-ctx.Done(): + log.Info("shutting down blk server. bye bye! =w=") + case err := <-server.Notify(): + log.Error("error listen to net ;_;", logger.Err(err)) + } + + if err := server.Shutdown(); err != nil { + return err + } + + return nil +}