From 7974d20503e7722ba44d7fed096dbd02577abad5 Mon Sep 17 00:00:00 2001 From: Bowden Date: Thu, 4 Jan 2018 16:16:00 -0800 Subject: [PATCH] have env var scripts.. but I'm not sure I like it --- .gitignore | 7 +++++++ package.json | 5 +---- sample.cmd | 11 +++++++++++ sample.sh | 12 ++++++++++++ src/app.ts | 12 ++++++------ src/util/loadSecrets.ts | 34 ++++++++++++++++++++++++++++++++++ 6 files changed, 71 insertions(+), 10 deletions(-) create mode 100644 sample.cmd create mode 100755 sample.sh create mode 100644 src/util/loadSecrets.ts diff --git a/.gitignore b/.gitignore index ebfa74d..5340f7f 100644 --- a/.gitignore +++ b/.gitignore @@ -33,3 +33,10 @@ Thumbs.db # Ignore built ts files dist/**/* + +# Ignore scripts that contains secrets +env.sh +env.cmd + +# coverage reports +coverage/**/* \ No newline at end of file diff --git a/package.json b/package.json index 0143e1c..b65c0fb 100644 --- a/package.json +++ b/package.json @@ -20,10 +20,7 @@ "build-sass": "node-sass src/public/css/main.scss dist/public/css/main.css", "watch-sass": "node-sass -w src/public/css/main.scss dist/public/css/main.css", "tslint": "tslint -c tslint.json -p tsconfig.json", - "copy-static-assets": "ts-node copyStaticAssets.ts", - "debug": "npm run build && npm run watch-debug", - "serve-debug": "nodemon --inspect dist/server.js", - "watch-debug": "concurrently -k -p \"[{name}]\" -n \"Sass,TypeScript,Node\" -c \"yellow.bold,cyan.bold,green.bold\" \"npm run watch-sass\" \"npm run watch-ts\" \"npm run serve-debug\"" + "copy-static-assets": "ts-node copyStaticAssets.ts" }, "dependencies": { "async": "^2.6.0", diff --git a/sample.cmd b/sample.cmd new file mode 100644 index 0000000..0d7c9db --- /dev/null +++ b/sample.cmd @@ -0,0 +1,11 @@ +:: Note we depend on NODE_ENV being set to dictate which of the env variables below get loaded at runtime. +:: See README for more details + +:: Database connection strings below. You'll eventually have a lot of these! +:: Get this from https://mlab.com/home after you've logged in and created a database +set MONGODB_URI=mongodb://:@ +:: This is standard if you have mongodb running locally and you didn't change default ports +set MONGODB_URI_LOCAL=mongodb://localhost:27017 + +:: Put lots of randomness in this +set SESSION_SECRET=ashdfjhasdlkjfhalksdjhflak diff --git a/sample.sh b/sample.sh new file mode 100755 index 0000000..7203f2a --- /dev/null +++ b/sample.sh @@ -0,0 +1,12 @@ +# Note we depend on NODE_ENV being set to dictate which of the env variables below get loaded at runtime. +# See README for more details + +# Database connection strings below. You'll eventually have a lot of these! +# Get this from https://mlab.com/home after you've logged in and created a database +export MONGODB_URI="mongodb://:@" +# This is standard if you have mongodb running locally and you didn't change default ports +export MONGODB_URI_DEV="mongodb://localhost:27017" + +# Put lots of randomness in these +export SESSION_SECRET="ashdfjhasdlkjfhalksdjhflak" +export SESSION_SECRET_DEV="ashdfjhasdlkjfhalksdjhflak" diff --git a/src/app.ts b/src/app.ts index 0e91813..8f3e089 100644 --- a/src/app.ts +++ b/src/app.ts @@ -12,19 +12,19 @@ import * as mongoose from "mongoose"; import * as passport from "passport"; import * as expressValidator from "express-validator"; import * as bluebird from "bluebird"; +import { MONGODB_URI, SESSION_SECRET } from "./util/loadSecrets"; + +console.log(MONGODB_URI); +console.log(SESSION_SECRET); const MongoStore = mongo(session); -// Load environment variables from .env file, where API keys and passwords are configured -dotenv.config({ path: ".env.example" }); - // Controllers (route handlers) import * as homeController from "./controllers/home"; import * as userController from "./controllers/user"; import * as apiController from "./controllers/api"; import * as contactController from "./controllers/contact"; - // API keys and Passport configuration import * as passportConfig from "./config/passport"; @@ -32,7 +32,7 @@ import * as passportConfig from "./config/passport"; const app = express(); // Connect to MongoDB -const mongoUrl = process.env.MONGOLAB_URI; +const mongoUrl = MONGODB_URI; (mongoose).Promise = bluebird; mongoose.connect(mongoUrl, {useMongoClient: true}).then( () => { /** ready to use. The `mongoose.connect()` promise resolves to undefined. */ }, @@ -53,7 +53,7 @@ app.use(expressValidator()); app.use(session({ resave: true, saveUninitialized: true, - secret: process.env.SESSION_SECRET, + secret: SESSION_SECRET, store: new MongoStore({ url: mongoUrl, autoReconnect: true diff --git a/src/util/loadSecrets.ts b/src/util/loadSecrets.ts new file mode 100644 index 0000000..5b0a550 --- /dev/null +++ b/src/util/loadSecrets.ts @@ -0,0 +1,34 @@ +// import log from './logger'; TODO + +export const ENVIRONMENT = process.env.NODE_ENV; +const prod = ENVIRONMENT === "production"; + +export const SESSION_SECRET = process.env["SESSION_SECRET"]; +export const MONGODB_URI = prod ? process.env["MONGODB_URI"] : process.env["MONGODB_URI_LOCAL"]; + + + +// if (!GITHUB_CLIENT_SECRET) { +// log.fatal("ERROR\tNo client secret. Set TCQ_GH_SECRET."); +// process.exit(1); +// } + +// if (!GITHUB_CLIENT_ID) { +// log.fatal("ERROR\tNo client id. Set TCQ_GH_ID."); +// process.exit(1); +// } + +// if (!SESSION_SECRET) { +// log.fatal("ERROR\tNo session secret. Set TCQ_SESSION_SECRET."); +// process.exit(1); +// } + +// if (!CDB_SECRET) { +// log.fatal("ERROR\tNo CosmosDB secret. Set TCQ_CDB_SECRET."); +// process.exit(1); +// } + +// if (!AI_IKEY) { +// log.fatal("ERROR\tNo Application Insights Instrumentation Key. Set TCQ_AI_IKEY."); +// process.exit(1); +// } \ No newline at end of file