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
|
WORKDIR /app
|
||||||
COPY . .
|
COPY . .
|
||||||
RUN npm install --force --omit=dev && \
|
RUN npm install --force && \
|
||||||
echo "DATABASE_URL=${DATABASE_URL}" >> .env && \
|
echo "DATABASE_URL=${DATABASE_URL}" >> .env && \
|
||||||
echo "DB_HOST=${DB_HOST}" >> .env && \
|
echo "DB_HOST=${DB_HOST}" >> .env && \
|
||||||
echo "DB_USERNAME=${DB_USERNAME}" >> .env && \
|
echo "DB_USERNAME=${DB_USERNAME}" >> .env && \
|
||||||
@@ -27,4 +27,4 @@ RUN npm install --force --omit=dev && \
|
|||||||
echo "DB_DATABASE=${DB_DATABASE}" >> .env && \
|
echo "DB_DATABASE=${DB_DATABASE}" >> .env && \
|
||||||
echo "JWT_SECRET=${JWT_SECRET}" >> .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",
|
"main": "app.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"dev": "nodemon src/app.js",
|
"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": "eslint .",
|
||||||
"lint:fix": "eslint . --fix",
|
"lint:fix": "eslint . --fix",
|
||||||
"migrate": "node-pg-migrate -m ./src/migrations",
|
"migrate": "node-pg-migrate -m ./src/migrations",
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
import { loadEnvFile } from 'node:process' // load envs asap
|
||||||
|
|
||||||
import express from 'express'
|
import express from 'express'
|
||||||
import path from 'path'
|
import path from 'path'
|
||||||
import cors from 'cors'
|
import cors from 'cors'
|
||||||
@@ -11,6 +13,10 @@ import collectionRoutes from './routes/collections/index.js'
|
|||||||
import setRoutes from './routes/sets/index.js'
|
import setRoutes from './routes/sets/index.js'
|
||||||
import itemRoutes from './routes/items/index.js'
|
import itemRoutes from './routes/items/index.js'
|
||||||
|
|
||||||
|
if (process.env.NODE_ENV !== 'test') {
|
||||||
|
loadEnvFile()
|
||||||
|
}
|
||||||
|
|
||||||
const app = express()
|
const app = express()
|
||||||
const port = process.env.PORT || 3000
|
const port = process.env.PORT || 3000
|
||||||
|
|
||||||
|
|||||||
+5
-1
@@ -1,5 +1,7 @@
|
|||||||
// config/index.js
|
// config/index.js
|
||||||
export const database = {
|
import { Pool } from 'pg'
|
||||||
|
|
||||||
|
const database = {
|
||||||
host: process.env.DB_HOST,
|
host: process.env.DB_HOST,
|
||||||
user: process.env.DB_USER,
|
user: process.env.DB_USER,
|
||||||
password: process.env.DB_PASSWORD,
|
password: process.env.DB_PASSWORD,
|
||||||
@@ -7,6 +9,8 @@ export const database = {
|
|||||||
port: parseInt(process.env.DB_PORT, 10),
|
port: parseInt(process.env.DB_PORT, 10),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const pool = new Pool(database)
|
||||||
|
|
||||||
export const jwtEnv = {
|
export const jwtEnv = {
|
||||||
secret: process.env.JWT_SECRET,
|
secret: process.env.JWT_SECRET,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,14 +1,10 @@
|
|||||||
import express from 'express'
|
import express from 'express'
|
||||||
import jwt from 'jsonwebtoken'
|
import jwt from 'jsonwebtoken'
|
||||||
import bcrypt from 'bcryptjs'
|
import bcrypt from 'bcryptjs'
|
||||||
import { database, jwtEnv } from '../../config/index.js'
|
import { pool, jwtEnv } from '../../config/index.js'
|
||||||
import { Pool } from 'pg'
|
|
||||||
|
|
||||||
const router = express.Router()
|
const router = express.Router()
|
||||||
|
|
||||||
// Database connection pool
|
|
||||||
const pool = new Pool(database)
|
|
||||||
|
|
||||||
// Route to handle login and issue JWT token
|
// Route to handle login and issue JWT token
|
||||||
router.post('/login', async (req, res) => {
|
router.post('/login', async (req, res) => {
|
||||||
const { username, password } = req.body
|
const { username, password } = req.body
|
||||||
|
|||||||
@@ -1,13 +1,9 @@
|
|||||||
import express from 'express'
|
import express from 'express'
|
||||||
import { Pool } from 'pg'
|
import { pool } from '../../config/index.js'
|
||||||
import { database } from '../../config/index.js'
|
|
||||||
import { check, validationResult } from 'express-validator'
|
import { check, validationResult } from 'express-validator'
|
||||||
|
|
||||||
const router = express.Router()
|
const router = express.Router()
|
||||||
|
|
||||||
// Create a connection pool to the database
|
|
||||||
const pool = new Pool(database)
|
|
||||||
|
|
||||||
// Get all collections
|
// Get all collections
|
||||||
router.get('/', async (req, res) => {
|
router.get('/', async (req, res) => {
|
||||||
try {
|
try {
|
||||||
|
|||||||
@@ -1,13 +1,9 @@
|
|||||||
import express from 'express'
|
import express from 'express'
|
||||||
import { Pool } from 'pg'
|
import { pool } from '../../config/index.js'
|
||||||
import { database } from '../../config/index.js'
|
|
||||||
import { check, validationResult } from 'express-validator'
|
import { check, validationResult } from 'express-validator'
|
||||||
|
|
||||||
const router = express.Router()
|
const router = express.Router()
|
||||||
|
|
||||||
// Create a connection pool to the database
|
|
||||||
const pool = new Pool(database)
|
|
||||||
|
|
||||||
// CREATE AN IMAGE
|
// CREATE AN IMAGE
|
||||||
router.post(
|
router.post(
|
||||||
'/',
|
'/',
|
||||||
|
|||||||
@@ -1,13 +1,9 @@
|
|||||||
import express from 'express'
|
import express from 'express'
|
||||||
import { Pool } from 'pg'
|
import { pool } from '../../config/index.js'
|
||||||
import { database } from '../../config/index.js'
|
|
||||||
import { check, validationResult } from 'express-validator'
|
import { check, validationResult } from 'express-validator'
|
||||||
|
|
||||||
const router = express.Router()
|
const router = express.Router()
|
||||||
|
|
||||||
// Create a connection pool to the database
|
|
||||||
const pool = new Pool(database)
|
|
||||||
|
|
||||||
// Get all items
|
// Get all items
|
||||||
router.get('/', async (req, res) => {
|
router.get('/', async (req, res) => {
|
||||||
const client = await pool.connect()
|
const client = await pool.connect()
|
||||||
|
|||||||
@@ -1,13 +1,9 @@
|
|||||||
import express from 'express'
|
import express from 'express'
|
||||||
import { Pool } from 'pg'
|
import { pool } from '../../config/index.js'
|
||||||
import { database } from '../../config/index.js'
|
|
||||||
import { check, validationResult } from 'express-validator'
|
import { check, validationResult } from 'express-validator'
|
||||||
|
|
||||||
const router = express.Router()
|
const router = express.Router()
|
||||||
|
|
||||||
// Create a connection pool to the database
|
|
||||||
const pool = new Pool(database)
|
|
||||||
|
|
||||||
// Create a new location
|
// Create a new location
|
||||||
router.post(
|
router.post(
|
||||||
'/',
|
'/',
|
||||||
|
|||||||
@@ -1,12 +1,8 @@
|
|||||||
import express from 'express'
|
import express from 'express'
|
||||||
import { database } from '../../config/index.js'
|
import { pool } from '../../config/index.js'
|
||||||
import { Pool } from 'pg'
|
|
||||||
|
|
||||||
const router = express.Router()
|
const router = express.Router()
|
||||||
|
|
||||||
// Create a connection pool to the database
|
|
||||||
const pool = new Pool(database)
|
|
||||||
|
|
||||||
// Create a new role
|
// Create a new role
|
||||||
router.post('/', async (req, res) => {
|
router.post('/', async (req, res) => {
|
||||||
const { name } = req.body
|
const { name } = req.body
|
||||||
|
|||||||
@@ -1,13 +1,9 @@
|
|||||||
import express from 'express'
|
import express from 'express'
|
||||||
import { Pool } from 'pg'
|
import { pool } from '../../config/index.js'
|
||||||
import { database } from '../../config/index.js'
|
|
||||||
import { check, validationResult } from 'express-validator'
|
import { check, validationResult } from 'express-validator'
|
||||||
|
|
||||||
const router = express.Router()
|
const router = express.Router()
|
||||||
|
|
||||||
// Create a connection pool to the database
|
|
||||||
const pool = new Pool(database)
|
|
||||||
|
|
||||||
// Get all sets
|
// Get all sets
|
||||||
router.get('/', async (req, res) => {
|
router.get('/', async (req, res) => {
|
||||||
try {
|
try {
|
||||||
|
|||||||
@@ -1,12 +1,8 @@
|
|||||||
import express from 'express'
|
import express from 'express'
|
||||||
import { database } from '../../config/index.js'
|
import { pool } from '../../config/index.js'
|
||||||
import { Pool } from 'pg'
|
|
||||||
|
|
||||||
const router = express.Router()
|
const router = express.Router()
|
||||||
|
|
||||||
// Create a connection pool to the database
|
|
||||||
const pool = new Pool(database)
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get all users.
|
* Get all users.
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user