made updates to load envs and streamline pg pool to cut down on connections #15
+2
-2
@@ -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"]
|
||||
|
||||
+2
-1
@@ -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",
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
+5
-1
@@ -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,
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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(
|
||||
'/',
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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(
|
||||
'/',
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user