diff options
author | Matthew Lemon <y@yulqen.org> | 2024-04-10 15:05:57 +0100 |
---|---|---|
committer | Matthew Lemon <y@yulqen.org> | 2024-04-10 15:05:57 +0100 |
commit | b408be58debc72322fa1680a8f7293b9ba3b571f (patch) | |
tree | a20f0d5618aaa7618f024121342f94d3299b5dc6 | |
parent | 1f5402530a5c5011ee4e3d0fc8c94636fbd4ed60 (diff) |
Postgres implementation now set up and in containers - no tables yet
-rw-r--r-- | cmd/dbasik-api/main.go | 38 | ||||
-rw-r--r-- | compose.yaml | 2 | ||||
-rw-r--r-- | go.mod | 2 | ||||
-rw-r--r-- | go.sum | 2 | ||||
-rw-r--r-- | migrations/setup.sql | 6 |
5 files changed, 49 insertions, 1 deletions
diff --git a/cmd/dbasik-api/main.go b/cmd/dbasik-api/main.go index b10080c..e1f93d5 100644 --- a/cmd/dbasik-api/main.go +++ b/cmd/dbasik-api/main.go @@ -21,12 +21,16 @@ package main import ( + "context" + "database/sql" "flag" "fmt" "log/slog" "net/http" "os" "time" + + _ "github.com/lib/pq" ) const version = "0.0.1" @@ -36,6 +40,9 @@ const version = "0.0.1" type config struct { port int env string + db struct { + dsn string + } } // This application struct holds the dependencies for our HTTP handlers, helpers and @@ -52,11 +59,21 @@ func main() { // Read the flags into the config struct. Defaults are provided if none given. flag.IntVar(&cfg.port, "port", 5000, "API server port") flag.StringVar(&cfg.env, "env", "development", "Environment (development|staging|production)") + flag.StringVar(&cfg.db.dsn, "db-dsn", "postgres://dbasik:dbasik@db/dbasik?sslmode=disable", "PostgreSQL DSN") flag.Parse() // Initialize a new structured logger which writes to stdout logger := slog.New(slog.NewTextHandler(os.Stdout, nil)) + // set up the database pool + db, err := openDB(cfg) + if err != nil { + logger.Error(err.Error()) + os.Exit(1) + } + defer db.Close() + logger.Info("database connection pool established") + // An instance of application struct, containing the config struct and the logger app := &application{ config: cfg, @@ -77,7 +94,26 @@ func main() { logger.Info("starting server", "addr", srv.Addr, "env", cfg.env) - err := srv.ListenAndServe() + err = srv.ListenAndServe() logger.Error(err.Error()) os.Exit(1) } + +func openDB(cfg config) (*sql.DB, error) { + db, err := sql.Open("postgres", cfg.db.dsn) + if err != nil { + return nil, err + } + + // create a context with a 5 second timeout + // if the database hasn't connected within this time, there is a problem + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + + err = db.PingContext(ctx) + if err != nil { + return nil, err + } + + return db, nil +} diff --git a/compose.yaml b/compose.yaml index 91c06dd..4206716 100644 --- a/compose.yaml +++ b/compose.yaml @@ -1,6 +1,8 @@ services: db: image: postgres:16-alpine + ports: + - 5432:5432 environment: - POSTGRES_PASSWORD=secret volumes: @@ -1,3 +1,5 @@ module git.yulqen.org/go/dbasik-go go 1.22.1 + +require github.com/lib/pq v1.10.9 // indirect @@ -0,0 +1,2 @@ +github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw= +github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= diff --git a/migrations/setup.sql b/migrations/setup.sql new file mode 100644 index 0000000..3d95044 --- /dev/null +++ b/migrations/setup.sql @@ -0,0 +1,6 @@ +/* should be added manually at this point */ +CREATE DATABASE dbasik; +\c dbasik + +CREATE ROLE 'dbasik' WITH LOGIN PASSWORD 'dbasik'; +CREATE EXTENSION IF NOT EXISTS citext; |