Skip to content

Commit

Permalink
feat(scripts): use "npm start" instead of "npm run run" to start the …
Browse files Browse the repository at this point in the history
…server (#245)

* feat(scripts): use "npm start" instead of "npm run run" to start the server

* actually rename the npm script

* remove run-dev npm script + mentions to "--dev" flag

* display colors in stdout and stderr by default => add --no-color flag

* fix dockerfile to run npm start

* remove run-for-tests npm script

* remove "run-dev-local" npm script => document corresponding use case

* remove "run-dev-digest" npm script => document use case

* remove run-dev-search npm script
  • Loading branch information
adrienjoly committed Oct 27, 2019
1 parent ba8b1b1 commit 1540581
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 31 deletions.
3 changes: 1 addition & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
command: source env-vars-testing.sh && npm run test-reset
- run:
name: Run Openwhyd server
command: source env-vars-testing.sh && npm run run --mongoDbDatabase openwhyd_test
command: source env-vars-testing.sh && npm start --mongoDbDatabase openwhyd_test
background: true
- run:
name: Wait for server up
Expand All @@ -45,4 +45,3 @@ jobs:
# - run:
# name: Semantic Release
# command: npm run semantic-release

2 changes: 1 addition & 1 deletion .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jobs:
- name: Start Openwhyd server
run: |
source env-vars-testing.sh
npm run run --mongoDbDatabase openwhyd_test &
npm start --mongoDbDatabase openwhyd_test &
./scripts/wait-for-http-server.sh 8080 # give openwhyd's server some time to start
- name: npm test
env:
Expand Down
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ before_script:
- chmod +x ./cc-test-reporter
- ./cc-test-reporter before-build
- npm run test-reset # will initialize the openwhyd_test db
- npm run run --mongoDbDatabase openwhyd_test &
- npm start --mongoDbDatabase openwhyd_test &
- ./scripts/wait-for-http-server.sh 8080 # give openwhyd's server some time to start

script:
Expand Down
34 changes: 30 additions & 4 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,15 @@
"envFile": "${workspaceRoot}/env-vars-testing.conf",
"program": "${workspaceRoot}/app.js",
"runtimeVersion": "10.16.3", // to keep in sync with .nvmrc
"args": [ "--color", "--fakeEmail", "--digestInterval", "-1", "--mongoDbDatabase", "openwhyd_test", "--mongoDbPort", "27117" ]
"args": [
"--fakeEmail",
"--digestInterval",
"-1",
"--mongoDbDatabase",
"openwhyd_test",
"--mongoDbPort",
"27117"
]
// Notice: make sure that the mongodb server is listening on port 27117
},
{
Expand All @@ -26,7 +34,13 @@
"cwd": "${workspaceRoot}",
"envFile": "${workspaceRoot}/env-vars-testing.conf",
"program": "${workspaceRoot}/app.js",
"args": [ "--color", "--fakeEmail", "--digestInterval", "-1", "--mongoDbDatabase", "openwhyd_test" ]
"args": [
"--fakeEmail",
"--digestInterval",
"-1",
"--mongoDbDatabase",
"openwhyd_test"
]
// Notice: make sure that the mongodb server is listening on port 27017
},
{
Expand All @@ -39,7 +53,13 @@
"cwd": "${workspaceRoot}",
"envFile": "${workspaceRoot}/env-vars-testing.conf",
"program": "${workspaceRoot}/app.js",
"args": [ "--color", "--fakeEmail", "--digestInterval", "-1", "--mongoDbDatabase", "openwhyd_test" ]
"args": [
"--fakeEmail",
"--digestInterval",
"-1",
"--mongoDbDatabase",
"openwhyd_test"
]
// Notice: make sure that the mongodb server is listening on port 27017
},
{
Expand All @@ -52,7 +72,13 @@
"cwd": "${workspaceRoot}",
"envFile": "${workspaceRoot}/env-vars-testing.conf",
"program": "${workspaceRoot}/app.js",
"args": [ "--color", "--fakeEmail", "--digestInterval", "-1", "--mongoDbDatabase", "openwhyd_test" ]
"args": [
"--fakeEmail",
"--digestInterval",
"-1",
"--mongoDbDatabase",
"openwhyd_test"
]
// Notice: make sure that the mongodb server is listening on port 27017
},
{
Expand Down
4 changes: 2 additions & 2 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"focus": true,
"panel": "new"
},
"command": "source env-vars-testing.sh; npm run test-reset; npm run run --mongoDbDatabase openwhyd_test & sleep 2; npm test"
"command": "source env-vars-testing.sh; npm run test-reset; npm start --mongoDbDatabase openwhyd_test & sleep 2; npm test"
},
{
"taskName": "run-api-tests",
Expand All @@ -25,7 +25,7 @@
"focus": true,
"panel": "new"
},
"command": "source env-vars-testing.sh; npm run test-reset; npm run run --mongoDbDatabase openwhyd_test & sleep 2; node_modules/.bin/mocha test/api/*.js"
"command": "source env-vars-testing.sh; npm run test-reset; npm start --mongoDbDatabase openwhyd_test & sleep 2; node_modules/.bin/mocha test/api/*.js"
}
]
}
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ COPY ./ /usr/src/app

EXPOSE 8080

CMD [ "npm", "run", "run" ]
CMD [ "npm", "start" ]
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ fetch-deps: ## Fetch JS dependencies.
@npm install

dev: fetch-deps ## Start a local dev server.
@npm run run-dev
@npm start

