aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthew Lemon <y@yulqen.org>2024-04-10 15:12:12 +0100
committerMatthew Lemon <y@yulqen.org>2024-04-10 15:12:12 +0100
commit50d6737b0c81869d77486fded1476f55917b940b (patch)
tree875151701dd1cf758130f28fb186cc4af86fcf75
parentb408be58debc72322fa1680a8f7293b9ba3b571f (diff)
Adds .env file with database dsn in it
-rw-r--r--.gitignore1
-rw-r--r--cmd/dbasik-api/main.go9
-rw-r--r--go.mod5
-rw-r--r--go.sum2
4 files changed, 15 insertions, 2 deletions
diff --git a/.gitignore b/.gitignore
index e660fd9..9f96217 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1 +1,2 @@
bin/
+.env
diff --git a/cmd/dbasik-api/main.go b/cmd/dbasik-api/main.go
index e1f93d5..e296551 100644
--- a/cmd/dbasik-api/main.go
+++ b/cmd/dbasik-api/main.go
@@ -25,11 +25,13 @@ import (
"database/sql"
"flag"
"fmt"
+ "log"
"log/slog"
"net/http"
"os"
"time"
+ "github.com/joho/godotenv"
_ "github.com/lib/pq"
)
@@ -56,10 +58,15 @@ func main() {
// Instance of config
var cfg config
+ err := godotenv.Load()
+ if err != nil {
+ log.Fatal("Cannot load .env file - is it present?")
+ }
+
// 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.StringVar(&cfg.db.dsn, "db-dsn", os.Getenv("DBASIK_DB_DSN"), "PostgreSQL DSN")
flag.Parse()
// Initialize a new structured logger which writes to stdout
diff --git a/go.mod b/go.mod
index 6b79d60..0070a82 100644
--- a/go.mod
+++ b/go.mod
@@ -2,4 +2,7 @@ module git.yulqen.org/go/dbasik-go
go 1.22.1
-require github.com/lib/pq v1.10.9 // indirect
+require (
+ github.com/joho/godotenv v1.5.1 // indirect
+ github.com/lib/pq v1.10.9 // indirect
+)
diff --git a/go.sum b/go.sum
index aeddeae..ecb9035 100644
--- a/go.sum
+++ b/go.sum
@@ -1,2 +1,4 @@
+github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
+github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=