feat: get postgresql URL from env

This commit is contained in:
Bastien Riviere 2024-01-29 19:53:15 +01:00
parent 8e03a1e087
commit c7be765c23
Signed by: babariviere
GPG key ID: 4E5F0839249F162E
2 changed files with 10 additions and 1 deletions

2
.envrc
View file

@ -1 +1,3 @@
use flake use flake
export DATABASE_URL="postgres://short:short@localhost:5432/short"

View file

@ -4,6 +4,7 @@ import (
"context" "context"
"log" "log"
"net/http" "net/http"
"os"
"github.com/babariviere/short/internal/api" "github.com/babariviere/short/internal/api"
"github.com/babariviere/short/internal/db" "github.com/babariviere/short/internal/db"
@ -12,7 +13,13 @@ import (
) )
func main() { 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 { if err != nil {
log.Fatalf("cannot connect to database: %v", err) log.Fatalf("cannot connect to database: %v", err)
} }