gPlatform Production Settings
Settings - Principles
- Easy to use in Development
- Easy to update in Production
YouTube Tutorial
Features
- Node.JS Configuration
- Command line interface to create settings folder
- Command line interface to build .env for docker deploy
- Atomic Object Merging / Partial Updates
- Environment Variables
- Command Line Arguments
- Config Validation
How can I use this?
# Recommended app folder structure
- package.json # NodeJS package
- src # Source code
- app.js # app entry point
- settings # settings module
- index.js # module config
- settings.json # default values
- schema.js # validation schema
# Install gplatform module
$ yarn add @gplatform/settings
# In ./src | Creates ./settings folder
$ npx @gplatform/settings --init MY_APP_PREFIX
# In ./src | Create .env file for production docker
$ npx @gplatform/settings --env > production.env
Usage | Variants
# Run with defaults
$ node src/app.js
# Run with defaults updating service.port
$ node src/app.js --service-port 9000
# Run with defaults updating service
$ node src/app.js --service-port 9000 --service-host 10.0.0.10
# Run with defaults updating service.host
$ MY_APP_PREFIX_service_host="prod.mywebsite.com" \
node src/app.js
# Run without defaults updating all from env variable
$ MY_APP_PREFIX='{"service":{"host":"local","port":"27017"}' \
node src/app.js
Manual - How can I use this?
$ yarn add @gplatform/settings
// add ./settings/index.js
const { load } = require('@gplatform/settings')
const defaults = require('./settings.json')
const schema = require('./schema.js')
const appName = 'MY_APP_PREFIX'
module.exports = load({
defaults,
schema, // Joi Schema - Optional
commandLineInterface: true, // Experimental
app: process.env[appName],
variables: process.env,
regex: new RegExp('^' + appName + '_')
})
// add ./settings/settings.json
{
"name": "Gary Ascuy Anturiano",
"service": {
"host": "localhost",
"port": "27017"
},
"mongo": {
"uri": "mongodb://localhost:27017/prod"
}
}
// add ./settings/schema.js
const joi = require('joi')
module.exports = {
name: joi.string().min(3).max(30).required(),
service: {
host: joi.string().required(),
port: joi.number().integer().min(0).max(65535)
},
mongo: {
uri: joi.string().required()
}
}
// add ./app.js
const express = require('express')
const { get } = require('./settings')
const { log } = console
const app = express()
const name = get('name')
const mongoUri = get('mongo.uri')
const {host, port} = get('service')
app.get('/', (req, res) => res.send(name))
app.listen(port, host, () => {
log(`Server Created, Ready to listen at ${host}:${port}`)
})
# Run application at port 8000
$ node app.js --service-port 8000
Development - Contribution
To update the code you can run tests in watch mode
$ yarn test -w
After complete you can create a build using
$ yarn build
Example
- In repo at example/main.js you can find a example
$ git clone https://github.com/ziosd/settings.git
$ cd settings
# Basic execution
$ yarn start
# Basic execution
$ yarn start
# Using args as cli
$ yarn start --service-port 5600 --service-host localhost
# Using env variables
$ MY_APP_PREFIX_service_port=8000 \
MY_APP_PREFIX_service_port=gplatform.me \
yarn start
Coming soon
- Docker config generation
- More examples
- Better video tutorial