提交 7db2552c36a69a4b35202a465e4e9d96916d57c6

作者 Nick Baugh
1 个父辈 f9bc3b6c

Added documentation for all components, added basic test, added travis, added co…

…veralls, removed non-used dependencies in logger and mongoose-plugin, added working example, added sponsor
... ... @@ -101,3 +101,4 @@ dwsync.xml
101 101 # ------------------------------------------------------------------------------
102 102 # Add your custom excludes below
103 103 # ------------------------------------------------------------------------------
  104 +coverage/
... ...
  1 +language: node_js
  2 +node_js:
  3 + - "0.10"
  4 + - "0.11"
  5 +script: "npm run-script test-travis"
  6 +after_script: "npm install coveralls@2 && cat ./coverage/lcov.info | coveralls"
... ...
1 1
2   -# Igloo [![NPM version](https://badge.fury.io/js/igloo.png)](http://badge.fury.io/js/igloo)
  2 +# Igloo
3 3
4   -![Igloo](https://filenode.s3.amazonaws.com/igloo.png)
  4 +[![NPM version][npm-image]][npm-url]
  5 +[![Build Status][travis-image]][travis-url]
  6 +[![Dependency Status][depstat-image]][depstat-url]
  7 +[![NPM downloads][npm-downloads]][npm-url]
  8 +[![Test Coverage][coveralls-image]][coveralls-url]
5 9
6   -Igloo is a lightweight, fast, and minimal framework for rapid development.
  10 +![Igloo][igloo-image]
7 11
8   -View documentation for Igloo at <http://igloojs.com>.
  12 +> Igloo is a library of components built with [electrolyte][electrolyte] to be used as building blocks for an app.
9 13
10   -## Dependencies
  14 +Need to view documentation for a given component? See [Components](#components) below.
11 15
12   -* [Node](http://nodejs.org)
13   -* [Redis](http://redis.io/)
14   -* [MongoDB](http://www.mongodb.org/)
  16 +## Sponsor
15 17
  18 +[![Clevertech][clevertech-image]](http://clevertech.biz)
16 19
17   -## Reference
18 20
19   -Please use [`eskimo`](https://github.com/niftylettuce/eskimo) in order to build an `igloo`.
  21 +## Install
  22 +
  23 +**Please use [eskimo][eskimo] which has igloo built-in.**
  24 +
  25 +However you can use igloo in your own project by installing it.
20 26
21 27 ```bash
22   -npm install -g eskimo
23   -eskimo --help
  28 +npm install -s igloo
  29 +```
  30 +
  31 +
  32 +## Usage
  33 +
  34 +You will need to first `require('igloo')`, define a "config" component, and then use `electrolyte`'s `IoC.loader` method to load `igloo`'s components ([listed below][#components]).
  35 +
  36 +> **Note that `igloo` requires you to have defined a "config" component (since `igloo`'s "setting" component requires it as a dependency). For example, [Eskimo][eskimo] defines a "config" component here.**
  37 +
  38 +```js
  39 +// boot/config.js
  40 +
  41 +exports = module.exports = function() {
  42 + return {
  43 + defaults: {
  44 + logger: {
  45 + console: true,
  46 + requests: true,
  47 + mongo: false,
  48 + file: false,
  49 + hipchat: false
  50 + },
  51 + output: {
  52 + handleExceptions: true,
  53 + colorize: true,
  54 + prettyPrint: false
  55 + }
  56 + },
  57 + development: {
  58 + port: 3000,
  59 + },
  60 + production: {
  61 + port: 3080,
  62 + requests: false,
  63 + output: {
  64 + colorize: false,
  65 + prettyPrint: false
  66 + }
  67 + }
  68 + }
  69 +}
  70 +
  71 +exports['@singleton'] = true
  72 +
  73 +```
  74 +
  75 +
  76 +```js
  77 +// app.js
  78 +
  79 +var IoC = require('electrolyte')
  80 +var igloo = require('igloo')
  81 +var path = require('path')
  82 +var express = require('express')
  83 +var winstonRequestLogger = require('winston-request-logger')
  84 +
  85 +IoC.loader(IoC.node(path.join(__dirname, 'boot')))
  86 +IoC.loader('igloo', igloo)
  87 +
  88 +// logger component
  89 +var logger = IoC.create('igloo/logger')
  90 +logger.info('Hello world')
  91 +
  92 +// settings component
  93 +var settings = IoC.create('igloo/settings')
  94 +
  95 +// basic server
  96 +
  97 +var app = express()
  98 +
  99 +// winston request logger before everything else
  100 +// but only if it was enabled in settings
  101 +if (settings.logger.requests)
  102 + app.use(winstonRequestLogger.create(logger))
  103 +
  104 +app.get('/', function(req, res, next) {
  105 + res.send("It's cold outside, but warm in the igloo")
  106 +})
  107 +
  108 +app.listen(settings.port, function() {
  109 + logger.info('Server started on port %d', settings.port)
  110 +})
  111 +
  112 +```
  113 +
  114 +
  115 +## Components
  116 +
  117 +* [error-handler](#error-handler)
  118 +* [knex](#knex)
  119 +* [logger](#logger)
  120 +* [mongo](#mongo)
  121 +* [mongoose-plugin](#mongoose-plugin)
  122 +* [server](#server)
  123 +* [sessions](#sessions)
  124 +* [settings](#settings)
  125 +* [update-notifier](#update-notifier)
  126 +
  127 +### error-handler
  128 +
  129 +> Returns [Express][express] route middleware for error handling.
  130 +
  131 +[View source](lib/boot/error-handler.js)
  132 +
  133 +```js
  134 +// example - error handler
  135 +
  136 +var _ = require('underscore')
  137 +var errorHandler = IoC.create('igloo/error-handler')
  138 +
  139 +app.post('/user', createUser, errorHandler)
  140 +
  141 +function createUser(req, res, next) {
  142 +
  143 + if (_.isString(req.body.name))
  144 + return next({
  145 + param: 'name',
  146 + message: 'User name is missing'
  147 + })
  148 +
  149 + // ... create a user in your db
  150 + res.send('You successfully created a user')
  151 +
  152 +}
  153 +
  154 +```
  155 +
  156 +
  157 +### knex
  158 +
  159 +> Returns a [Knex][knex] SQL database connection using `settings.knex`.
  160 +
  161 +[View source](lib/boot/knex.js)
  162 +
  163 +```js
  164 +// example - knex
  165 +
  166 +var knex = IoC.create('igloo/knex')
  167 +
  168 +app.get('/users', function(req, res, next) {
  169 + knex
  170 + .raw('SELECT * FROM USERS LIMIT 10')
  171 + .exec(function(err, results) {
  172 + if (err) return next(err)
  173 + res.send(results)
  174 + })
  175 +})
  176 +
  177 +```
  178 +
  179 +
  180 +### logger
  181 +
  182 +> Returns a [Winston][winston] logger object with pre-configured transports using `settings.logger` and `settings.output`.
  183 +
  184 +[View source](lib/boot/logger.js)
  185 +
  186 +```js
  187 +// example - logger
  188 +
  189 +var logger = IoC.create('igloo/logger')
  190 +
  191 +logger.info('Hello world')
  192 +
  193 +```
  194 +
  195 +
  196 +### mongo
  197 +
  198 +> Returns a [MongoDB][mongodb] NoSQL connection using `settings.logger`, `settings.output`, and `settings.mongo` and [Mongoose][mongoose] ORM.
  199 +
  200 +> TODO: Should we rename this to `igloo/mongoose`?
  201 +
  202 +[View source](lib/boot/mongo.js)
  203 +
  204 +```js
  205 +// example - mongo
  206 +
  207 +var validator = require('validator')
  208 +var mongoose = IoC.create('igloo/mongo')
  209 +
  210 +var User = new mongoose.Schema({
  211 + email: {
  212 + type: String,
  213 + required: true,
  214 + unique: true,
  215 + validate: validator.isEmail
  216 + }
  217 +})
  218 +
  219 +mongoose.model('User', User)
  220 +
  221 +```
  222 +
  223 +
  224 +### mongoose-plugin
  225 +
  226 +> Returns a [Mongoose][mongoose] plugin helper for common schema additions.
  227 +
  228 +[View source](lib/boot/mongoose-plugin.js)
  229 +
  230 +```js
  231 +// example - mongoose plugin
  232 +
  233 +var validator = require('validator')
  234 +var mongoose = IoC.create('igloo/mongo')
  235 +var mongoosePlugin = IoC.create('igloo/mongoose-plugin')
  236 +
  237 +var User = new mongoose.Schema({
  238 + email: {
  239 + type: String,
  240 + required: true,
  241 + unique: true,
  242 + validate: validator.isEmail
  243 + }
  244 +})
  245 +
  246 +User.plugin(mongoosePlugin)
  247 +
  248 +mongoose.model('User', User)
  249 +
  250 +```
  251 +
  252 +
  253 +### server
  254 +
  255 +> Returns a function which accepts a callback argument (currently no `err` argument is returned when this callback function is executed). This component starts either a [cluster][cluster] (with one thread per CPU) or a simple single-threaded instance using `app.listen`. This should be used with `app.phase` using [bootable][bootable], since bootable's "phases" (`app.phase`) method accepts this callback function as a standard argument when defining a "phase" (read more about bootable to understand this please). This component uses `settings.logger`, `settings.output`, and `settings.server`.
  256 +
  257 +[View source](lib/boot/server.js)
  258 +
  259 +```js
  260 +// example - server
  261 +
  262 +var bootable = require('bootable')
  263 +var express = require('express')
  264 +
  265 +var app = bootable(express())
  266 +
  267 +var server = IoC.create('igloo/server')
  268 +var logger = IoC.create('igloo/logger')
  269 +
  270 +// this should always be the last phase
  271 +app.phase(server)
  272 +
  273 +app.boot(function(err) {
  274 +
  275 + if (err) {
  276 + logger.error(err.message)
  277 + if (settings.showStack)
  278 + logger.error(err.stack)
  279 + process.exit(-1)
  280 + return
  281 + }
  282 +
  283 + logger.info('app booted')
  284 +
  285 +})
  286 +
  287 +```
  288 +
  289 +
  290 +### sessions
  291 +
  292 +> Returns a [Redis][redis] connection using `settings.logger`, `settings.output`, and `settings.redis` with [express-session][express-session] and [connect-redis][connect-redis].
  293 +
  294 +[View source](lib/boot/sessions.js)
  295 +
  296 +```js
  297 +// example - sessions
  298 +
  299 +var session = require('express-session')
  300 +var express = require('express')
  301 +
  302 +var app = express()
  303 +
  304 +var sessions = IoC.create('igloo/sessions')
  305 +var settings = IoC.create('igloo/settings')
  306 +
  307 +// add req.session cookie support
  308 +settings.session.store = sessions
  309 +app.use(session(settings.session))
  310 +
  311 +```
  312 +
  313 +
  314 +### settings
  315 +
  316 +> Returns a settings object using a "config" component defined and loaded by [electrolyte][electrolyte] in a location such as "boot/config" (see initial example at the top of this Readme).
  317 +
  318 +For a full config example file, see [Eskimo's default config][eskimo-default-config].
  319 +
  320 +> TODO: Check into requiring a "local" component so Adnan's work is tested
  321 +
  322 +[View source](lib/boot/settings.js)
  323 +
  324 +
  325 +### update-notifier
  326 +
  327 +> Returns nothing, as it is used for notifying an app of module updates using [update-notifier][update-notifier].
  328 +
  329 +> TODO: Look into why this isn't working per <https://github.com/yeoman/update-notifier/issues/25>
  330 +
  331 +[View source](lib/boot/update-notifier.js)
  332 +
  333 +
  334 +## Tests
  335 +
  336 +```bash
  337 +npm install -d
  338 +npm test
24 339 ```
25 340
26 341
... ... @@ -38,12 +353,35 @@ eskimo --help
38 353
39 354 ## License
40 355
41   -The MIT License
  356 +[MIT](LICENSE)
  357 +
  358 +
  359 +[npm-image]: http://img.shields.io/npm/v/igloo.svg?style=flat
  360 +[npm-url]: https://npmjs.org/package/igloo
  361 +[npm-downloads]: http://img.shields.io/npm/dm/igloo.svg?style=flat
42 362
43   -Copyright (c) 2014- Nick Baugh niftylettuce@gmail.com (http://niftylettuce.com/)
  363 +[travis-url]: http://travis-ci.org/niftylettuce/igloo
  364 +[travis-image]: http://img.shields.io/travis/niftylettuce/igloo.svg?style=flat
44 365
45   -Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  366 +[depstat-url]: https://gemnasium.com/niftylettuce/igloo
  367 +[depstat-image]: http://img.shields.io/gemnasium/niftylettuce/igloo.svg?style=flat
46 368
47   -The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  369 +[coveralls-image]: https://img.shields.io/coveralls/niftylettuce/igloo.svg?style=flat
  370 +[coveralls-url]: https://coveralls.io/r/niftylettuce/igloo?branch=master
48 371
49   -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  372 +[igloo-image]: https://filenode.s3.amazonaws.com/igloo.png
  373 +[eskimo]: https://github.com/niftylettuce/eskimo
  374 +[clevertech-image]: http://eskimo.io/img/clevertech.png
  375 +[electrolyte]: https://github.com/jaredhanson/electrolyte
  376 +[knex]: http://knexjs.org
  377 +[express]: http://expressjs.com
  378 +[winston]: https://github.com/flatiron/winston
  379 +[mongodb]: https://github.com/mongodb/node-mongodb-native
  380 +[mongoose]: http://mongoosejs.com/
  381 +[bootable]: https://github.com/jaredhanson/bootable
  382 +[cluster]: http://nodejs.org/api/cluster.html
  383 +[redis]: http://redis.io
  384 +[express-session]: https://github.com/expressjs/session
  385 +[connect-redis]: https://github.com/visionmedia/connect-redis
  386 +[eskimo-default-config]: https://github.com/niftylettuce/eskimo/blob/master/boot/config.js
  387 +[update-notifier]: https://github.com/yeoman/update-notifier
... ...
  1 +// app.js
  2 +
  3 +var IoC = require('electrolyte')
  4 +var igloo = require('../')
  5 +var path = require('path')
  6 +var express = require('express')
  7 +var winstonRequestLogger = require('winston-request-logger')
  8 +
  9 +IoC.loader(IoC.node(path.join(__dirname, 'boot')))
  10 +IoC.loader('igloo', igloo)
  11 +
  12 +// logger component
  13 +var logger = IoC.create('igloo/logger')
  14 +logger.info('Hello world')
  15 +
  16 +// settings component
  17 +var settings = IoC.create('igloo/settings')
  18 +
  19 +// basic server
  20 +
  21 +var app = express()
  22 +
  23 +// winston request logger before everything else
  24 +// but only if it was enabled in settings
  25 +if (settings.logger.requests)
  26 + app.use(winstonRequestLogger.create(logger))
  27 +
  28 +app.get('/', function(req, res, next) {
  29 + res.send("It's cold outside, but warm in the igloo")
  30 +})
  31 +
  32 +app.listen(settings.port, function() {
  33 + logger.info('Server started on port %d', settings.port)
  34 +})
... ...
  1 +// boot/config.js
  2 +
  3 +exports = module.exports = function() {
  4 + return {
  5 + defaults: {
  6 + logger: {
  7 + console: true,
  8 + requests: true,
  9 + mongo: false,
  10 + file: false,
  11 + hipchat: false
  12 + },
  13 + output: {
  14 + handleExceptions: true,
  15 + colorize: true,
  16 + prettyPrint: false
  17 + }
  18 + },
  19 + development: {
  20 + port: 3000,
  21 + },
  22 + production: {
  23 + port: 3080,
  24 + requests: false,
  25 + output: {
  26 + colorize: false,
  27 + prettyPrint: false
  28 + }
  29 + }
  30 + }
  31 +}
  32 +
  33 +exports['@singleton'] = true
... ...
  1 +{
  2 + "name": "example",
  3 + "version": "0.0.1",
  4 + "dependencies": {
  5 + "electrolyte": "0.0.4",
  6 + "express": "^4.9.0",
  7 + "winston-request-logger": "^1.0.5"
  8 + }
  9 +}
... ...
1 1
2 2 // # boot - logger
3 3
  4 +var _ = require('underscore')
4 5 var mergeDefaults = require('merge-defaults')
5 6 var winston = require('winston')
6 7 var winstonMongoDB = require('winston-mongodb')
... ... @@ -8,6 +9,12 @@ var winstonHipchat = require('winston-hipchat')
8 9
9 10 exports = module.exports = function(settings) {
10 11
  12 + if (!_.isObject(settings.logger))
  13 + throw new Error('Settings did not have a `logger` object')
  14 +
  15 + if (!_.isObject(settings.output))
  16 + throw new Error('Settings did not have a `output` object')
  17 +
11 18 var transports = []
12 19
13 20 if (settings.logger['console'])
... ...
... ... @@ -4,7 +4,7 @@
4 4 var _ = require('underscore')
5 5 var jsonSelect = require('mongoose-json-select')
6 6
7   -exports = module.exports = function(settings) {
  7 +exports = module.exports = function() {
8 8
9 9 return function(Schema) {
10 10
... ... @@ -46,4 +46,3 @@ exports = module.exports = function(settings) {
46 46 }
47 47
48 48 exports['@singleton'] = true
49   -exports['@require'] = [ 'igloo/settings' ]
... ...
... ... @@ -31,17 +31,26 @@
31 31 "express-session": "^1.2.1",
32 32 "merge-defaults": "^0.1.0",
33 33 "to-camel-case": "^0.2.1",
34   - "underscore": "~1.6.0",
  34 + "underscore": "^1.6.0",
35 35 "update-notifier": "^0.2.0",
36 36 "winston": "git://github.com/niftylettuce/winston",
37 37 "winston-hipchat": "^0.1.1",
38 38 "winston-mongodb": "~0.4.3"
39 39 },
40 40 "devDependencies": {
  41 + "chai": "^1.9.1",
  42 + "istanbul": "^0.3.0",
41 43 "knex": "^0.6.13",
42 44 "mariasql": "^0.1.21",
  45 + "mocha": "^1.21.4",
43 46 "mongoose": "~3.8.7",
44 47 "mongoose-json-select": "^0.2.1",
45 48 "mysql": "^2.3.2"
  49 + },
  50 + "scripts": {
  51 + "prepublish": "npm prune",
  52 + "test": "mocha --reporter spec --bail --check-leaks --require test/support/should test/",
  53 + "test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --reporter dot --check-leaks --require test/support/should test/",
  54 + "test-travis": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --reporter spec --check-leaks --require test/support/should test/"
46 55 }
47 56 }
... ...
  1 +
  2 +describe('igloo', function() {
  3 +
  4 + it('should return function', function() {
  5 + var igloo = require('../')
  6 + igloo.should.be.a('function')
  7 + })
  8 +
  9 + /*
  10 + describe('error-handler', function() {
  11 +
  12 + })
  13 +
  14 + describe('knex', function() {
  15 +
  16 + })
  17 +
  18 + describe('logger', function() {
  19 +
  20 + })
  21 +
  22 + describe('mongo', function() {
  23 +
  24 + })
  25 +
  26 + describe('mongoose-plugin', function() {
  27 +
  28 + })
  29 +
  30 + describe('server', function() {
  31 +
  32 + })
  33 +
  34 + describe('sessions', function() {
  35 +
  36 + })
  37 +
  38 + describe('settings', function() {
  39 +
  40 + })
  41 +
  42 + describe('update-notifier', function() {
  43 +
  44 + })
  45 + */
  46 +
  47 +})
... ...
  1 +var chai = require('chai')
  2 +chai.should()
... ...
注册登录 后发表评论