Skip to content

Commit

Permalink
Migrate to dotenvx and dotenv-vault
Browse files Browse the repository at this point in the history
  • Loading branch information
jrock-3 committed Feb 29, 2024
1 parent c5c1084 commit 69d21cb
Show file tree
Hide file tree
Showing 10 changed files with 1,393 additions and 184 deletions.
25 changes: 25 additions & 0 deletions .env.vault
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#/-------------------.env.vault---------------------/
#/ cloud-agnostic vaulting standard /
#/ [how it works](https://dotenv.org/env-vault) /
#/--------------------------------------------------/

# development
DOTENV_VAULT_DEVELOPMENT="mnuq6r009xu1oBXlbgX1oP5kPeOwUPukxBK3cphuZ0Fsl/hOEQYsReIhQ5DlluUAjOMZvWuOwVDX5tfmqrtpBqEbbQZKpqYp9t/PPVr6hABwU1ELGuQDENi1iqbuQGI/WTPW6QjDCgAbEavBIkIOeSAIL5dCdrq7vjCO//Dz+r0ESX+kfQcVKHTuLD4LxqwqSzxjAq+L79v4rmaHg1avKod1dOCZiP1MUtWkVdEpshCbzy2mos5I4gpGQErTfSLT16ihORsD7PCzsVf2b0vYj+OgnXtpWUAjGj+qrCxjEqYTe2xvQjG+6lons4lbDcqEh1zxw84cKD2S1Iiz0MsaUkhNfSuv66oiuniZaxe+yBSu/cKMl+qD5qE53m4N9kDUQk1pQcOM8FpyvaWGwrmrl4kmG1AHii957jSDNsz+6rOLDLmkX3BbxK+1XXN3wwa/kRdOjXQ/zN7r5kikK0dqb64Kvy1MJ5+J2PIB53J8CE5S/auQgiLq80/AhjhH33qTyXTszCuJ4AlDSZwP/l0TsSbZxfQVkR1lSscKzCv3XHuq+rhIM+GbgUYoLIkhbhc2YLVjkznw16hjPyP+kQ4vASXlGz/6goIpAPf4a+f8dTg8zH4dRpYdU66VHxwpiqaqzInykBBHb29OSBmfOuw/6M3RESKyMGlsnRqzKe+mQM0VYEbN7OtmoGOND5nMh5IzmjpORC4uvyRt1EzI5dXDZrefi+ySHaUC5uf/df/x4d/IAgLqlQNQGKRnXsjutovrCy6PdcRD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XSgEMWwXSBzfZYq1lGMmyaoGLzu0MAZ9It05a2svjZrR2t4h/hh4ClQUJtruqVMhutr/XxoO2l8So7bLBn1duujvpgmlSBO67n4c0OjfGpxNHfRU6anaWHqVNNPIZT5DUs/JqzfRXVnI46syrydSW2cLUcajH5jpl7uH2RWwazYncklPwVjwWz3Bb/kNK7KhJyfSTUGi5gSPsttRLdQYfbYr0GBUZIDq67G6mNNZGO9k424="
DOTENV_VAULT_STAGING_VERSION=3

# production
DOTENV_VAULT_PRODUCTION="JNTkw2+v+staeSB83lDkYpr/J11nRWfCvFlyCujLqg5GIFn0VlDIQe3YBFoUky/hcUBKX9AIwfdoxeVClWD4lHqhlZxA+gV6z3aRnmtHHthqWGzBdKs7MTR5IoaYczB9OCFa6yKfiu7Fxd64UOMF+5JblajJg3/lB3WkkEDVz4ouCQdU6B3PR5Yhlc5ZQ6SATk2eJfK5594WFSXhI3xIpOCijxw/tNhYs4G9zLYeduJdtgrxIfqOWvkV9RJIXHebqI1WKiKwuPwbU9z3ZYkCKbI0n9eM1gueFz0mnwf74vn+HjprBKqL0aGCFX0h8eNnQvoipbRKUiY+nQSq8HwhSfy69M1ky9JB8ouuo9zuiku7/T6deyV8HoMJkhukUjNqB7ptbOk="
DOTENV_VAULT_PRODUCTION_VERSION=3

