diff --git a/config.env b/config.env index 2c27b2d..9925670 100644 --- a/config.env +++ b/config.env @@ -12,3 +12,10 @@ GIN_MODE= TASK_API_HOST= TASK_API_PORT= TASK_SOURCE_TIMEOUT_SECONDS= + +#[DBase] +DB_HOST= +DB_PORT= +DB_USERNAME= +DB_PASSWORD= +DB_NAME= diff --git a/config/config.go b/config/config.go index 12e5fd1..1aa2d68 100644 --- a/config/config.go +++ b/config/config.go @@ -6,6 +6,7 @@ type Config struct { App App Http Http TasksSource TasksSource + DBase DBase } type App struct { @@ -26,6 +27,14 @@ type TasksSource struct { Timeout time.Duration } +type DBase struct { + Host string + Port string + Username string + Password string + DBName string +} + func NewConfig() Config { return Config{ App: App{ @@ -45,5 +54,13 @@ func NewConfig() Config { Port: getEnv("TASK_API_PORT", "61000"), Timeout: getEnvSeconds("TASK_SOURCE_TIMEOUT_SECONDS", 60), }, + + DBase: DBase{ + Host: getEnv("DB_HOST", "10.0.0.4"), + Port: getEnv("DB_PORT", "54326"), + Username: getEnv("DB_USERNAME", "merch_app_user"), + Password: getEnv("DB_PASSWORD", "7kek8wait9"), + DBName: getEnv("DB_NAME", "merch_dev"), + }, } } diff --git a/go.mod b/go.mod index 6370395..de0dd9c 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.25.0 require ( github.com/gin-gonic/gin v1.11.0 + github.com/lib/pq v1.11.2 github.com/sirupsen/logrus v1.9.4 github.com/swaggo/files v1.0.1 github.com/swaggo/gin-swagger v1.6.1 diff --git a/go.sum b/go.sum index e5cd302..72c9185 100644 --- a/go.sum +++ b/go.sum @@ -60,6 +60,8 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ= github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI= +github.com/lib/pq v1.11.2 h1:x6gxUeu39V0BHZiugWe8LXZYZ+Utk7hSJGThs8sdzfs= +github.com/lib/pq v1.11.2/go.mod h1:/p+8NSbOcwzAEI7wiMXFlgydTwcgTr3OSKMsD2BitpA= github.com/mailru/easyjson v0.0.0-20190614124828-94de47d64c63/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc= github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc= github.com/mailru/easyjson v0.7.6 h1:8yTIVnZgCoiM1TgqoeTl+LfU5Jg6/xL3QhGQnimLYnA= diff --git a/internal/app/handler.go b/internal/app/handler.go index fe65524..ecee711 100644 --- a/internal/app/handler.go +++ b/internal/app/handler.go @@ -6,10 +6,13 @@ import ( log "github.com/sirupsen/logrus" "merch-api/config" "merch-api/internal/merch" + "merch-api/pkg/dbase" "merch-api/pkg/router" "time" ) +const pkgLogHeader string = "Application |" + type App struct { cfg config.Config router *router.Router @@ -25,10 +28,22 @@ func New(cfg config.Config) *App { GinMode: cfg.Http.GinMode, }) + db, err := dbase.Connect(dbase.Deps{ + Host: cfg.DBase.Host, + Port: cfg.DBase.Port, + Username: cfg.DBase.Username, + Password: cfg.DBase.Password, + DBName: cfg.DBase.DBName, + }) + + if err != nil { + log.WithError(err).Fatalf("%v failed to connect database", pkgLogHeader) + } + //modules var modules []Module - m := merch.New(nil) + m := merch.New(db) modules = append(modules, m) return &App{ @@ -39,7 +54,7 @@ func New(cfg config.Config) *App { } func (app *App) Run(ctx context.Context) error { - log.Info("Starting application...") + log.Infof("%v starting...", pkgLogHeader) baseGroup := app.router.BaseGroup() app.collectRoutes(baseGroup) @@ -63,19 +78,20 @@ func (app *App) Run(ctx context.Context) error { } func (app *App) shutdown(ctx context.Context) { - log.Info("Shutting down application...") + log.Infof("%v shutting down...", pkgLogHeader) shutdownCtx, shutdownCancel := context.WithTimeout(ctx, 15*time.Second) defer shutdownCancel() if err := app.router.Shutdown(shutdownCtx); err != nil { - log.Warnf("Error shutting down application: %v", err) + log.WithError(err).Warnf("%v error shutting down application", pkgLogHeader) } - log.Info("Application shutdown complete") + log.Infof("%v shutdown complete", pkgLogHeader) } func (app *App) collectRoutes(group *gin.RouterGroup) { for _, m := range app.modules { m.RegisterRoutes(group) } + log.Infof("%v routes registered", pkgLogHeader) } diff --git a/pkg/dbase/handler.go b/pkg/dbase/handler.go new file mode 100644 index 0000000..8f65482 --- /dev/null +++ b/pkg/dbase/handler.go @@ -0,0 +1,33 @@ +package dbase + +import ( + "database/sql" + "fmt" + _ "github.com/lib/pq" +) + +type Deps struct { + Host string + Port string + Username string + Password string + DBName string +} + +func Connect(deps Deps) (*sql.DB, error) { + dsn := fmt.Sprintf( + "host=%s port=%s user=%s password=%s dbname=%s sslmode=disable", + deps.Host, deps.Port, deps.Username, deps.Password, deps.DBName) + + db, err := sql.Open("postgres", dsn) + if err != nil { + return nil, err + } + + if err = db.Ping(); err != nil { + db.Close() + return nil, fmt.Errorf("failed to connect to DB: %w", err) + } + + return db, nil +}