Skip to content

Commit

Permalink
dotenv (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnpapa authored Dec 2, 2017
1 parent a048921 commit 86634db
Show file tree
Hide file tree
Showing 11 changed files with 49 additions and 61 deletions.
6 changes: 6 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
SERVER_PORT=3001
PUBLICWEB=./server/www
COSMOSDB_ACCOUNT=your_cosmos_account
COSMOSDB_DB=your_cosmos_db
COSMOSDB_KEY=your_cosmos_key
COSMOSDB_PORT=10255
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# ignore server configuration
*/env/development.js
*.azcli

# Logs
Expand Down
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
## vue-heroes Changelog

<a name="1.1.0"></a>

# 1.1.0 (2017-12-02)

_Breaking Changes_

* Added `.env` file in the root
* Added `dotenv` to read the `.env` file when running locally (without docker)
* Removed `./src/server/environment` folder and its contents (replaced with `.env`)
* docker-compose "debug" now uses the `.env` file
* docker-compose for prod and cloud hosting will require ENV vars to be set in the cloud
* `.env` is gitignored and not copied in the `Dockerfile` build process
16 changes: 7 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,15 @@ npm install

* Configure Cosmos DB server settings

Rename the `example.js` file to `development.js` in the `server/env/` folder and update it with your Cosmos DB settings. Replace the accountName, databaseName, key, and port with your specific configuration.
Rename the `.env.example.js` file to `.env` in the root folder and update it with your Cosmos DB settings. Replace the accountName, databaseName, key, and port with your specific configuration.

```javascript
// server/env/development.js

module.exports = {
accountName: 'your-cosmosdb-account-name-goes-here',
databaseName: 'your-cosmosdb-database-name-goes-here',
key: 'your-key-goes-here',
port: 10255
};
SERVER_PORT=3001
PUBLICWEB=./server/www
COSMOSDB_ACCOUNT=your_cosmos_account
COSMOSDB_DB=your_cosmos_db
COSMOSDB_KEY=your_cosmos_key
COSMOSDB_PORT=10255
```

### Quickstart
Expand Down
4 changes: 2 additions & 2 deletions docker-compose.debug.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ services:
vue-heroes:
image: vue-heroes
build: .
environment:
NODE_ENV: development
env_file:
- .env
ports:
- 3001:3001
- 9229:9229
Expand Down
13 changes: 6 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
const express = require('express');
const bodyParser = require('body-parser');

const envFile = './server/env/' + process.env.NODE_ENV;
const env = require(envFile);
const routes = require('./server/routes');

const publicWeb = process.env.PUBLICWEB;
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

app.use(express.static(env.publicWeb));
console.log(`serving ${env.publicWeb}`);
app.use(express.static(publicWeb));
console.log(`serving ${publicWeb}`);
app.use('/api', routes);
app.get('*', (req, res) => {
res.sendFile(`index.html`, { root: env.publicWeb });
res.sendFile(`index.html`, { root: publicWeb });
});

app.listen(env.serverPort, () => console.log(`API running on http://localhost:${env.serverPort}`));
const port = process.env.SERVER_PORT;
app.listen(port, () => console.log(`API running on http://localhost:${port}`));
7 changes: 6 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"name": "vue-heroes",
"version": "1.0.0",
"version": "1.1.0",
"description": "",
"main": "index.js",
"scripts": {
"dev": "cross-env NODE_ENV=development webpack-dev-server --open --hot",
"dev-proxy": "cross-env NODE_ENV=development webpack-dev-server --open --hot",
"build-dev": "webpack --progress --hide-modules",
"build": "cross-env NODE_ENV=production webpack --progress --hide-modules",
"debug": "concurrently \"NODE_ENV=development node --inspect index.js\" \"npm run dev\"",
"start": "node index.js",
"debug": "concurrently \"node -r dotenv/config --inspect index.js\" \"npm run dev-proxy\"",
"start": "concurrently \"node -r dotenv/config index.js\" \"npm run dev-proxy\"",
"docker-up": "docker-compose up -d --build",
"docker-up-debug": "docker-compose -f docker-compose.debug.yml up -d --build"
},
Expand All @@ -18,6 +18,7 @@
"dependencies": {
"axios": "^0.17.0",
"body-parser": "^1.18.2",
"dotenv": "^4.0.0",
"express": "^4.16.2",
"mongoose": "^4.13.0",
"vue": "^2.3.4",
Expand Down
15 changes: 0 additions & 15 deletions server/env/example.js

This file was deleted.

14 changes: 0 additions & 14 deletions server/env/production.js

This file was deleted.

11 changes: 3 additions & 8 deletions server/mongo.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,11 @@ const mongoose = require('mongoose');
*/
mongoose.Promise = global.Promise;

const envFile = './env/' + (process.env.NODE_ENV || 'development');
console.log(`reading env file ${envFile}`);
const env = require(envFile);
console.log(`env file contains cosmos settings = ${!!env.cosmos.accountName}`);

// Cosmos DB Connection String
// eslint-disable-next-line max-len
const mongoUri = `mongodb://${env.cosmos.accountName}:${env.cosmos.key}@${
env.cosmos.accountName
}.documents.azure.com:${env.cosmos.port}/${env.cosmos.databaseName}?ssl=true`;
const mongoUri = `mongodb://${process.env.COSMOSDB_ACCOUNT}:${process.env.COSMOSDB_KEY}@${
process.env.COSMOSDB_ACCOUNT
}.documents.azure.com:${process.env.COSMOSDB_PORT}/${process.env.COSMOSDB_DB}?ssl=true`;
// &replicaSet=globaldb`;

// Local MongoDB Connection String
Expand Down

0 comments on commit 86634db

Please sign in to comment.