feat: get postgresql URL from env
This commit is contained in:
parent
8e03a1e087
commit
c7be765c23
2 changed files with 10 additions and 1 deletions
2
.envrc
2
.envrc
|
@ -1 +1,3 @@
|
||||||
use flake
|
use flake
|
||||||
|
|
||||||
|
export DATABASE_URL="postgres://short:short@localhost:5432/short"
|
||||||
|
|
9
main.go
9
main.go
|
@ -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)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue