Skip to content

Commit

Permalink
moved app init into internal/app directory
Browse files Browse the repository at this point in the history
  • Loading branch information
optclblast committed Jun 1, 2024
1 parent b0ac010 commit 5cf5980
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 59 deletions.
61 changes: 2 additions & 59 deletions cmd/app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
73 changes: 73 additions & 0 deletions internal/app/app.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 5cf5980

Please sign in to comment.