#/----------------settings/metadata-----------------/
DOTENV_VAULT="vlt_2fabcdc982d04b5a1e33c3e8727a59ecabadc39576535c5d2ad6eb8840bd7c2e"
DOTENV_API_URL="https://vault.dotenv.org"
DOTENV_CLI="npx dotenv-vault@latest"
17 changes: 3 additions & 14 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,13 @@ const schedule = require('node-schedule'); // FIXME: Keep only if scheduling cro
// Routes
const sampleRouter = require('./routes/sample'); // FIXME: delete sample router

require('dotenv').config();

schedule.scheduleJob('0 0 0 0 0', () => console.log('Hello Cron Job!')); // FIXME: delete sample cronjob

const app = express();

const CLIENT_HOSTNAME =
process.env.NODE_ENV === 'development'
? `${process.env.DEV_CLIENT_HOSTNAME}:${process.env.DEV_CLIENT_PORT}`
: process.env.PROD_CLIENT_HOSTNAME;
const SERVER_PORT =
process.env.NODE_ENV === 'development'
? process.env.DEV_SERVER_PORT
: process.env.PROD_SERVER_PORT;

app.use(
cors({
origin: CLIENT_HOSTNAME,
origin: process.env.CLIENT_URL,
}),
);

Expand All @@ -32,6 +21,6 @@ app.use(cookieParser());
app.use(express.json()); // for req.body
app.use('/', sampleRouter); // FIXME: delete sample endpoint

app.listen(SERVER_PORT, () => {
console.log(`Server listening on ${SERVER_PORT}`);
app.listen(process.env.SERVER_PORT, () => {
console.log(`Server listening on ${process.env.SERVER_PORT}`);
});
19 changes: 4 additions & 15 deletions common/s3.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,11 @@
const aws = require('aws-sdk');
const crypto = require('crypto');

const region =
process.env.NODE_ENV === 'development' ? process.env.DEV_S3_REGION : process.env.PROD_S3_REGION;
const accessKeyId =
process.env.NODE_ENV === 'development'
? process.env.DEV_S3_ACCESS_KEY_ID
: process.env.PROD_S3_ACCESS_KEY_ID;
const secretAccessKey =
process.env.NODE_ENV === 'development'
? process.env.DEV_SECRET_ACCESS_KEY
: process.env.PROD_S3_SECRET_ACCESS_KEY;

// initialize a S3 instance
const s3 = new aws.S3({
region,
accessKeyId,
secretAccessKey,
region: process.env.S3_REGION,
accessKeyId: process.env.S3_ACCESS_KEY_ID,
secretAccessKey: process.env.S3_SECRET_ACCESS_KEY,
signatureVersion: 'v4',
});

Expand All @@ -34,7 +23,7 @@ const getS3UploadURL = async () => {
};

// get a s3 upload url
const uploadURL = await s3.getSignedUrl('putObject', params);
const uploadURL = s3.getSignedUrl('putObject', params);

return uploadURL;
};
Expand Down
21 changes: 4 additions & 17 deletions common/transporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,16 @@

const nodemailer = require('nodemailer');

require('dotenv').config();

const user =
process.env.NODE_ENV === 'development'
? process.env.DEV_EMAIL_USERNAME
: process.env.PROD_EMAIL_USERNAME;
const pass =
process.env.NODE_ENV === 'development'
? process.env.DEV_EMAIL_PASSWORD
: process.env.PROD_EMAIL_PASSWORD;
const emailSender =
process.env.NODE_ENV === 'development'
? `${process.env.DEV_EMAIL_FIRSTNAME} ${process.env.DEV_EMAIL_LASTNAME}`
: `${process.env.PROD_EMAIL_FIRSTNAMS} ${process.env.DEV_EMAIL_LASTNAME}`;
const emailSender = `${process.env.EMAIL_FIRSTNAME} ${process.env.EMAIL_LASTNAME}`;

// sender information
const transport = {
host: 'smtp.gmail.com', // e.g. smtp.gmail.com
auth: {
user,
pass,
user: process.env.EMAIL_USERNAME,
pass: process.env.EMAIL_PASSWORD,
},
from: user,
from: process.env.EMAIL_USERNAME,
secure: true,
};

Expand Down
53 changes: 0 additions & 53 deletions env.template

This file was deleted.

18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"author": "CTC <[email protected]>",
"license": "MIT",
"scripts": {
"start": "nodemon app.js",
"start": "dotenvx run -- nodemon app.js",
"prepare": "husky install",
"lint": "eslint '**/*.js'",
"format": "prettier --write \"**/*.js\""
Expand All @@ -19,23 +19,23 @@
"aws-sdk": "^2.1568.0",
"cookie-parser": "^1.4.6",
"cors": "^2.8.5",
"dotenv": "^10.0.0",
"eslint": "^8.1.0",
"eslint-config-airbnb-base": "^14.2.1",
"eslint-plugin-import": "^2.25.2",
"eslint-plugin-prettier": "^4.0.0",
"express": "^4.17.1",
"mongoose": "^8.2.0",
"mysql": "^2.18.1",
"node-schedule": "^2.1.1",
"nodemailer": "^6.9.10",
"nodemon": "^2.0.14",
"pg-promise": "^11.5.4",
"prettier": "^2.4.1"
"pg-promise": "^11.5.4"
},
"devDependencies": {
"prettier": "^2.4.1",
"eslint": "^8.1.0",
"eslint-config-airbnb-base": "^14.2.1",
"eslint-plugin-import": "^2.25.2",
"eslint-plugin-prettier": "^4.0.0",
"@dotenvx/dotenvx": "^0.24.0",
"eslint-config-prettier": "^8.3.0",
"husky": "^7.0.4",
"lint-staged": "^11.2.6"
}
}
}
5 changes: 1 addition & 4 deletions server/db-mongoose.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
// FIXME: keep file only if using mongodb
const mongoose = require('mongoose');

