Skip to content

Commit

Permalink
Merge pull request #130 from Magellol/enhancement/introduce-dotenv
Browse files Browse the repository at this point in the history
Proposal for allowing users to set their own env vars directly in the project.
  • Loading branch information
Pierre-Gilles authored Dec 15, 2016
2 parents 3272562 + bdb07a4 commit b639b47
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 34 deletions.
12 changes: 12 additions & 0 deletions .env-sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
MYSQL_HOST=
MYSQL_PORT=
MYSQL_USER=
MYSQL_PASSWORD=
MYSQL_DATABASE=

# Only required if you wish to run the test suites.
MYSQL_HOST_TEST=
MYSQL_PORT_TEST=
MYSQL_USER_TEST=
MYSQL_PASSWORD_TEST=
MYSQL_DATABASE_TEST=
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,5 @@ nbproject
.node_history
.editorconfig
typings
jsconfig.json
jsconfig.json
.env
3 changes: 2 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,5 @@ nbproject
.node_history
.editorconfig
typings
jsconfig.json
jsconfig.json
.env
31 changes: 6 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,35 +64,16 @@ For example on Raspbian or on a Mac, it's located in :

#### Connect Gladys to MySQL

To connect Gladys to your database, you need to modify environment variables.
To connect Gladys to your database, you will need to set some environment variables.
To do so, you'll have to create a `.env` file at the root of this project. The content of this file can be found by looking at `.env-sample`, which lists all the customizable env vars.

You can set `MYSQL_HOST`, `MYSQL_PORT`, `MYSQL_USER`, `MYSQL_PASSWORD` and `MYSQL_DATABASE`.



**Dirty way :**

If you are not able to modify environment variables, you can enter your connections informations in the `config/connections.js` file.

Modify the following lines with your own informations :
**Note :** You will need to create the database as well in MySQL:

```sql
CREATE DATABASE gladys; -- or whatever name you've set in your .env file.`
```
sailsmysql: {
adapter: 'sails-mysql',
host: process.env.MYSQL_HOST || 'localhost',
port: process.env.MYSQL_PORT || 3306,
user: process.env.MYSQL_USER || 'root',
password: process.env.MYSQL_PASSWORD || 'root',
database: process.env.MYSQL_DATABASE || 'gladys'
},
```

**Note :** You need to create the database first in MySQL :

`CREATE DATABASE gladys;`


#### Compile assets
#### Compile assets

If you want to recompile assets and run all tasks, you can run :

Expand Down
5 changes: 4 additions & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,12 @@

// if no env variable is set, we are in production
if(!process.env.NODE_ENV){
process.env.NODE_ENV = 'production';
process.env.NODE_ENV = 'production';
}

if (process.env.NODE_ENV === 'development') {
require('dotenv').config();
}

// Ensure we're in the project directory, so relative paths work as expected
// no matter where we actually lift from.
Expand Down
12 changes: 6 additions & 6 deletions config/connections.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ module.exports.connections = {

test: {
adapter: 'sails-mysql',
host: 'localhost',
port: 8889,
user: 'root',
password: 'root',
database: 'gladystest'
host: process.env.MYSQL_HOST_TEST || 'localhost',
port: process.env.MYSQL_PORT_TEST || 8889,
user: process.env.MYSQL_USER_TEST || 'root',
password: (typeof process.env.MYSQL_PASSWORD_TEST !== 'undefined') ? process.env.MYSQL_PASSWORD_TEST : 'root',
database: process.env.MYSQL_DATABASE_TEST || 'gladystest'
},

/***************************************************************************
Expand All @@ -54,7 +54,7 @@ module.exports.connections = {
host: process.env.MYSQL_HOST || 'localhost',
port: process.env.MYSQL_PORT || 3306,
user: process.env.MYSQL_USER || 'root',
password: process.env.MYSQL_PASSWORD || 'root',
password: (typeof process.env.MYSQL_PASSWORD !== 'undefined') ? process.env.MYSQL_PASSWORD : 'root',
database: process.env.MYSQL_DATABASE || 'gladys'
},

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"bluebird": "^3.3.4",
"clone": "^2.0.0",
"crypto": "~0.0.3",
"dotenv": "^2.0.0",
"ejs": "^2.4.1",
"es6-template-strings": "^2.0.0",
"fs-extra": "^0.30.0",
Expand Down
4 changes: 4 additions & 0 deletions test/bootstrap.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ var Sails = require('sails'),
var Barrels = require('barrels');
var Promise = require('bluebird');

if (process.env.NODE_ENV === 'development') {
require('dotenv').config();
}

/*
log: {
level: 'error'
Expand Down

0 comments on commit b639b47

Please sign in to comment.