Skip to content

Commit 6f91ac5

Browse files
authored
Merge pull request #37 from gren-lang/env-var-config
Configure app with environment variables
2 parents 1adaeed + da0f9c4 commit 6f91ac5

9 files changed

Lines changed: 66 additions & 31 deletions

File tree

.envrc.sample

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,6 @@ eval "$(devbox generate direnv --print-envrc)"
1010
# Use POSTMARK_API_TEST if you don't need to send real emails.
1111
# https://postmarkapp.com/support/article/1213-best-practices-for-testing-your-emails-through-postmark
1212
export POSTMARK_SERVER_TOKEN=POSTMARK_API_TEST
13+
14+
# SQLite database path
15+
export REGISTRY_DB_PATH=./db/registry.db

.github/workflows/test.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,3 @@ jobs:
2020
timeout_minutes: 1
2121
max_attempts: 3
2222
command: devbox run test
23-
env:
24-
POSTMARK_SERVER_TOKEN: ${{ vars.POSTMARK_SERVER_TOKEN }}

.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
.envrc
22
.gren
33
db/*.db*
4-
dist/app
5-
dist/test
4+
dist/*
5+
log/*
66
node_modules

log/.gitkeep

Whitespace-only changes.

process-compose.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ version: "0.5"
44
processes:
55
server:
66
command: make server
7+
log_location: ${SERVER_LOG_PATH:-./log/server.log}

scripts/test.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,18 @@
33
# fail immediately if app doesn't build
44
make dist/app || exit 1
55

6+
TEST_DB_PATH=./db/test.db
7+
TEST_LOG_PATH=./log/test.log
8+
TEST_POSTMARK_TOKEN=POSTMARK_API_TEST
9+
10+
export REGISTRY_DB_PATH="$TEST_DB_PATH"
11+
export SERVER_LOG_PATH="$TEST_LOG_PATH"
12+
export POSTMARK_SERVER_TOKEN="$TEST_POSTMARK_TOKEN"
13+
14+
# clear leftover state from previous runs
15+
rm -f "$TEST_DB_PATH"
16+
rm -f "$TEST_LOG_PATH"
17+
618
# start app in background and run all tests
719
devbox services up -b && \
820
npx -y wait-on tcp:3000 -t 5s && \

src/Main.gren

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import Crypto
55
import Sqlite
66
import Dict
77
import FileSystem
8+
import FileSystem.Path as Path
89
import HttpClient
910
import HttpServer exposing (Request, ServerError(..), Method(..))
1011
import HttpServer.Response as Response exposing (Response)
@@ -71,6 +72,11 @@ init env =
7172
, apiToken = token
7273
}
7374
)
75+
76+
maybeDbLocation =
77+
envVars
78+
|> Dict.get "REGISTRY_DB_PATH"
79+
|> Maybe.map (\path -> Sqlite.File (Path.fromPosixString path))
7480
in
7581
Node.startProgram
7682
{ model =
@@ -82,23 +88,31 @@ init env =
8288
, postmark = postmark
8389
}
8490
, command =
85-
let
86-
initDb =
87-
Registry.Db.open fsPerm
88-
|> Task.andThen
89-
(\db ->
90-
Registry.Db.migrate db
91-
|> Task.map (\_ -> db)
92-
)
93-
|> Task.attempt DbMigrationResult
94-
in
95-
Cmd.batch
96-
[ initDb
97-
, HttpServer.createServer serverPermission serverConfig
98-
|> Task.attempt CreateServerResult
99-
, Crypto.getSecureContext
100-
|> Task.attempt SecureContextResult
101-
]
91+
when maybeDbLocation is
92+
Nothing ->
93+
"REGISTRY_DB_PATH environment variable is not set"
94+
|> print env.stderr
95+
|> Task.map (\_ -> Node.exitWithCode 1)
96+
|> Task.executeCmd
97+
98+
Just dbLocation ->
99+
let
100+
initDb =
101+
Registry.Db.open fsPerm dbLocation
102+
|> Task.andThen
103+
(\db ->
104+
Registry.Db.migrate db
105+
|> Task.map (\_ -> db)
106+
)
107+
|> Task.attempt DbMigrationResult
108+
in
109+
Cmd.batch
110+
[ initDb
111+
, HttpServer.createServer serverPermission serverConfig
112+
|> Task.attempt CreateServerResult
113+
, Crypto.getSecureContext
114+
|> Task.attempt SecureContextResult
115+
]
102116
}
103117

104118

src/Registry/Db.gren

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,13 @@ module Registry.Db exposing
55

66

77
import FileSystem
8-
import FileSystem.Path as Path
98
import Sqlite
109
import Task exposing (Task)
1110

1211

13-
open : FileSystem.Permission -> Task Sqlite.Error Sqlite.Database
14-
open fsPerm =
15-
let
16-
-- TODO: get db path from environment
17-
dbPath =
18-
"./db/registry.db"
19-
in
20-
Sqlite.open fsPerm Sqlite.defaultOptions (Sqlite.File (Path.fromPosixString dbPath))
12+
open : FileSystem.Permission -> Sqlite.Location -> Task Sqlite.Error Sqlite.Database
13+
open fsPerm location =
14+
Sqlite.open fsPerm Sqlite.defaultOptions location
2115

2216

2317
migrate : Sqlite.Database -> Task Sqlite.Error (Array Sqlite.ExecutionSummary)

src/Test/Main.gren

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
module Test.Main exposing (main)
22

3+
import Dict
34
import Expect
45
import FileSystem
6+
import FileSystem.Path as Path
57
import HttpClient
68
import Init
79
import Node
810
import Registry.Db as Db
11+
import Sqlite
912
import Task
1013
import Test
1114
import Test.E2E
@@ -17,10 +20,20 @@ main =
1720
Node.defineSimpleProgram <| \env ->
1821
Init.await HttpClient.initialize <| \httpPerm ->
1922
Init.await FileSystem.initialize <| \fsPerm ->
23+
Init.awaitTask Node.getEnvironmentVariables <| \envVars ->
24+
let
25+
openDb =
26+
when Dict.get "REGISTRY_DB_PATH" envVars is
27+
Just path ->
28+
Db.open fsPerm (Sqlite.File (Path.fromPosixString path))
29+
30+
Nothing ->
31+
Task.fail (Sqlite.GenericError "REGISTRY_DB_PATH environment variable is not set")
32+
in
2033
Effectful.run env <|
2134
Effectful.concat
2235
[ Effectful.wrap <| Test.describe "Unit tests" Test.Unit.tests
23-
, await "Init DB for E2E tests" (Db.open fsPerm) <| \db ->
36+
, await "Init DB for E2E tests" openDb <| \db ->
2437
await "Run migrations for E2E tests" (Db.migrate db) <| \_ ->
2538
Effectful.describe "E2E tests" <|
2639
Test.E2E.tests

0 commit comments

Comments
 (0)