initiate repo
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"antrian-operasi/internal/config"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
|
||||
_ "github.com/jackc/pgx/v5/stdlib"
|
||||
)
|
||||
|
||||
func (s *service) openPostgresConnection(config config.DatabaseConfig) (*sql.DB, error) {
|
||||
// Build connection string with security parameters
|
||||
// Convert timeout durations to seconds for pgx
|
||||
connectTimeoutSec := int(config.ConnectTimeout.Seconds())
|
||||
statementTimeoutSec := int(config.StatementTimeout.Seconds())
|
||||
|
||||
connStr := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=%s connect_timeout=%d statement_timeout=%d",
|
||||
config.Host,
|
||||
config.Port,
|
||||
config.Username,
|
||||
config.Password,
|
||||
config.Database,
|
||||
config.SSLMode,
|
||||
connectTimeoutSec,
|
||||
statementTimeoutSec,
|
||||
)
|
||||
|
||||
if config.Schema != "" {
|
||||
connStr += " search_path=" + config.Schema
|
||||
}
|
||||
|
||||
// Add SSL configuration if required
|
||||
if config.RequireSSL {
|
||||
connStr += " sslcert=" + config.SSLCert + " sslkey=" + config.SSLKey + " sslrootcert=" + config.SSLRootCert
|
||||
}
|
||||
|
||||
// Open connection using standard database/sql interface
|
||||
db, err := sql.Open("pgx", connStr)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to open PostgreSQL connection: %w", err)
|
||||
}
|
||||
|
||||
return db, nil
|
||||
}
|
||||
Reference in New Issue
Block a user