diff --git a/config/INSTRUCTIONS.md b/config/INSTRUCTIONS.md new file mode 100644 index 00000000..08359474 --- /dev/null +++ b/config/INSTRUCTIONS.md @@ -0,0 +1,12 @@ +# Example for dbconfig.yaml + +```yaml +user: "user" +password: "password" +dbname: "test" +sslmode: "disable" +hostname: "localhost" +``` +We use Postgres v. 16 + +You need to create own yaml file \ No newline at end of file diff --git a/database/database.go b/database/database.go new file mode 100644 index 00000000..425c5ea3 --- /dev/null +++ b/database/database.go @@ -0,0 +1,85 @@ +package database + +import ( + "fmt" + "log" + "os" + + "github.com/jmoiron/sqlx" + _ "github.com/lib/pq" + yaml "gopkg.in/yaml.v2" +) + +type Conf struct { + User string `yaml:"user"` + DBname string `yaml:"dbname"` + SSLMode string `yaml:"sslmode"` + Password string `yaml:"password"` + Host string `yaml:"hostname"` +} + +func LoadConfig(path string) (*Conf, error) { + data, err := os.ReadFile(path) + if err != nil { + return nil, err + } + var config Conf + err = yaml.Unmarshal(data, &config) + if err != nil { + return nil, err + } + return &config, nil +} + +func CheckError(err error) bool { + if err != nil { + log.Fatalln(err) + return false + } + return true +} + +func Test_connection() { + config, err := LoadConfig("config/config.yaml") + if err != nil { + log.Fatalln("Failed to load config:", err) + } + dsn := fmt.Sprintf("user=%s dbname=%s sslmode=%s password=%s host=%s", + config.User, config.DBname, config.SSLMode, config.Password, config.Host) + + db, err := sqlx.Connect("postgres", dsn) + if err != nil { + log.Fatalln(err) + } + + defer db.Close() + + // Test the connection to the database + if err := db.Ping(); err != nil { + log.Fatal(err) + } else { + log.Println("Successfully Connected") + } +} + +func Insert_Data(query string) bool { + config, err := LoadConfig("config/config.yaml") + if err != nil { + log.Fatalln("Failed to load config:", err) + return false + } + + dsn := fmt.Sprintf("user=%s dbname=%s sslmode=%s password=%s host=%s", + config.User, config.DBname, config.SSLMode, config.Password, config.Host) + + db, err := sqlx.Open("postgres", dsn) + if !CheckError(err) { + return false + } + + defer db.Close() + + _, e := db.Exec(query) + + return CheckError(e) +} diff --git a/go.mod b/go.mod index 9a36617e..5288bc06 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,9 @@ module market go 1.18 + +require ( + github.com/jmoiron/sqlx v1.4.0 + github.com/lib/pq v1.10.9 + gopkg.in/yaml.v2 v2.4.0 +) \ No newline at end of file diff --git a/go.sum b/go.sum index 36c43246..951f03ae 100644 --- a/go.sum +++ b/go.sum @@ -6,5 +6,13 @@ github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI= github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY= github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ= +github.com/jmoiron/sqlx v1.4.0 h1:1PLqN7S1UYp5t4SrVVnt4nUVNemrDAtxlulVe+Qgm3o= +github.com/jmoiron/sqlx v1.4.0/go.mod h1:ZrZ7UsYB/weZdl2Bxg6jCRO9c3YHl8r3ahlKmRT4JLY= +github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw= +github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= +github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y= github.com/mattn/go-sqlite3 v1.14.24 h1:tpSp2G2KyMnnQu99ngJ47EIkWVmliIizyZBfPrBWDRM= github.com/mattn/go-sqlite3 v1.14.24/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=