diff --git a/Dockerfile b/Dockerfile index 659274b..e9236b8 100644 --- a/Dockerfile +++ b/Dockerfile @@ -18,7 +18,7 @@ ENV JWT_SECRET=${JWT_SECRET} WORKDIR /app COPY . . -RUN npm install --force --omit=dev && \ +RUN npm install --force && \ echo "DATABASE_URL=${DATABASE_URL}" >> .env && \ echo "DB_HOST=${DB_HOST}" >> .env && \ echo "DB_USERNAME=${DB_USERNAME}" >> .env && \ @@ -27,4 +27,4 @@ RUN npm install --force --omit=dev && \ echo "DB_DATABASE=${DB_DATABASE}" >> .env && \ echo "JWT_SECRET=${JWT_SECRET}" >> .env -CMD ["node", "--env-file=.env", "src/app.js"] +CMD ["npm", "run", "docker:start"] diff --git a/package.json b/package.json index 0bb4fcb..be41df7 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,8 @@ "main": "app.js", "scripts": { "dev": "nodemon src/app.js", - "start": "node src/app.js", + "start": "NODE_ENV=production node src/app.js", + "docker:start": "NODE_ENV=production node --env-file=.env src/app.js", "lint": "eslint .", "lint:fix": "eslint . --fix", "migrate": "node-pg-migrate -m ./src/migrations", diff --git a/src/app.js b/src/app.js index 72cd1fd..7f76367 100644 --- a/src/app.js +++ b/src/app.js @@ -1,3 +1,5 @@ +import { loadEnvFile } from 'node:process' // load envs asap + import express from 'express' import path from 'path' import cors from 'cors' @@ -11,6 +13,10 @@ import collectionRoutes from './routes/collections/index.js' import setRoutes from './routes/sets/index.js' import itemRoutes from './routes/items/index.js' +if (process.env.NODE_ENV !== 'test') { + loadEnvFile() +} + const app = express() const port = process.env.PORT || 3000 diff --git a/src/config/index.js b/src/config/index.js index d83f369..5629e23 100644 --- a/src/config/index.js +++ b/src/config/index.js @@ -1,5 +1,7 @@ // config/index.js -export const database = { +import { Pool } from 'pg' + +const database = { host: process.env.DB_HOST, user: process.env.DB_USER, password: process.env.DB_PASSWORD, @@ -7,6 +9,8 @@ export const database = { port: parseInt(process.env.DB_PORT, 10), } +export const pool = new Pool(database) + export const jwtEnv = { secret: process.env.JWT_SECRET, } diff --git a/src/routes/auth/index.js b/src/routes/auth/index.js index 43ce5fa..87b1f4d 100644 --- a/src/routes/auth/index.js +++ b/src/routes/auth/index.js @@ -1,14 +1,10 @@ import express from 'express' import jwt from 'jsonwebtoken' import bcrypt from 'bcryptjs' -import { database, jwtEnv } from '../../config/index.js' -import { Pool } from 'pg' +import { pool, jwtEnv } from '../../config/index.js' const router = express.Router() -// Database connection pool -const pool = new Pool(database) - // Route to handle login and issue JWT token router.post('/login', async (req, res) => { const { username, password } = req.body diff --git a/src/routes/collections/index.js b/src/routes/collections/index.js index 2a61e8c..b13475c 100644 --- a/src/routes/collections/index.js +++ b/src/routes/collections/index.js @@ -1,13 +1,9 @@ import express from 'express' -import { Pool } from 'pg' -import { database } from '../../config/index.js' +import { pool } from '../../config/index.js' import { check, validationResult } from 'express-validator' const router = express.Router() -// Create a connection pool to the database -const pool = new Pool(database) - // Get all collections router.get('/', async (req, res) => { try { diff --git a/src/routes/images/index.js b/src/routes/images/index.js index bc1d168..c114859 100644 --- a/src/routes/images/index.js +++ b/src/routes/images/index.js @@ -1,13 +1,9 @@ import express from 'express' -import { Pool } from 'pg' -import { database } from '../../config/index.js' +import { pool } from '../../config/index.js' import { check, validationResult } from 'express-validator' const router = express.Router() -// Create a connection pool to the database -const pool = new Pool(database) - // CREATE AN IMAGE router.post( '/', diff --git a/src/routes/items/index.js b/src/routes/items/index.js index 9cc4ec4..77c2098 100644 --- a/src/routes/items/index.js +++ b/src/routes/items/index.js @@ -1,13 +1,9 @@ import express from 'express' -import { Pool } from 'pg' -import { database } from '../../config/index.js' +import { pool } from '../../config/index.js' import { check, validationResult } from 'express-validator' const router = express.Router() -// Create a connection pool to the database -const pool = new Pool(database) - // Get all items router.get('/', async (req, res) => { const client = await pool.connect() diff --git a/src/routes/locations/index.js b/src/routes/locations/index.js index f0c199a..e4e5be6 100644 --- a/src/routes/locations/index.js +++ b/src/routes/locations/index.js @@ -1,13 +1,9 @@ import express from 'express' -import { Pool } from 'pg' -import { database } from '../../config/index.js' +import { pool } from '../../config/index.js' import { check, validationResult } from 'express-validator' const router = express.Router() -// Create a connection pool to the database -const pool = new Pool(database) - // Create a new location router.post( '/', diff --git a/src/routes/roles/index.js b/src/routes/roles/index.js index 134011f..4c06b81 100644 --- a/src/routes/roles/index.js +++ b/src/routes/roles/index.js @@ -1,12 +1,8 @@ import express from 'express' -import { database } from '../../config/index.js' -import { Pool } from 'pg' +import { pool } from '../../config/index.js' const router = express.Router() -// Create a connection pool to the database -const pool = new Pool(database) - // Create a new role router.post('/', async (req, res) => { const { name } = req.body diff --git a/src/routes/sets/index.js b/src/routes/sets/index.js index 246b254..403e469 100644 --- a/src/routes/sets/index.js +++ b/src/routes/sets/index.js @@ -1,13 +1,9 @@ import express from 'express' -import { Pool } from 'pg' -import { database } from '../../config/index.js' +import { pool } from '../../config/index.js' import { check, validationResult } from 'express-validator' const router = express.Router() -// Create a connection pool to the database -const pool = new Pool(database) - // Get all sets router.get('/', async (req, res) => { try { diff --git a/src/routes/users/index.js b/src/routes/users/index.js index 0d2f5f5..46f4825 100644 --- a/src/routes/users/index.js +++ b/src/routes/users/index.js @@ -1,12 +1,8 @@ import express from 'express' -import { database } from '../../config/index.js' -import { Pool } from 'pg' +import { pool } from '../../config/index.js' const router = express.Router() -// Create a connection pool to the database -const pool = new Pool(database) - /** * Get all users. */