aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthew Lemon <y@yulqen.org>2024-04-10 15:05:57 +0100
committerMatthew Lemon <y@yulqen.org>2024-04-10 15:05:57 +0100
commitb408be58debc72322fa1680a8f7293b9ba3b571f (patch)
treea20f0d5618aaa7618f024121342f94d3299b5dc6
parent1f5402530a5c5011ee4e3d0fc8c94636fbd4ed60 (diff)
Postgres implementation now set up and in containers - no tables yet
-rw-r--r--cmd/dbasik-api/main.go38
-rw-r--r--compose.yaml2
-rw-r--r--go.mod2
-rw-r--r--go.sum2
-rw-r--r--migrations/setup.sql6
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:
diff --git a/go.mod b/go.mod
index 6c7a716..6b79d60 100644
--- a/go.mod
+++ b/go.mod
@@ -1,3 +1,5 @@
module git.yulqen.org/go/dbasik-go
go 1.22.1
+
+require github.com/lib/pq v1.10.9 // indirect
diff --git a/go.sum b/go.sum
new file mode 100644
index 0000000..aeddeae
--- /dev/null
+++ b/go.sum
@@ -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;