Permalink
Browse files

Update config to use convict, start docs.

  • Loading branch information...
davidkaneda committed Nov 8, 2014
1 parent 45492bf commit e1e905a4fc3d6995d06146cae32badaffc6b6fda
View
@@ -12,7 +12,7 @@ A fast, simple way to build dynamic websites with [Node.js](http://nodejs.org).
Buckets requires [Node.js](http://nodejs.org) and [MongoDB](http://www.mongodb.org). Once you have those, the easiest way to use Buckets is to install it as an NPM module:
```bash
npm install buckets@latest
npm install buckets
```
You can also run the above command with `--save` to add Buckets to your app’s package.json (recommended). From here, you can run Buckets in a script like this:
@@ -21,6 +21,8 @@ You can also run the above command with `--save` to add Buckets to your app’s
buckets = require('buckets');
buckets({
buildsPath: __dirname + "/builds/",
// Add your Gmail to use features that send email (Mandrill and others also supported)
smtp:
// Additional configuration…
});
```
@@ -60,10 +62,12 @@ If you're deploying directly from this repo, you'll need to make one change to y
### Developer Documentation
* [Config](docs/config.md)
* [Frontend Architecture](docs/frontend.md)
* [Database](docs/database.md)
* [Tests](docs/tests.md)
* [Plugins](docs/plugins.md)
* [REST API Docs](docs/api/)
### User Documentation
@@ -29,8 +29,6 @@
{{/hasRole}}
{{/is}}
{{checkbox 'previewMode' previewMode label="View site in staging mode" help="View upcoming design changes, if any exist."}}
{{#if dropboxEnabled}}
<h5>Dropbox</h5>
View
@@ -0,0 +1,6 @@
{
// Buckets’ local testing environment
"db": "mongodb://localhost/buckets_test",
"buildsPath": "./test/builds/",
"logLevel": "none"
}
View
@@ -0,0 +1,63 @@
# Buckets Configuration
Buckets provides a few ways to set configuration options when starting up. You can provide these settings directly to the `buckets` function, or you can set ENV variables. Buckets will also look for two JSON files at the root of your project, both a `config.json` and an `_environment_.json` (where environment is whatever environment is currently active).
Here are the settings:
## Guide
### name `process.env.GLOBAL_NAME` _default_
## Settings
### autoStart _true_
Set to false to include Buckets as Middleware.
### adminSegment _'admin'_
The URL segment at which the server will serve the Buckets admin interface.
### apiSegment _'api'_
The URL segment at which the server will serve the Buckets [API](../api/).
### cloudinary `CLOUDINARY_URL`
The connection URL for [Cloudinary](http://cloudinary.com/). Set up for default Heroku installation.
### db `MONGOHQ_URL` _'mongodb://localhost/buckets_#{env}'_
A MongoDB connection string. The env variable is set up for a default Heroku installation.
### fastlyApiKey `FASTLY_API_KEY`
For Fastly CDN support.
### fastlyServiceId `FASTLY_SERVICE_ID`
For Fastly CDN support.
### fastlyCdnUrl `FASTLY_CDN_URL`
For Fastly CDN support.
### port `PORT` _3000_
Port at which Buckets serves the website.
### logLevel _'info'_
The level at which the logger reports. Possible values are "none", "debug", "verbose", "info", "warn", and "error".
### pluginsPath _'./node_modules/'_
Path to where plugins are loaded from. All plugins are node modules with a naming convention of "buckets-_name_".
#### smtp.service _'GMAIL'_
#### smtp.auth.user `MANDRILL_USERNAME`
#### smtp.auth.pass `MANDRILL_APIKEY`
View
@@ -67,7 +67,6 @@
"cors": "^2.4.1",
"datejs": "^1.0.0-beta",
"dbox": "^0.6.3",
"dotenv": "^0.4.0",
"express": "^4.2.0",
"express-winston": "^0.2.8",
"fastly": "^1.2.0",
View
@@ -1,43 +0,0 @@
env = process.env.NODE_ENV || 'development'
_ = require 'underscore'
config =
default:
adminSegment: 'admin'
apiSegment: 'api'
salt: 'BUCKETS4LIFE!!1'
port: process.env.PORT || 3000
env: env
buildsPath: "#{__dirname}/../builds/"
pluginsPath: "#{__dirname}/../node_modules/"
catchAll: yes
autoStart: yes
cloudinary: process.env.CLOUDINARY_URL
db: "mongodb://localhost/buckets_development"
logLevel: 'info'
test:
db: "mongodb://localhost/buckets_#{env}"
buildsPath: "#{__dirname}/../test/builds/"
logLevel: 'error'
production:
smtp:
service: 'Mandrill'
auth:
user: process.env.MANDRILL_USERNAME
pass: process.env.MANDRILL_APIKEY
db: process.env.MONGOHQ_URL
fastly:
api_key: process.env.FASTLY_API_KEY
cdn_url: process.env.FASTLY_CDN_URL
service_id: process.env.FASTLY_SERVICE_ID
development:
smtp:
service: 'Gmail'
auth:
user: 'your.email@gmail.com'
pass: 'PASSWORD'
module.exports = if config[env]?
_.defaults(config[env], config.default)
else
config.default
View
@@ -1,6 +1,3 @@
dotenv = require 'dotenv'
dotenv.load()
async = require 'async'
_ = require 'underscore'
@@ -18,17 +15,16 @@ events = require 'events'
Build = require './models/build'
logger = require './lib/logger'
config = require './lib/config'
class Buckets extends events.EventEmitter
listening: no
constructor: (config={}, callback) ->
@config = require './config'
@config = _.extend @config, config
constructor: (options={}, callback) ->
@config = config.load options
logger.verbose 'Starting Buckets', config: config.toString(), callback: callback?
logger.verbose 'Starting Buckets', config: @config, callback: callback?
callback ?= ->
@generateBuilds callback
@generateBuilds callback || ->
# Turn on NewRelic
try
@@ -43,9 +39,9 @@ class Buckets extends events.EventEmitter
logger.error 'There was an error loading NewRelic', e
# Purge Fastly on prod pushes
if @config.fastly?.api_key and @config.fastly?.service_id and @config.env is 'production'
fastly = require('fastly')(@config.fastly.api_key)
fastly.purgeAll @config.fastly.service_id, -> logger.error 'Purged Fastly Cache'.red
if config.get('fastlyApiKey')?.api_key and config.get('fastlyServiceId')?.service_id and config.get('env') is 'production'
@fastly = require('fastly') config.get 'fastlyApiKey'
@fastly.purgeAll config.get('fastlyServiceId'), -> logger.info 'Purged Fastly Cache'
passport = require './lib/auth'
@@ -62,20 +58,19 @@ class Buckets extends events.EventEmitter
# Handle cookies and sessions and stuff
@app.use compression level: 4
@app.use responseTime() if @config.env isnt 'production'
@app.use cookieParser @config.salt
@app.use responseTime() if config.get('env') isnt 'production'
@app.use cookieParser config.get('salt')
@app.use session
secret: @config.salt
secret: config.get('salt')
name: 'buckets'
# domain: ".#{@config.host}" if @config.host
@app.use bodyParser.json()
@app.use bodyParser.urlencoded extended: true
@app.use passport.initialize()
@app.use passport.session()
@app.set 'view engine', 'hbs'
@app.set 'view cache', no
@app.set 'view cache', false
@app.use expressWinston.logger
winstonInstance: logger
@@ -94,20 +89,20 @@ class Buckets extends events.EventEmitter
# statusLevels: yes
# Load Routes for the API, admin, and frontend
@app.use "/#{@config.apiSegment}", @routers.api
@app.use "/#{@config.adminSegment}", @routers.admin
@app.use "/#{config.get('apiSegment')}", @routers.api
@app.use "/#{config.get('adminSegment')}", @routers.admin
@app.use @routers.frontend
@app.use expressWinston.errorLogger
winstonInstance: logger
@start() if @config.autoStart
@start() if config.get('autoStart')
start: (done) ->
done?() if @server
@server = @app.listen @config.port, =>
@server = @app.listen config.get('port'), =>
@listening = yes
logger.info ("Buckets is running at " + "http://localhost:#{@config.port}/".underline.bold).yellow
logger.info ("Buckets is running at " + "http://localhost:#{config.get('port')}/".underline.bold).yellow
stop: (done) ->
return done?() unless @listening or not @server
@@ -116,7 +111,7 @@ class Buckets extends events.EventEmitter
done()
generateBuilds: (callback) ->
path = @config.buildsPath
path = config.get('buildsPath')
logger.profile 'Generated builds'
async.parallel [
@@ -140,19 +135,18 @@ class Buckets extends events.EventEmitter
# There can be only one #highlander
buckets = null
module.exports = (config={}, callback) ->
if _.isFunction(config) and !callback
callback = config
config = {}
module.exports = (options={}, callback) ->
if _.isFunction(options) and !callback
callback = options
options = {}
if buckets?
buckets.config = config
buckets.config.load options
if callback
if buckets.generated
callback()
else
buckets.once 'buildsGenerated', callback
buckets
else
buckets = new Buckets config, callback
buckets = new Buckets options, callback
View
@@ -0,0 +1,90 @@
convict = require 'convict'
fs = require 'fs-extra'
logger = require './logger'
config = convict
autoStart:
doc: 'Should the app start listenening? (Pass no to use as middleware)'
format: Boolean
default: yes
adminSegment:
doc: 'The URL segment which serves the Buckets admin.'
format: String
default: 'admin'
apiSegment:
doc: 'The URL segment which serves the API.'
default: 'api'
buildsPath:
doc: 'Path to local development theme.'
format: String
default: './builds/'
cloudinary:
doc: 'Cloudinary API URL for image uploads.'
env: 'CLOUDINARY_URL'
default: ''
format: String
db:
doc: 'A MongoDB connection string.'
format: String
default: "mongodb://localhost/buckets_development"
env: 'MONGOHQ_URL'
env:
doc: 'The app environment.'
format: ['production', 'development', 'test']
default: 'development'
env: 'NODE_ENV'
fastlyApiKey:
env: 'FASTLY_API_KEY'
default: false
fastlyServiceId:
env: 'FASTLY_SERVICE_ID'
default: false
fastlyCdnUrl:
env: 'FASTLY_CDN_URL'
default: false
logLevel:
doc: 'What level for the logs.'
format: ['none', 'debug', 'verbose', 'info', 'warn', 'error']
default: 'info'
pluginsPath:
doc: 'Path to where plugins are loaded from.'
default: "#{__dirname}/../../node_modules/"
port:
doc: 'The port to bind to.'
format: 'port'
default: 3000
env: 'PORT'
salt:
doc: 'A private salt used to obscure tokens and passwords.'
default: 'BUCKETS4LIFE!!1'
format: String
env: 'BUCKETS_SALT'
smtp:
service:
doc: 'Nodemailer service type for sending email.'
format: String
default: ''
auth:
user:
doc: 'SMTP Username'
format: String
env: 'MANDRILL_USERNAME'
default: ''
pass:
doc: 'SMTP Password'
format: String
env: 'MANDRILL_APIKEY'
default: ''
if fs.existsSync "./config/config.json"
logger.info 'Loading config from config/config.json'
config.loadFile "./config/config.json"
if fs.existsSync "./config/#{config.get('env')}.json"
logger.info "Loading config from config/#{config.get('env')}.json"
config.loadFile "./config/#{config.get('env')}.json"
config.validate()
module.exports = config
@@ -1,9 +1,11 @@
mongoose = require 'mongoose'
logger = require './logger'
config = require '../config'
config = require '../lib/config'
mongoose.set 'debug', (collection, method, query, doc, options) ->
logger.verbose '%s#%s'.magenta, collection, method, query, doc, options
module.exports = mongoose.connect config.db
module.exports = mongoose.connect config.get 'db', (err) ->
logger.error 'Could not connect to MongoDB', connectionString: config.get('db'), error: err
Oops, something went wrong.

0 comments on commit e1e905a

Please sign in to comment.