Adding setup for postgres docker compose + connection from app.

master
Tomasz Polgrabia 2024-07-29 23:14:42 +02:00
parent 4282cc4b12
commit fad73f26de
3 changed files with 47 additions and 0 deletions

View File

@ -0,0 +1,11 @@
services:
db:
image: postgres:11
ports:
- '5432:5432'
volumes:
- '/opt/db_data:/var/lib/postgresql/data'
environment:
POSTGRES_PASSWORD: pass
POSTGRES_DB: repo
POSTGRES_USER: db_user

View File

@ -1,3 +1,17 @@
module golang_demo1 module golang_demo1
go 1.22 go 1.22
require (
github.com/go-pg/pg/v10 v10.13.0 // indirect
github.com/go-pg/zerochecker v0.2.0 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc // indirect
github.com/vmihailenco/bufpool v0.1.11 // indirect
github.com/vmihailenco/msgpack/v5 v5.3.4 // indirect
github.com/vmihailenco/tagparser v0.1.2 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
golang.org/x/crypto v0.21.0 // indirect
golang.org/x/sys v0.18.0 // indirect
mellium.im/sasl v0.3.1 // indirect
)

View File

@ -1,13 +1,35 @@
package main package main
import ( import (
"context"
"flag" "flag"
"fmt" "fmt"
"github.com/go-pg/pg/v10"
"golang_demo1/handlers" "golang_demo1/handlers"
"net/http" "net/http"
) )
func main() { 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") portPtr := flag.Int("port", 8080, "port for webserver")
flag.Parse() flag.Parse()
port := *portPtr port := *portPtr