switch to pgx driver + create merch
This commit is contained in:
parent
546fe13107
commit
97f8d27430
13 changed files with 289 additions and 69 deletions
|
|
@ -2,14 +2,17 @@ package app
|
|||
|
||||
import (
|
||||
"context"
|
||||
"github.com/gin-gonic/gin"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"merch-api/config"
|
||||
"merch-api/internal/merch"
|
||||
"merch-api/internal/user"
|
||||
"merch-api/pkg/dbase"
|
||||
"merch-api/pkg/router"
|
||||
"merch-api/pkg/utils"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
const pkgLogHeader string = "Application |"
|
||||
|
|
@ -18,9 +21,10 @@ type App struct {
|
|||
cfg config.Config
|
||||
router *router.Router
|
||||
modules []Module
|
||||
dbPool *pgxpool.Pool
|
||||
}
|
||||
|
||||
func New(cfg config.Config) *App {
|
||||
func New(ctx context.Context, cfg config.Config) *App {
|
||||
//providers
|
||||
r := router.NewRouter(router.Deps{
|
||||
Host: cfg.Http.Host,
|
||||
|
|
@ -29,7 +33,7 @@ func New(cfg config.Config) *App {
|
|||
GinMode: cfg.Http.GinMode,
|
||||
})
|
||||
|
||||
db, err := dbase.Connect(dbase.Deps{
|
||||
dbPool, err := dbase.ConnectPool(ctx, dbase.Deps{
|
||||
Host: cfg.DBase.Host,
|
||||
Port: cfg.DBase.Port,
|
||||
Username: cfg.DBase.Username,
|
||||
|
|
@ -39,6 +43,11 @@ func New(cfg config.Config) *App {
|
|||
|
||||
u := utils.New()
|
||||
|
||||
userProv := user.New(user.Deps{
|
||||
DB: dbPool,
|
||||
Utils: u,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
log.WithError(err).Fatalf("%v failed to connect database", pkgLogHeader)
|
||||
}
|
||||
|
|
@ -47,8 +56,9 @@ func New(cfg config.Config) *App {
|
|||
var modules []Module
|
||||
|
||||
m := merch.New(merch.Deps{
|
||||
DB: db,
|
||||
Utils: u,
|
||||
DB: dbPool,
|
||||
Utils: u,
|
||||
UserProvider: userProv,
|
||||
})
|
||||
modules = append(modules, m)
|
||||
|
||||
|
|
@ -56,6 +66,7 @@ func New(cfg config.Config) *App {
|
|||
cfg: cfg,
|
||||
router: r,
|
||||
modules: modules,
|
||||
dbPool: dbPool,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -88,6 +99,8 @@ func (app *App) shutdown(ctx context.Context) {
|
|||
shutdownCtx, shutdownCancel := context.WithTimeout(ctx, 15*time.Second)
|
||||
defer shutdownCancel()
|
||||
|
||||
app.dbPool.Close()
|
||||
|
||||
if err := app.router.Shutdown(shutdownCtx); err != nil {
|
||||
log.WithError(err).Warnf("%v error shutting down application", pkgLogHeader)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue