Skip to content

Commit 3e968fa

Browse files
authored
feat: add host property to server config (#123)
* add host as flag and environment variable "LOGCHIMP_SECRET_HOST" * add host property in config-generate.spec.js * set LOGCHIMP_SECRET_HOST default value to "127.0.0.1" * format the code * test: add "with --env flag and default values" * test: remove .env file in beforeEach hook * change toBe('') to toBeUndefined() for "with --env flag and default values" test
1 parent 1735bc4 commit 3e968fa

File tree

2 files changed

+73
-19
lines changed

2 files changed

+73
-19
lines changed

src/commands/config/generate.js

+6
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ class ConfigGenerateCommand extends Command {
5151
if (flags.env) {
5252
const generateConfig = {
5353
server: {
54+
host: process.env.LOGCHIMP_SECRET_HOST || '127.0.0.1',
5455
port: _.toNumber(process.env.LOGCHIMP_SERVER_PORT) || 3000,
5556
secretkey: process.env.LOGCHIMP_SECRET_KEY,
5657
},
@@ -80,6 +81,7 @@ class ConfigGenerateCommand extends Command {
8081
const generateConfig = {
8182
server: {
8283
local: flags.local,
84+
host: flags.host,
8385
port: flags.port,
8486
secretkey: flags.secretkey ? flags.secretkey : generatePassword(),
8587
},
@@ -128,6 +130,10 @@ ConfigGenerateCommand.flags = {
128130
}),
129131

130132
// server
133+
host: flags.string({
134+
description: 'Server host',
135+
default: '127.0.0.1',
136+
}),
131137
port: flags.integer({
132138
description: 'Server port to listen on',
133139
default: 3000,

tests/command/config-generate.spec.js

+67-19
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ describe('config:generate command', () => {
1010
beforeEach(async () => {
1111
// fail-safe: delete any existing logchimp config at root directory
1212
const currentDirectory = await process.cwd()
13-
await fs.removeSync(`${currentDirectory}/logchimp.config.json`)
13+
fs.removeSync(`${currentDirectory}/logchimp.config.json`)
14+
fs.removeSync(`${currentDirectory}/.env`)
1415
})
1516

1617
describe('generate config', () => {
@@ -27,6 +28,7 @@ describe('config:generate command', () => {
2728

2829
// server
2930
expect(config.server.local).toBe(false)
31+
expect(config.server.host).toBe('127.0.0.1')
3032
expect(config.server.port).toBe(3000)
3133

3234
// database
@@ -41,6 +43,7 @@ describe('config:generate command', () => {
4143
const command = await runCommand([
4244
'config:generate',
4345
'--port=80',
46+
'--host=0.0.0.0',
4447
'--secretkey=mySecretKey',
4548
'--dbhost=postgres-db.logchimp.codecarrot.net',
4649
'--dbuser=pg_db_user',
@@ -63,6 +66,7 @@ describe('config:generate command', () => {
6366

6467
// server
6568
expect(config.server.local).toBe(false)
69+
expect(config.server.host).toBe('0.0.0.0')
6670
expect(config.server.port).toBe(80)
6771
expect(config.server.secretkey).toBe('mySecretKey')
6872
// database
@@ -141,6 +145,7 @@ describe('config:generate command', () => {
141145
fs.writeFileSync(
142146
`${currentDirectory}/.env`,
143147
`LOGCHIMP_SERVER_PORT=3000
148+
LOGCHIMP_SECRET_HOST=0.0.0.0
144149
LOGCHIMP_SECRET_KEY=secret-key
145150
LOGCHIMP_DB_HOST=localhost
146151
LOGCHIMP_DB_PORT=5432
@@ -166,23 +171,66 @@ LOGCHIMP_MAIL_PASSWORD=mail_password`
166171
const isConfigEmpty = _.isEmpty(config)
167172
expect(isConfigEmpty).toBe(false)
168173

169-
// server
170-
expect(config.server.secretkey).toBe('secret-key')
171-
expect(config.server.port).toBe(3000)
172-
173-
// database
174-
expect(config.database.host).toBe('localhost')
175-
expect(config.database.user).toBe('logchimp')
176-
expect(config.database.password).toBe('secret-password')
177-
expect(config.database.name).toBe('logchimpDB')
178-
expect(config.database.port).toBe(5432)
179-
expect(config.database.ssl).toBe(true)
180-
181-
// mail
182-
expect(config.mail.service).toBe('service')
183-
expect(config.mail.host).toBe('mail_host')
184-
expect(config.mail.user).toBe('mail_username')
185-
expect(config.mail.password).toBe('mail_password')
186-
expect(config.mail.port).toBe(587)
174+
// server
175+
expect(config.server.host).toBe('0.0.0.0')
176+
expect(config.server.secretkey).toBe('secret-key')
177+
expect(config.server.port).toBe(3000)
178+
179+
// database
180+
expect(config.database.host).toBe('localhost')
181+
expect(config.database.user).toBe('logchimp')
182+
expect(config.database.password).toBe('secret-password')
183+
expect(config.database.name).toBe('logchimpDB')
184+
expect(config.database.port).toBe(5432)
185+
expect(config.database.ssl).toBe(true)
186+
187+
// mail
188+
expect(config.mail.service).toBe('service')
189+
expect(config.mail.host).toBe('mail_host')
190+
expect(config.mail.user).toBe('mail_username')
191+
expect(config.mail.password).toBe('mail_password')
192+
expect(config.mail.port).toBe(587)
193+
})
194+
195+
it('with --env flag and default values', async () => {
196+
const currentDirectory = await process.cwd()
197+
198+
// create .env file if not already present
199+
const envIsPresent = fs.existsSync(`${currentDirectory}/.env`)
200+
201+
if (!envIsPresent) {
202+
// intentionally creating an empty .env file
203+
fs.writeFileSync(`${currentDirectory}/.env`, '')
204+
}
205+
206+
// generate config file
207+
const command = await runCommand(['config:generate', '--env'])
208+
209+
expect(command.stdout).toContain('LogChimp configuration file succesfully created from environment variables')
210+
211+
// validate configuration file
212+
const config = fs.readJsonSync(`${currentDirectory}/logchimp.config.json`)
213+
const isConfigEmpty = _.isEmpty(config)
214+
expect(isConfigEmpty).toBe(false)
215+
216+
// server
217+
expect(config.server.host).toBe('127.0.0.1')
218+
expect(config.server.secretkey).toBeUndefined()
219+
expect(config.server.port).toBe(3000)
220+
221+
// database
222+
expect(config.database.host).toBeUndefined()
223+
expect(config.database.user).toBeUndefined()
224+
expect(config.database.password).toBeUndefined()
225+
expect(config.database.name).toBeUndefined()
226+
expect(config.database.port).toBe(5432)
227+
expect(config.database.ssl).toBe(true)
228+
229+
// mail
230+
expect(config.mail.service).toBeUndefined()
231+
expect(config.mail.host).toBeUndefined()
232+
expect(config.mail.user).toBeUndefined()
233+
expect(config.mail.password).toBeUndefined()
234+
expect(config.mail.port).toBe(587)
187235
})
188236
})

0 commit comments

Comments
 (0)