Skip to content

Commit 007b22a

Browse files
authored
Merge pull request #64 from JDIZM/update-deps
[JDI-132] Update deps, docker and minor changes
2 parents 18c5065 + c42fcf6 commit 007b22a

21 files changed

+173
-125
lines changed

.dockerignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
node_modules
22
.git
33
dist
4-
.env
4+
.env
5+
node_modules/
6+
!nodes_modules/.prisma

.env.example

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,9 @@
22
## prisma
33
###
44
DATABASE_URL=mongodb://root:example@localhost:27017/dbname
5-
APP_URL=http://localhost
6-
PORT=3000
5+
PORT=4000
6+
7+
###
8+
# GCP // Firebase credentials.
9+
###
10+
GOOGLE_APPLICATION_CREDENTIALS=service-account-file.json

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@ node_modules
22
dist
33
coverage
44
.env
5-
package-lock.json
5+
package-lock.json
6+
pnpm-lock.yaml
7+
yarn.lock

.vscode/settings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44
"editor.fontFamily": "JetBrains Mono",
55
"editor.defaultFormatter": "esbenp.prettier-vscode",
66
"editor.formatOnSave": true,
7-
"typescript.tsdk": "node_modules/typescript/lib"
7+
"typescript.tsdk": "node_modules/typescript/lib",
8+
"prettier.prettierPath": "node_modules/prettier/index.cjs"
89
}

Dockerfile

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,65 @@
11
# use a slimmer alpine image to consume less memory
22
# https://viralganatra.com/docker-nodejs-production-secure-best-practices/
3-
FROM node:20-alpine
3+
FROM node:20-alpine AS base
4+
5+
ENV PNPM_VERSION=9.4.0
6+
7+
RUN npm install -g pnpm@$PNPM_VERSION
48

5-
# set working directory
69
WORKDIR /app
710

11+
ARG NODE_ENV
12+
ENV NODE_ENV=${NODE_ENV}
13+
814
# required for gcloud; will require a service account key to be copied to the path location
915
ENV GOOGLE_APPLICATION_CREDENTIALS=/app/gcloud.json
1016

1117
# install deps first so we can cache them
12-
COPY package*.json ./
18+
COPY package*.json pnpm-lock.yaml ./
1319
COPY prisma ./prisma/
14-
RUN npm ci && npm cache clean --force
20+
RUN pnpm install --frozen-lockfile
21+
RUN npx prisma generate
22+
23+
FROM base AS builder
24+
25+
WORKDIR /app
1526

16-
# build the app
1727
COPY . .
28+
1829
RUN mkdir dist
19-
RUN npm run build
30+
31+
RUN pnpm build
32+
33+
FROM base AS runner
34+
35+
WORKDIR /app
36+
37+
RUN addgroup --system --gid 1001 express
38+
RUN adduser --system --uid 1001 express
39+
40+
COPY --from=builder --chown=express:express /app/prisma /app/prisma
41+
COPY --from=builder --chown=express:express /app/node_modules /app/node_modules
42+
COPY --from=builder --chown=express:express /app/dist /app/dist
43+
COPY --from=builder --chown=express:express /app/package.json /app/package.json
44+
45+
USER express
46+
47+
EXPOSE 4000
48+
49+
# --- Development ---
50+
51+
FROM runner AS dev
52+
53+
WORKDIR /app
54+
55+
COPY --from=builder --chown=express:express /app/.env /app/.env
56+
57+
CMD ["node", "./dist/server.cjs"]
58+
59+
# --- Production ---
60+
61+
FROM runner AS prod
62+
63+
WORKDIR /app
2064

2165
CMD ["node", "./dist/server.cjs"]

README.md

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# node-express-backend-component
1+
# node-express-firebase-mongodb
22

