├── template └── config │ ├── .gitignore │ └── defaults.json ├── index.js ├── .gitignore ├── bin └── init.js ├── package.json ├── LICENSE └── README.md /template/config/.gitignore: -------------------------------------------------------------------------------- 1 | *.json 2 | !defaults.json 3 | -------------------------------------------------------------------------------- /template/config/defaults.json: -------------------------------------------------------------------------------- 1 | { 2 | "port": 3000 3 | } -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | module.exports = require('democracyos-config')({ 2 | path: process.env.CONFIG_PATH 3 | }) 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.log 2 | *.pid 3 | *.seed 4 | *.sublime-workspace 5 | .DS_Store 6 | .node_repl_history 7 | .npm 8 | /build 9 | /node_modules 10 | logs 11 | npm-debug.log* 12 | pids 13 | -------------------------------------------------------------------------------- /bin/init.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | var path = require('path') 4 | var copyDir = require('copy-dir') 5 | 6 | var from = path.join(__dirname, '..', 'template', 'config') 7 | var to = process.env.CONFIG_PATH || path.join(process.cwd(), 'config') 8 | 9 | copyDir(from, to, function (err) { 10 | if (err) { 11 | console.error(err) 12 | return process.exit(1) 13 | } 14 | 15 | console.log('· Config folder generated at: ', to) 16 | process.exit(0) 17 | }) 18 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "dos-config", 3 | "version": "3.0.2", 4 | "description": "Singleton variant of DemocracyOS/config", 5 | "scripts": {}, 6 | "repository": { 7 | "type": "git", 8 | "url": "git+https://github.com/DemocracyOS/dos-config.git" 9 | }, 10 | "license": "MIT", 11 | "bugs": { 12 | "url": "https://github.com/DemocracyOS/dos-config/issues" 13 | }, 14 | "homepage": "https://github.com/DemocracyOS/dos-config#readme", 15 | "bin": { 16 | "dos-config-init": "bin/init.js" 17 | }, 18 | "dependencies": { 19 | "democracyos-config": "1.1.2" 20 | }, 21 | "devDependencies": { 22 | "copy-dir": "0.0.8" 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | The MIT License (MIT) 3 | 4 | Copyright (c) 2015 Democracia en Red 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in all 14 | copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | SOFTWARE. 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # DOS Config 2 | 3 | Library to configure node apps. At its core uses [DemocracyOS/config](https://github.com/DemocracyOS/config) but with sane defaults. 4 | 5 | Keep your default configs on `config/defaults.json`, override them with `config/{NODE_ENV}.json` or with environment variables. 6 | 7 | ## Getting Started 8 | 9 | 1 · Install it: `npm install --save dos-config` 10 | 11 | 2 · Run the command: `./node_modules/.bin/dos-config-init`. It will create the folder `config` with the most basic defaults. 12 | 13 | 3 · Profit 🙌 14 | 15 | ```javascript 16 | var config = require('dos-config') 17 | 18 | if (config.port) { 19 | console.log(`The server should run at port ${config.port}. Go code it now.`) 20 | } 21 | ``` 22 | 23 | ## Configuring your app 24 | 25 | First of all, `dos-config` will look for the `config/defaults.json` file, which will define the configuration structure with the default values. 26 | 27 | Here's a complete example, keep in mind that the keys of your json files should always be `camelCase`: 28 | 29 | ```json 30 | { 31 | "port": 3000, 32 | "mongoUrl": "mongodb://localhost/DemocracyOS-dev", 33 | "staff": [ 34 | "some@example.com", 35 | "another@example.com" 36 | ], 37 | "connectionData": {}, 38 | "auth": { 39 | "user": "Fring", 40 | "password": "always-be-secure-123!$" 41 | } 42 | } 43 | ``` 44 | 45 | * The most primitive values are `Integer` or `String`. Which work as expected. 46 | * `Array` values can only have `String`s inside. 47 | * The empty object `{}` means that it doesn't have a default value, but can be overriden by absolutely any JSON. 48 | 49 | After defining your `defaults`, you can override them using another json file on your current environment. For example, if you are on your development machine, create `config/development.json` and add there only the values you want to change (**always remember to .gitignore this file!**): 50 | 51 | ```json 52 | { 53 | "port": 8888, 54 | "connectionData": { 55 | "domain": "localhost", 56 | "port": 27099, 57 | "user": "root" 58 | } 59 | } 60 | ``` 61 | 62 | Excellent! Now you only need to configure your production server. 63 | 64 | You have two options; first, do the same as development, but create the file `config/production.json` on your server, and make sure the `NODE_ENV` environment variable is set to `production`. 65 | 66 | The other and *recommended* option for production, is to use [environment variables](https://en.wikipedia.org/wiki/Environment_variable). Here's an example of all the variables you should set to override the previous example: 67 | 68 | ``` 69 | PORT=8080 70 | MONGO_URL='mongodb://user:pass@mongoserver/DemocracyOS-production' 71 | STAFF=some@example.com,another@example.com 72 | CONNECTION_DATA='{"domain": "127.123.123.123", "port": 3412}' 73 | AUTH_USER='Admin' 74 | AUTH_PASSWORD='some-production-password' 75 | ``` 76 | 77 | * All the keys are transformed from `camelCase` to `CONSTANT_CASE`. 78 | * Nested values, just add a `_`, for example from `auth.user` as `AUTH_USER`. 79 | * `Array`s should be divided by commas `,` 80 | * And JSON values are a JSON string. 81 | 82 | ## DOS Config Options 83 | 84 | You can only configure the default location of your `config` folder setting the environment variable `CONFIG_PATH`, e.g: `CONFIG_PATH=/usr/src/config`. On that folder it will look for the `defaults.json` and optionally for the `{NODE_ENV}.json`. 85 | 86 | If you want something more flexible, just use https://github.com/DemocracyOS/config 87 | 88 | ## License 89 | * MIT 90 | * © 2017 [Demoracia en Red](http://democraciaenred.org) 91 | * More details under [LICENSE](https://github.com/DemocracyOS/config/blob/master/LICENSE) 92 | --------------------------------------------------------------------------------