Skip to content

Commit f709ee9

Browse files
committed
add support for reading database url from password
1 parent 732ee2f commit f709ee9

2 files changed

Lines changed: 18 additions & 2 deletions

File tree

  • 05-example-web-application

‎05-example-web-application/api-golang/main.go‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"io/ioutil"
45
"log"
56
"os"
67
"time"
@@ -11,7 +12,16 @@ import (
1112
)
1213

1314
func init() {
14-
errDB := database.InitDB(os.Getenv("DATABASE_URL"))
15+
databaseUrl := os.Getenv("DATABASE_URL")
16+
if databaseUrl == "" {
17+
content, err := ioutil.ReadFile(os.Getenv("DATABASE_URL_FILE"))
18+
if err != nil {
19+
log.Fatal(err)
20+
}
21+
databaseUrl = string(content)
22+
}
23+
24+
errDB := database.InitDB(databaseUrl)
1525
if errDB != nil {
1626
log.Fatalf("⛔ Unable to connect to database: %v\n", errDB)
1727
} else {

‎05-example-web-application/api-node/src/db.js‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1+
const fs = require('fs');
2+
13
const { Pool } = require('pg');
24

5+
databaseUrl =
6+
process.env.DATABASE_URL ||
7+
fs.readFileSync(process.env.DATABASE_URL_FILE, 'utf8');
8+
39
const pool = new Pool({
4-
connectionString: process.env.DATABASE_URL,
10+
connectionString: databaseUrl,
511
});
612

713
// the pool will emit an error on behalf of any idle clients

0 commit comments

Comments
 (0)