1.2.2 • Published 6 years ago

@gplatform/settings v1.2.2

Weekly downloads
19
License
MIT
Repository
github
Last release
6 years ago

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

License

MIT

1.2.2

6 years ago

1.2.1

6 years ago

1.2.0

6 years ago

1.1.9

6 years ago

1.1.8

6 years ago

1.1.7

6 years ago

1.1.6

6 years ago

1.1.5

6 years ago

1.1.4

6 years ago

1.1.3

6 years ago

1.1.2

6 years ago

1.1.0

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago

0.0.3

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago