Adding setup for postgres docker compose + connection from app.

This commit is contained in:
Tomasz Półgrabia 2024-07-29 23:14:42 +02:00
parent 4282cc4b12
commit fad73f26de
3 changed files with 47 additions and 0 deletions

View file

@ -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