restart: ## Restart the production server without downtime.
@cd scripts && ./restart.sh
Expand Down
29 changes: 18 additions & 11 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ var params = (process.appParams = {
mongoDbAuthUser: process.env['MONGODB_USER'],
mongoDbAuthPassword: process.env['MONGODB_PASS'],
mongoDbDatabase: process.env['MONGODB_DATABASE'], // || "openwhyd_data",
color: true,

// secrets
genuineSignupSecret: process.env.WHYD_GENUINE_SIGNUP_SECRET.substr(),
Expand Down Expand Up @@ -109,16 +110,8 @@ var params = (process.appParams = {
});

var FLAGS = {
'--color': function() {
console.warn = makeErrorLog(
makeColorConsole(consoleError, 'yellow'),
'Warning'
);
console.error = makeErrorLog(
makeColorConsole(consoleError, 'red'),
'Error'
);
process.appParams.color = true;
'--no-color': function() {
process.appParams.color = false;
},
'--fakeEmail': function() {
params.emailModule = '';
Expand Down Expand Up @@ -187,7 +180,8 @@ function start() {
// startup

function init() {
if (process.argv.length > 2)
// apply command-line arguments
if (process.argv.length > 2) {
// ignore "node" and the filepath of this script
for (var i = 2; i < process.argv.length; ++i) {
var flag = process.argv[i];
Expand All @@ -196,6 +190,19 @@ function init() {
else if (flag.indexOf('--') == 0)
params[flag.substr(2)] = process.argv[++i];
}
}
if (params.color == true) {
console.warn = makeErrorLog(
makeColorConsole(consoleError, 'yellow'),
'Warning'
);
console.error = makeErrorLog(
makeColorConsole(consoleError, 'red'),
'Error'
);
} else {
process.appParams.color = false;
}
console.log('Starting web server with params:', params);
require('./app/models/mongodb.js').init(function(err, db) {
if (err) throw err;
Expand Down
30 changes: 28 additions & 2 deletions docs/INSTALL.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ If you don't want to use Docker (or can't), you can follow these instructions.

### Usage (advanced)

- Run `npm run run`, or `npm forever:start` (auto-restart daemon)
- Run `npm start`, or `npm forever:start` (auto-restart daemon)
- Open [http://localhost:8080](http://localhost:8080) (or `WHYD_URL_PREFIX`)
- During development, you may have to restart the server to have your changes taken into account.

Expand All @@ -98,7 +98,7 @@ Run all tests, including acceptance tests (webdriver.io-based):

```sh
# in a terminal session, start Openwhyd's application server
$ npm run run-for-tests
$ . ./env-vars-testing.sh && npm start
# in another terminal session, run the tests
$ npm test
```
Expand All @@ -107,6 +107,32 @@ $ npm test

## Configuration (advanced)

### Command-line arguments

Openwhyd's entry point (`app.js`) accepts two kinds of command-line arguments:

- toggles: `--no-color`, `--fakeEmail` and `--emailAdminsOnly`; (see `FLAGS` from `app.js` for an up-to-date list)
- overrides: any app-level configuration parameter can be set, e.g. `urlPrefix` can be set as `--urlPrefix <value>`. (see `process.appParams` from `app.js` for an up-to-date list)

### Advanced use cases

#### Test email digests

If you want to test email digests locally:

```sh
$ node app.js --emailAdminsOnly --digestInterval 5000 --digestImmediate true
```

#### Map localhost to a domain name

If you want to test Deezer Connect, you will need your server to be reachable through a domain name. Here's a way to achieve that:

1. Configure your Deezer app to allow connections from `http://local.openwhyd.org:8080`;
2. Add `local.openwhyd.org` to your `/private/etc/hosts` file;
3. Flush the corresponding cache: `$ dscacheutil -flushcache`;
4. Start Openwhyd with `$ npm start -- --urlPrefix http://local.openwhyd.org:8080`.

### Environment variables

- `WHYD_GENUINE_SIGNUP_SECRET` (mandatory. a secret key that is used to make sure that sign-ups are legit)
Expand Down
7 changes: 1 addition & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,7 @@
"forever:stop": "forever stop app.js",
"forever:start": "mv appd.out prev.out; mv appd.err prev.err; forever --outFile appd.out --errFile appd.err --minUptime 1000 --spinSleepTime 500 --number 100000 start app.js $@",
"forever:restart": "npm run forever:stop; npm run forever:start $@;",
"run": "node app.js --color --fakeEmail --digestInterval -1 $@",
"run-for-tests": ". ./env-vars-testing.sh && npm run run",
"run-dev": "node app.js --color --fakeEmail --digestInterval -1 --dev $@",
"run-dev-local": "echo \"to test deezer connect, add local.openwhyd.org to /private/etc/hosts, then dscacheutil -flushcache\"; npm run run-dev -- --urlPrefix http://local.openwhyd.org:8080 $@",
"run-dev-digest": "node app.js --dev --color --emailAdminsOnly --digestInterval 5000 --digestImmediate true $@",
"run-dev-search": "node app.js --dev --color --fakeEmail --digestInterval -1 --enableSearchIndex true $@",
"start": "node app.js --fakeEmail --digestInterval -1 $@",
"test-reset": "node test/reset-test-db.js",
"test-api": "npm run test-reset && node_modules/.bin/mocha test/api/*.js --exit",
"test-unit": "node_modules/.bin/mocha test/unit/*.js --exit",
Expand Down

0 comments on commit 1540581

Please sign in to comment.