db connect
This commit is contained in:
parent
a6be7a9e21
commit
1dce375512
6 changed files with 81 additions and 5 deletions
|
|
@ -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=
|
||||
|
|
|
|||
|
|
@ -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"),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
|
|
|||
1
go.mod
1
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
|
||||
|
|
|
|||
2
go.sum
2
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=
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
33
pkg/dbase/handler.go
Normal file
33
pkg/dbase/handler.go
Normal file
|
|
@ -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
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue