0.2.1 databatage conectage
This commit is contained in:
11
config/conf.json
Normal file
11
config/conf.json
Normal file
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"database": {
|
||||
"host": "localhost",
|
||||
"port": "5432",
|
||||
"user": "postgres",
|
||||
"password": "postgres",
|
||||
"dbname": "ti1",
|
||||
"sslmode": "disable"
|
||||
},
|
||||
"temp": "value"
|
||||
}
|
||||
35
config/config.go
Normal file
35
config/config.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Database struct {
|
||||
Host string `json:"host"`
|
||||
Port string `json:"port"`
|
||||
User string `json:"user"`
|
||||
Password string `json:"password"`
|
||||
DBName string `json:"dbname"`
|
||||
SSLMode string `json:"sslmode"`
|
||||
} `json:"database"`
|
||||
Temp string `json:"temp"`
|
||||
}
|
||||
|
||||
func LoadConfig(file string) (Config, error) {
|
||||
var config Config
|
||||
configFile, err := os.Open(file)
|
||||
if err != nil {
|
||||
return config, fmt.Errorf("failed to open config file: %w", err)
|
||||
}
|
||||
defer configFile.Close()
|
||||
|
||||
jsonParser := json.NewDecoder(configFile)
|
||||
if err := jsonParser.Decode(&config); err != nil {
|
||||
return config, fmt.Errorf("failed to parse config file: %w", err)
|
||||
}
|
||||
|
||||
return config, nil
|
||||
}
|
||||
57
config/db.go
Normal file
57
config/db.go
Normal file
@@ -0,0 +1,57 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
_ "github.com/lib/pq"
|
||||
)
|
||||
|
||||
func ConnectToPostgreSQL() (*sql.DB, error) {
|
||||
fmt.Println("Connecting to PostgreSQL...")
|
||||
config, err := LoadConfig("config/conf.json")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
fmt.Println("Configuration loaded successfully!")
|
||||
|
||||
connStr := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=%s",
|
||||
config.Database.Host, config.Database.Port, config.Database.User, config.Database.Password, config.Database.DBName, config.Database.SSLMode)
|
||||
|
||||
// Open connection to database
|
||||
db, err := sql.Open("postgres", connStr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
fmt.Println("Connection to PostgreSQL opened successfully!")
|
||||
|
||||
// Ping database to verify connection
|
||||
err = db.Ping()
|
||||
|
||||
fmt.Println(err)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
log.Println("Connected to PostgreSQL!")
|
||||
|
||||
return db, nil
|
||||
}
|
||||
|
||||
func PrintDBConfig() {
|
||||
config, err := LoadConfig("config/conf.json")
|
||||
if err != nil {
|
||||
fmt.Println("Error loading config:", err)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println("Configuration:", config)
|
||||
fmt.Println("Host:", config.Database.Host)
|
||||
fmt.Println("Port:", config.Database.Port)
|
||||
fmt.Println("User:", config.Database.User)
|
||||
fmt.Println("Database Host:", config.Database.Host)
|
||||
fmt.Println("Database Password:", config.Database.Password)
|
||||
}
|
||||
Reference in New Issue
Block a user