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)
|
||||||
|
}
|
||||||
@@ -127,7 +127,7 @@ type Data struct {
|
|||||||
|
|
||||||
// FetchData function to retrieve data from API
|
// FetchData function to retrieve data from API
|
||||||
func FetchData() (*Data, error) {
|
func FetchData() (*Data, error) {
|
||||||
resp, err := http.Get("https://api.entur.io/realtime/v1/rest/et?maxSize=1")
|
resp, err := http.Get("https://api.entur.io/realtime/v1/rest/et?maxSize=0")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
2
go.mod
2
go.mod
@@ -1,3 +1,5 @@
|
|||||||
module ti1
|
module ti1
|
||||||
|
|
||||||
go 1.22.1
|
go 1.22.1
|
||||||
|
|
||||||
|
require github.com/lib/pq v1.10.9
|
||||||
|
|||||||
2
go.sum
Normal file
2
go.sum
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
|
||||||
|
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
|
||||||
4
main.go
4
main.go
@@ -2,11 +2,15 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
|
"ti1/config"
|
||||||
"ti1/data" // Import the data package
|
"ti1/data" // Import the data package
|
||||||
"ti1/export"
|
"ti1/export"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
config.PrintDBConfig()
|
||||||
|
config.ConnectToPostgreSQL()
|
||||||
|
|
||||||
data, err := data.FetchData() // Use the FetchData function from the data package
|
data, err := data.FetchData() // Use the FetchData function from the data package
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
|
|||||||
Reference in New Issue
Block a user