From c7be765c236c66db34242c3cf0a13b40aa4bf461 Mon Sep 17 00:00:00 2001 From: Bastien Riviere Date: Mon, 29 Jan 2024 19:53:15 +0100 Subject: [PATCH] feat: get postgresql URL from env --- .envrc | 2 ++ main.go | 9 ++++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/.envrc b/.envrc index 3550a30..5aaddf8 100644 --- a/.envrc +++ b/.envrc @@ -1 +1,3 @@ use flake + +export DATABASE_URL="postgres://short:short@localhost:5432/short" diff --git a/main.go b/main.go index 0a6fbbb..50731e9 100644 --- a/main.go +++ b/main.go @@ -4,6 +4,7 @@ import ( "context" "log" "net/http" + "os" "github.com/babariviere/short/internal/api" "github.com/babariviere/short/internal/db" @@ -12,7 +13,13 @@ import ( ) func main() { - psql, err := pgx.Connect(context.Background(), "postgres://short:short@localhost:5432/short") + // This is rude but at least we can configure the database URL securely. + psqlUrl := os.Getenv("DATABASE_URL") + if psqlUrl == "" { + log.Fatalln("DATABASE_URL is not configured.") + } + + psql, err := pgx.Connect(context.Background(), psqlUrl) if err != nil { log.Fatalf("cannot connect to database: %v", err) }