33
- [tsx](https://github.com/esbuild-kit/tsx)
44
- [pkgroll](https://github.com/privatenumber/pkgroll)
@@ -13,6 +13,8 @@
1313
- [firebase auth](https://firebase.google.com/docs/auth/admin)
1414
- [gcp](https://cloud.google.com/)
1515
- [digitalocean](https://www.digitalocean.com/)
16+
- [helmet](https://helmetjs.github.io/)
17+
- [cookie-parser](https://www.npmjs.com/package/cookie-parser)
1618

1719
A simple node/express backend api template.
1820

@@ -28,7 +30,7 @@ curl https://get.volta.sh | bash
2830

2931
This will require a mongodb database to be setup. You can set one up at https://cloud.mongodb.com/
3032

31-
This project also uses firebase auth for authentication. You can set one up at https://firebase.google.com/ and deployment is handled with DigitalOcean. See the authentication and deployment sections for more info.
33+
This project also uses firebase auth for authentication. You can set one up at https://firebase.google.com/ and deployment is handled with DigitalOcean. See the [Firebase Auth](#firebase-auth) section for more info on setting up your credentials and project.
3234

3335
## ESM Node
3436

@@ -54,7 +56,7 @@ npm i
5456
npm run dev
5557
5658
# view it running on localhost
57-
curl localhost:3000
59+
curl localhost:4000
5860
```
5961

6062
Be sure to configure the .env file with the correct values and make sure you have a mongodb database setup and firebase application credentials.
@@ -90,13 +92,13 @@ docker build . --tag node-express
9092
docker build . --tag node-express --platform linux/amd64
9193
9294
# start the docker container
93-
docker run -d -p 3000:3000 node-express
95+
docker run -d -p 4000:4000 node-express
9496
9597
# view it running on localhost
96-
curl localhost:3000
98+
curl localhost:4000
9799
```
98100

99-
When building with Docker locally it will require the service account key in the project root; this is because the Dockerfile is copying the service account key file into the image. See the [Firebase Auth](#firebase-auth) section for more info on the service account key file and Google Application Credentials.
101+
When building with Docker locally it will require the service account key to be set; this is because the Dockerfile is copying the service account key file into the image. See the [Firebase Auth](#firebase-auth) section for more info on the service account key file and Google Application Credentials.
100102

101103
## Database
102104

@@ -146,6 +148,9 @@ Sign in with email and password is supported out of the box with Firebase Auth.
146148
```js
147149
# sign in with email and password by posting
148150
https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key=[API_KEY]
151+
152+
# refresh token
153+
https://securetoken.googleapis.com/v1/token?key=[API_KEY]
149154
```
150155

151156
## Permissions
@@ -177,7 +182,7 @@ A claim is defined when the user is created which defines the user's role and pe
177182

178183
This project uses Firebase Auth for authentication. The firebase admin sdk is used to verify the token and decode the claims.
179184

180-
Because we are not running in a google environment we need to initialize the firebase admin sdk with a service account key file.
185+
Because we are not running in a google environment we need to initialize the firebase admin sdk with a service account key file. see the [Firebase Admin SDK docs](https://firebase.google.com/docs/admin/setup#initialize_the_sdk_in_non-google_environments) for more info.
181186

182187
This requires a service account key file at `$HOME/gcloud.json`. The `GOOGLE_APPLICATION_CREDENTIALS` env variable must be set to the path of the service account key file.
183188

@@ -187,6 +192,8 @@ You can create a service account from the firebase console and place in your hom
187192
export GOOGLE_APPLICATION_CREDENTIALS=$HOME/gcloud.json
188193
```
189194

195+
You can also set this in the `.env` file but if you have set the env variable in your shell it will take precedence.
196+
190197
When running in CI/CD the service account key file is stored as a secret and the env variable is set in the Dockerfile using the copied service account key file fron secrets.
191198

192199
## Deployment with DigitalOcean

docker-compose.yml

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
1-
version: "3.1"
2-
31
services:
42
app:
53
build:
64
context: .
75
environment:
8-
- NODE_ENV=development
6+
- NODE_ENV=${NODE_ENV:-development}
97
- DATABASE_URL=${DATABASE_URL}
10-
- APP_URL=${APP_URL}
118
- PORT=${PORT}
129
ports:
1310
- "${PORT}:${PORT}"
1411
volumes:
1512
# set the GOOGLE_APPLICATION_CREDENTIALS env variable to the path of the gcloud.json file
16-
- $HOME/gcloud.json:/app/gcloud.json
13+
# will use the default path if not set. If shell env is set it will take precedence.
14+
- ${GOOGLE_APPLICATION_CREDENTIALS:-$HOME/gcloud.json}:/app/gcloud.json

package.json

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "node-express-backend-component",
2+
"name": "node-express-firebase-mongodb",
33
"version": "1.0.0",
44
"description": "",
55
"main": "dist/server.cjs",
@@ -13,7 +13,7 @@
1313
"format": "prettier -w '**/*.{js,ts,mjs,cjs,json,tsx,jsx}'",
1414
"format:check": "prettier --check '**/*.{js,ts,mjs,cjs,json,tsx,jsx}'",
1515
"tsc:check": "tsc -p tsconfig.json --noEmit",
16-
"dev": "tsx watch ./src/server.ts",
16+
"dev": "tsx watch ./src/server.ts | pino-pretty",
1717
"build": "pkgroll",
1818
"test": "vitest --run --coverage"
1919
},
@@ -23,18 +23,19 @@
2323
"devDependencies": {
2424
"@typescript-eslint/eslint-plugin": "^7.0.0",
2525
"@typescript-eslint/parser": "^7.0.0",
26-
"@vitest/coverage-v8": "^2.0.0",
26+
"@vitest/coverage-istanbul": "^2.0.5",
2727
"dotenv": "^16.3.1",
2828
"eslint": "^8.37.0",
2929
"eslint-config-prettier": "^9.0.0",
3030
"eslint-import-resolver-typescript": "^3.5.5",
3131
"eslint-plugin-import": "^2.27.5",
32+
"pino-pretty": "^11.2.2",
3233
"pkgroll": "^2.0.0",
33-
"prettier": "^3.0.0",
34+
"prettier": "^3.3.3",
3435
"prisma": "^5.2.0",
3536
"tsx": "^4.0.0",
3637
"typescript": "^5.0.4",
37-
"vitest": "^2.0.0",
38+
"vitest": "^2.0.5",
3839
"zod": "^3.21.4",
3940
"zod-prisma-types": "^3.0.0"
4041
},
@@ -49,19 +50,21 @@
4950
},
5051
"dependencies": {
5152
"@prisma/client": "^5.2.0",
53+
"@types/cookie-parser": "^1.4.7",
5254
"@types/cors": "^2.8.17",
53-
"@types/node": "^20.2.1",
54-
"@types/bcrypt": "^5.0.0",
5555
"@types/express": "^4.17.17",
56-
"bcrypt": "^5.1.1",
56+
"@types/node": "^22.1.0",
57+
"cookie-parser": "^1.4.6",
5758
"cors": "^2.8.5",
5859
"express": "^4.18.2",
5960
"firebase-admin": "^12.0.0",
61+
"helmet": "^7.1.0",
6062
"mongodb": "^6.3.0",
6163
"pino": "^9.0.0",
6264
"pino-http": "^10.0.0"
6365
},
6466
"optionalDependencies": {
6567
"@rollup/rollup-linux-x64-gnu": "^4.9.6"
66-
}
68+
},
69+
"packageManager": "pnpm@9.4.0+sha512.f549b8a52c9d2b8536762f99c0722205efc5af913e77835dbccc3b0b0b2ca9e7dc8022b78062c17291c48e88749c70ce88eb5a74f1fa8c4bf5e18bb46c8bd83a"
6770
}

0 commit comments

Comments
 (0)