const mongoURI =
process.env.NODE_ENV === 'development' ? process.env.DEV_DB_URI : process.env.PROD_DB_URI;

mongoose.connect(mongoURI, {
mongoose.connect(process.env.DB_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
Expand Down
23 changes: 4 additions & 19 deletions server/db-mysql.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,11 @@

const mysql = require('mysql');

const host =
process.env.NODE_ENV === 'development'
? process.env.DEV_DB_HOSTNAME
: process.env.PROD_DB_HOSTNAME;
const user =
process.env.NODE_ENV === 'development'
? process.env.DEV_DB_USERNAME
: process.env.PROD_DB_USERNAME;
const password =
process.env.NODE_ENV === 'development'
? process.env.DEV_DB_PASSWORD
: process.env.PROD_DB_PASSWORD;
const database =
process.env.NODE_ENV === 'development' ? process.env.DEV_DB_NAME : process.env.PROD_DB_NAME;

const db = mysql.createConnection({
host,
user,
password,
database,
host: process.env.DB_HOSTNAME,
user: process.env.DB_USERNAME,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
});

db.connect();
Expand Down
28 changes: 5 additions & 23 deletions server/db-pgp.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,13 @@
// FIXME: keep file only if using postgresql

const pgp = require('pg-promise')({});
require('dotenv').config();

const host =
process.env.NODE_ENV === 'development'
? process.env.DEV_DB_HOSTNAME
: process.env.PROD_DB_HOSTNAME;
const user =
process.env.NODE_ENV === 'development'
? process.env.DEV_DB_USERNAME
: process.env.PROD_DB_USERNAME;
const password =
process.env.NODE_ENV === 'development'
? process.env.DEV_DB_PASSWORD
: process.env.PROD_DB_PASSWORD;
const database =
process.env.NODE_ENV === 'development' ? process.env.DEV_DB_NAME : process.env.PROD_DB_NAME;
const port =
process.env.NODE_ENV === 'development' ? process.env.DEV_DB_PORT : process.env.PROB_DB_PORT;

const db = pgp({
host,
user,
password,
database,
port,
host: process.env.DB_HOSTNAME,
user: process.env.DB_USERNAME,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
port: process.env.DB_PORT,
ssl: {
rejectUnauthorized: false,
},
Expand Down
Loading

0 comments on commit 69d21cb

Please sign in to comment.