119 lines
2.3 KiB
Go
119 lines
2.3 KiB
Go
|
package dbConfig
|
||
|
|
||
|
import (
|
||
|
"database/sql"
|
||
|
"encoding/json"
|
||
|
"fmt"
|
||
|
"net/http"
|
||
|
"os"
|
||
|
)
|
||
|
|
||
|
var db *sql.DB
|
||
|
|
||
|
const (
|
||
|
dbhost = "DBHOST"
|
||
|
dbport = "5432"
|
||
|
dbuser = "postgres"
|
||
|
dbpass = "admin"
|
||
|
dbname = "mydb"
|
||
|
)
|
||
|
|
||
|
func initDb() {
|
||
|
//config := dbConfig()
|
||
|
var err error
|
||
|
//psqlInfo := fmt.Sprintf("host=localhost port=%s user=%s password=%s dbname=%s sslmode=disable",
|
||
|
// config[dbport], config[dbuser], config[dbpass], config[dbname])
|
||
|
|
||
|
db, err = sql.Open("postgres", "port=5432 password=admin user=postgres dbname=mydb sslmode=disable")
|
||
|
if err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
err = db.Ping()
|
||
|
if err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
fmt.Println("Successfullu connected!")
|
||
|
}
|
||
|
|
||
|
func dbConfig() map[string]string {
|
||
|
conf := make(map[string]string)
|
||
|
host, ok := os.LookupEnv(dbhost)
|
||
|
if !ok {
|
||
|
panic("DBHOST environment variable required but not set")
|
||
|
}
|
||
|
port, ok := os.LookupEnv(dbport)
|
||
|
if !ok {
|
||
|
panic("DBPORT environment variable required but not set")
|
||
|
}
|
||
|
user, ok := os.LookupEnv(dbuser)
|
||
|
if !ok {
|
||
|
panic("DBUSER environment variable required but not set")
|
||
|
}
|
||
|
password, ok := os.LookupEnv(dbpass)
|
||
|
if !ok {
|
||
|
panic("DBPASS environment variable required but not set")
|
||
|
}
|
||
|
name, ok := os.LookupEnv(dbname)
|
||
|
if !ok {
|
||
|
panic("DBNAME environment variable required but not set")
|
||
|
}
|
||
|
conf[dbhost] = host
|
||
|
conf[dbport] = port
|
||
|
conf[dbuser] = user
|
||
|
conf[dbpass] = password
|
||
|
conf[dbname] = name
|
||
|
return conf
|
||
|
}
|
||
|
|
||
|
func testFunc(w http.ResponseWriter, r *http.Request) {
|
||
|
fmt.Println("weszlo")
|
||
|
repos := repositories{}
|
||
|
|
||
|
err := queryRepos(&repos)
|
||
|
if err != nil {
|
||
|
http.Error(w, err.Error(), 500)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
out, err := json.Marshal(repos)
|
||
|
if err != nil {
|
||
|
http.Error(w, err.Error(), 500)
|
||
|
return
|
||
|
}
|
||
|
fmt.Fprint(w, repos)
|
||
|
fmt.Fprintf(w, string(out))
|
||
|
}
|
||
|
|
||
|
func queryRepos(repos *repositories) error {
|
||
|
rows, err := db.Query(`SELECT * FROM friends.test;`)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
defer rows.Close()
|
||
|
for rows.Next() {
|
||
|
repo := repositorySummary{}
|
||
|
err = rows.Scan(
|
||
|
&repo.firstname,
|
||
|
&repo.lastname, )
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
fmt.Println("mam", repo)
|
||
|
repos.Repositories = append(repos.Repositories, repo)
|
||
|
}
|
||
|
err = rows.Err()
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
type repositorySummary struct {
|
||
|
firstname string
|
||
|
lastname string
|
||
|
}
|
||
|
|
||
|
type repositories struct {
|
||
|
Repositories []repositorySummary
|
||
|
}
|