Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

feat: add host property to server config #123

Merged
merged 7 commits into from
Oct 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/commands/config/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class ConfigGenerateCommand extends Command {
if (flags.env) {
const generateConfig = {
server: {
host: process.env.LOGCHIMP_SECRET_HOST || '127.0.0.1',
port: _.toNumber(process.env.LOGCHIMP_SERVER_PORT) || 3000,
secretkey: process.env.LOGCHIMP_SECRET_KEY,
},
Expand Down Expand Up @@ -80,6 +81,7 @@ class ConfigGenerateCommand extends Command {
const generateConfig = {
server: {
local: flags.local,
host: flags.host,
port: flags.port,
secretkey: flags.secretkey ? flags.secretkey : generatePassword(),
},
Expand Down Expand Up @@ -128,6 +130,10 @@ ConfigGenerateCommand.flags = {
}),

// server
host: flags.string({
description: 'Server host',
default: '127.0.0.1',
}),
port: flags.integer({
description: 'Server port to listen on',
default: 3000,
Expand Down
86 changes: 67 additions & 19 deletions tests/command/config-generate.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ describe('config:generate command', () => {
beforeEach(async () => {
// fail-safe: delete any existing logchimp config at root directory
const currentDirectory = await process.cwd()
await fs.removeSync(`${currentDirectory}/logchimp.config.json`)
fs.removeSync(`${currentDirectory}/logchimp.config.json`)
fs.removeSync(`${currentDirectory}/.env`)
})

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

// server
expect(config.server.local).toBe(false)
expect(config.server.host).toBe('127.0.0.1')
expect(config.server.port).toBe(3000)

// database
Expand All @@ -41,6 +43,7 @@ describe('config:generate command', () => {
const command = await runCommand([
'config:generate',
'--port=80',
'--host=0.0.0.0',
'--secretkey=mySecretKey',
'--dbhost=postgres-db.logchimp.codecarrot.net',
'--dbuser=pg_db_user',
Expand All @@ -63,6 +66,7 @@ describe('config:generate command', () => {

// server
expect(config.server.local).toBe(false)
expect(config.server.host).toBe('0.0.0.0')
expect(config.server.port).toBe(80)
expect(config.server.secretkey).toBe('mySecretKey')
// database
Expand Down Expand Up @@ -141,6 +145,7 @@ describe('config:generate command', () => {
fs.writeFileSync(
`${currentDirectory}/.env`,
`LOGCHIMP_SERVER_PORT=3000
LOGCHIMP_SECRET_HOST=0.0.0.0
LOGCHIMP_SECRET_KEY=secret-key
LOGCHIMP_DB_HOST=localhost
LOGCHIMP_DB_PORT=5432
Expand All @@ -166,23 +171,66 @@ LOGCHIMP_MAIL_PASSWORD=mail_password`
const isConfigEmpty = _.isEmpty(config)
expect(isConfigEmpty).toBe(false)

// server
expect(config.server.secretkey).toBe('secret-key')
expect(config.server.port).toBe(3000)

// database
expect(config.database.host).toBe('localhost')
expect(config.database.user).toBe('logchimp')
expect(config.database.password).toBe('secret-password')
expect(config.database.name).toBe('logchimpDB')
expect(config.database.port).toBe(5432)
expect(config.database.ssl).toBe(true)

// mail
expect(config.mail.service).toBe('service')
expect(config.mail.host).toBe('mail_host')
expect(config.mail.user).toBe('mail_username')
expect(config.mail.password).toBe('mail_password')
expect(config.mail.port).toBe(587)
// server
expect(config.server.host).toBe('0.0.0.0')
expect(config.server.secretkey).toBe('secret-key')
expect(config.server.port).toBe(3000)

// database
expect(config.database.host).toBe('localhost')
expect(config.database.user).toBe('logchimp')
expect(config.database.password).toBe('secret-password')
expect(config.database.name).toBe('logchimpDB')
expect(config.database.port).toBe(5432)
expect(config.database.ssl).toBe(true)

// mail
expect(config.mail.service).toBe('service')
expect(config.mail.host).toBe('mail_host')
expect(config.mail.user).toBe('mail_username')
expect(config.mail.password).toBe('mail_password')
expect(config.mail.port).toBe(587)
})

it('with --env flag and default values', async () => {
const currentDirectory = await process.cwd()

// create .env file if not already present
const envIsPresent = fs.existsSync(`${currentDirectory}/.env`)

if (!envIsPresent) {
// intentionally creating an empty .env file
fs.writeFileSync(`${currentDirectory}/.env`, '')
}

// generate config file
const command = await runCommand(['config:generate', '--env'])

expect(command.stdout).toContain('LogChimp configuration file succesfully created from environment variables')

// validate configuration file
const config = fs.readJsonSync(`${currentDirectory}/logchimp.config.json`)
const isConfigEmpty = _.isEmpty(config)
expect(isConfigEmpty).toBe(false)

// server
expect(config.server.host).toBe('127.0.0.1')
expect(config.server.secretkey).toBeUndefined()
expect(config.server.port).toBe(3000)

// database
expect(config.database.host).toBeUndefined()
expect(config.database.user).toBeUndefined()
expect(config.database.password).toBeUndefined()
expect(config.database.name).toBeUndefined()
expect(config.database.port).toBe(5432)
expect(config.database.ssl).toBe(true)

// mail
expect(config.mail.service).toBeUndefined()
expect(config.mail.host).toBeUndefined()
expect(config.mail.user).toBeUndefined()
expect(config.mail.password).toBeUndefined()
expect(config.mail.port).toBe(587)
})
})