Skip to content

Commit be85d04

Browse files
committed
fix: database changes to make docker flows easier
1 parent f256d43 commit be85d04

6 files changed

Lines changed: 80 additions & 19 deletions

File tree

.env.example

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ DEFAULT_ROLES=user,betaUser
1818
AVAILABLE_ROLES=user,admin,betaUser,team
1919

2020
# DATABASE
21-
DATABASE_URL=postgres://myuser:mypassword@localhost:5432/seamless-auth
21+
DB_HOST=localhost
22+
DB_PORT=5432
23+
DB_NAME=seamless-auth
24+
DB_USER=myuser
25+
DB_PASSWORD=mypassword
2226

2327
# AUTH CONFIGURATION
2428
ACCESS_TOKEN_TTL=30m

Dockerfile.dev

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# NOT a production image
2+
FROM node:20-alpine
3+
WORKDIR /app
4+
5+
RUN apk add --no-cache postgresql-client netcat-openbsd
6+
7+
COPY package.json package-lock.json* ./
8+
RUN npm ci
9+
10+
COPY . .
11+
RUN mkdir -p ./keys
12+
13+
ENV NODE_ENV=development
14+
15+
EXPOSE 5312
16+
RUN chmod +x validateEnvs.sh
17+
18+
ENTRYPOINT ["./validateEnvs.sh"]
19+
CMD ["npm", "run", "dev"]

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,11 @@ cp .env.example .env
140140
At minimum, ensure these values are set:
141141

142142
```env
143-
DATABASE_URL=postgres://myuser:mypassword@host:5432/seamless-auth
143+
DB_HOST=localhost
144+
DB_PORT=5432
145+
DB_NAME=seamless-auth
146+
DB_USER=myuser
147+
DB_PASSWORD=mypassword
144148
APP_ORIGIN=http://localhost:5001
145149
ISSUER=http://localhost:5312
146150
```
@@ -163,7 +167,7 @@ docker run -d \
163167
postgres:15
164168
```
165169

166-
Update `DATABASE_URL` accordingly.
170+
Update DB env values accordingly.
167171

168172
## 4. Run Seamless Auth Server
169173

src/config/config.js

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,37 @@
55
module.exports = {
66
development: {
77
dialect: 'postgres',
8-
use_env_variable: 'DATABASE_URL',
8+
development: {
9+
username: process.env.DB_USER,
10+
password: process.env.DB_PASSWORD,
11+
database: process.env.DB_NAME,
12+
host: process.env.DB_HOST,
13+
port: process.env.DB_PORT,
14+
dialect: 'postgres',
15+
},
916
logging: 'false',
1017
},
1118
test: {
1219
dialect: 'postgres',
13-
use_env_variable: process.env.TEST_DATABASE_URL,
20+
development: {
21+
username: process.env.DB_USER,
22+
password: process.env.DB_PASSWORD,
23+
database: process.env.DB_NAME,
24+
host: process.env.DB_HOST,
25+
port: process.env.DB_PORT,
26+
dialect: 'postgres',
27+
},
1428
},
1529
production: {
1630
dialect: 'postgres',
17-
use_env_variable: process.env.DATABASE_URL,
31+
development: {
32+
username: process.env.DB_USER,
33+
password: process.env.DB_PASSWORD,
34+
database: process.env.DB_NAME,
35+
host: process.env.DB_HOST,
36+
port: process.env.DB_PORT,
37+
dialect: 'postgres',
38+
},
1839
logging: 'false',
1940
},
2041
};

src/models/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,11 @@ const logger = getLogger('sequelize');
1212

1313
const isProduction = process.env.NODE_ENV === 'production';
1414
const enableDbLogging = !isProduction && process.env.DB_LOGGING === 'true';
15+
const { DB_HOST, DB_PORT, DB_NAME, DB_USER, DB_PASSWORD } = process.env;
1516

16-
const sequelize = new Sequelize(process.env.DATABASE_URL ?? '', {
17+
const DATABASE_URL = `postgres://${DB_USER}:${DB_PASSWORD}@${DB_HOST}:${DB_PORT}/${DB_NAME}`;
18+
19+
const sequelize = new Sequelize(DATABASE_URL, {
1720
logging: enableDbLogging ? (msg) => logger.debug(msg) : false,
1821
});
1922

validateEnvs.sh

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,34 @@
11
#!/bin/sh
2+
set -euo pipefail
23

3-
required_vars=""
4+
required_vars="APP_NAME APP_ID APP_ORIGIN ISSUER AUTH_MODE DEMO DB_HOST DB_PORT DB_USER DB_PASSWORD DB_NAME DEFAULT_ROLES AVALIABLE_ROLES DB_LOGGING ACCESS_TOKEN_TTL REFRESH_TOKEN_TTL RATE_LIMIT DELAY_AFTER API_SERVICE_TOKEN JWOKS_ACTIVE_KID RPID ORIGINS"
45

56
for var in $required_vars; do
67
if [ -z "$(eval echo \$$var)" ]; then
7-
echo "Environment variable $var is not set!"
8-
env
8+
echo "Environment variable $var is not set"
99
exit 1
1010
fi
1111
done
1212

1313
echo "Generating JWKS keys"
14-
if ! node dist/scripts/initKeys.js; then
15-
echo "Key creatione failed to run"
16-
exit 1
17-
fi
18-
echo "Done Creating JWKS keys"
14+
npx tsx ./src/scripts/initKeys.ts
15+
echo "JWKS keys ready"
1916

20-
echo "Running database migrations..."
21-
npx sequelize-cli db:migrate
17+
echo "Waiting for database..."
18+
until nc -z "$DB_HOST" "$DB_PORT"; do
19+
sleep 1
20+
done
21+
22+
echo "Ensuring database exists..."
23+
export PGPASSWORD="$DB_PASSWORD"
24+
25+
psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d postgres \
26+
-tc "SELECT 1 FROM pg_database WHERE datname = '$DB_NAME'" | grep -q 1 \
27+
|| psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d postgres \
28+
-c "CREATE DATABASE \"$DB_NAME\""
29+
30+
echo "Running migrations..."
31+
npx sequelize-cli db:migrate --debug
2232

23-
echo "Starting server..."
24-
exec node dist/server.js
33+
echo "Starting application"
34+
exec npm run dev

0 commit comments

Comments
 (0)