diff options
author | Matthew Lemon <y@yulqen.org> | 2024-04-10 15:12:12 +0100 |
---|---|---|
committer | Matthew Lemon <y@yulqen.org> | 2024-04-10 15:12:12 +0100 |
commit | 50d6737b0c81869d77486fded1476f55917b940b (patch) | |
tree | 875151701dd1cf758130f28fb186cc4af86fcf75 | |
parent | b408be58debc72322fa1680a8f7293b9ba3b571f (diff) |
Adds .env file with database dsn in it
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | cmd/dbasik-api/main.go | 9 | ||||
-rw-r--r-- | go.mod | 5 | ||||
-rw-r--r-- | go.sum | 2 |
4 files changed, 15 insertions, 2 deletions
@@ -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 @@ -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 +) @@ -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= |