66 lines
1 KiB
Go
66 lines
1 KiB
Go
package main
|
|
|
|
import (
|
|
"database/sql"
|
|
"fmt"
|
|
_ "github.com/lib/pq"
|
|
"log"
|
|
"net/http"
|
|
)
|
|
|
|
var db *sql.DB
|
|
|
|
const (
|
|
dbhost = "DBHOST"
|
|
dbport = "DBPORT"
|
|
dbuser = "DBUSER"
|
|
dbpass = "DBPASS"
|
|
dbname = "DBNAME"
|
|
)
|
|
|
|
func main() {
|
|
initDb()
|
|
defer db.Close()
|
|
|
|
http.HandleFunc("/api/users", UserHandler)
|
|
|
|
fmt.Println("Listening on :8080")
|
|
log.Fatal(http.ListenAndServe(":8080", nil))
|
|
|
|
}
|
|
|
|
|
|
func initDb() {
|
|
config := dbConfig()
|
|
var err error
|
|
psqlInfo := fmt.Sprintf("postgres://%v:%v@%v:%v/%v?sslmode=disable",
|
|
config[dbuser], config[dbpass],
|
|
config[dbhost], config[dbport], config[dbname])
|
|
|
|
fmt.Println(psqlInfo)
|
|
db, err = sql.Open("postgres", psqlInfo)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
err = db.Ping()
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
fmt.Println("Successfully connected!")
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
func dbConfig() map[string]string {
|
|
conf := make(map[string]string)
|
|
conf[dbhost] = "poc-golang-postgres-docker_db_1"
|
|
conf[dbport] = "5432"
|
|
conf[dbuser] = "root_user"
|
|
conf[dbpass] = "shahzad12"
|
|
conf[dbname] = "db_genity"
|
|
return conf
|
|
}
|