36 lines
636 B
Go
36 lines
636 B
Go
package svc
|
|
|
|
import (
|
|
"database/sql"
|
|
"log"
|
|
|
|
"src/internal/config"
|
|
|
|
_ "github.com/lib/pq" // PostgreSQL driver
|
|
)
|
|
|
|
type ServiceContext struct {
|
|
Config config.Config
|
|
DB *sql.DB
|
|
}
|
|
|
|
func NewServiceContext(c config.Config) *ServiceContext {
|
|
// Initialize database connection
|
|
db, err := sql.Open("postgres", c.Database.DSN)
|
|
if err != nil {
|
|
log.Fatalf("Failed to connect to database: %v", err)
|
|
}
|
|
|
|
// Test the connection
|
|
if err := db.Ping(); err != nil {
|
|
log.Fatalf("Failed to ping database: %v", err)
|
|
}
|
|
|
|
log.Println("Database connection established successfully")
|
|
|
|
return &ServiceContext{
|
|
Config: c,
|
|
DB: db,
|
|
}
|
|
}
|