Adding setup for postgres docker compose + connection from app.
This commit is contained in:
parent
4282cc4b12
commit
fad73f26de
3 changed files with 47 additions and 0 deletions
|
@ -1,13 +1,35 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"flag"
|
||||
"fmt"
|
||||
"github.com/go-pg/pg/v10"
|
||||
"golang_demo1/handlers"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var opt, errDb = pg.ParseURL("postgres://db_user:pass@localhost:5432/repo?sslmode=disable")
|
||||
if errDb != nil {
|
||||
panic("Failed to parse url: " + errDb.Error())
|
||||
}
|
||||
var db = pg.Connect(opt)
|
||||
|
||||
if db == nil {
|
||||
panic("failed to connect to the database")
|
||||
}
|
||||
|
||||
defer func(db *pg.DB) {
|
||||
_ = db.Close()
|
||||
}(db)
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
if err := db.Ping(ctx); err != nil {
|
||||
panic("Failed to ping: " + err.Error())
|
||||
}
|
||||
|
||||
portPtr := flag.Int("port", 8080, "port for webserver")
|
||||
flag.Parse()
|
||||
port := *portPtr
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue