Skip to content

Commit

Permalink
Merge pull request #49 from thevahidal/bug/fails-with-missing-port
Browse files Browse the repository at this point in the history
overriding env variables with CLI args. adding shebang to server.js
  • Loading branch information
thevahidal authored Oct 30, 2022
2 parents d9ecac7 + 8f12558 commit beda9f5
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 6 deletions.
2 changes: 1 addition & 1 deletion core/.env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ NODE_ENV=development

CORS_ORIGIN_WHITELIST=http://localhost:3000,http://127.0.0.1:3000

RATE_LIMIT_ENABLED=True
RATE_LIMIT_ENABLED=False
RATE_LIMIT_WINDOW_MS=1000
RATE_LIMIT_MAX_REQUESTS=10

2 changes: 1 addition & 1 deletion core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "soul-cli",
"version": "0.0.1",
"version": "0.0.3",
"description": "A SQLite RESTful server",
"main": "src/server.js",
"bin": {
Expand Down
16 changes: 12 additions & 4 deletions core/src/config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const path = require('path');

const { yargs, usage, options } = require('../cli');

const { argv } = yargs;

dotenv.config({ path: path.join(__dirname, '../../.env') });

const envVarsSchema = Joi.object()
Expand All @@ -19,22 +21,28 @@ const envVarsSchema = Joi.object()

CORS_ORIGIN_WHITELIST: Joi.string().required(),

RATE_LIMIT_ENABLED: Joi.boolean().required(),
RATE_LIMIT_ENABLED: Joi.boolean().default(false),
RATE_LIMIT_WINDOW_MS: Joi.number().positive().required(),
RATE_LIMIT_MAX_REQUESTS: Joi.number().positive().required(),
})
.unknown();

const { value: envVars, error } = envVarsSchema
.prefs({ errors: { label: 'key' } })
.validate(process.env);
.validate({
...process.env,

// Allow overriding of config with CLI args
PORT: argv.port,
VERBOSE: argv['verbose'],
DB: argv.database,
RATE_LIMIT_ENABLED: argv['rate-limit-enabled'],
});

if (error) {
throw new Error(`Config validation error: ${error.message}`);
}

const { argv } = yargs;

module.exports = {
env: envVars.NODE_ENV,

Expand Down
2 changes: 2 additions & 0 deletions core/src/server.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/usr/bin/env node

const app = require('./index');
const config = require('./config/index');

Expand Down

0 comments on commit beda9f5

Please sign in to comment.