Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
seneca 0.5.21
  • Loading branch information
rjrodger committed Oct 7, 2014
1 parent 8540d36 commit f08517c
Show file tree
Hide file tree
Showing 12 changed files with 153 additions and 42 deletions.
6 changes: 3 additions & 3 deletions data-entities/package.json
@@ -1,15 +1,15 @@
{
"name": "data-entities-seneca-example",
"version": "0.0.2",
"version": "0.0.3",
"description": "Data entities example",
"subdomain": "data-entities-seneca-example",
"main": "main.js",
"repository": "",
"author": "Richard Rodger",
"license": "MIT",
"dependencies": {
"seneca": "~0.5.18",
"seneca": "~0.5.21",
"seneca-level-store": "~0.1.3",
"seneca-jsonfile-store": "~0.1.7"
"seneca-jsonfile-store": "~0.1.9"
}
}
11 changes: 10 additions & 1 deletion micro-services/README.txt
Expand Up @@ -25,7 +25,7 @@ $ node index.js
This always prints merged debug logs for all services. The individual
logs are saved to the log folder.

The micro-services communicate over the default builtin TCP channel,
The micro-services communicate over the default built-in HTTP channel,
and all listen on different ports. Review the .client and .listen
method calls in the services/*.js files to see how the channels are
set up.
Expand All @@ -35,9 +35,18 @@ lib/*.js. Notice that the user-details service is just pulling out the
standard seneca-user plugin implementation into a separate service.

Open http://localhost:3000 to see the services in action.
Login with username:u1, password:u2

The offered product depends on your login status.

The web-app delivers the main web application using express.
The user-details delivers user login and logout.
The offer-service delivers product offers based on the visitor
being identified (i.e. logged in) or not.

This example is a vastly simplified version of Fred George's talk:
http://oredev.org/2013/wed-fri-conference/implementing-micro-service-architectures




1 change: 0 additions & 1 deletion micro-services/lib/api.js
Expand Up @@ -4,7 +4,6 @@ module.exports = function( options ) {
var plugin = 'api'



seneca.add( {role:plugin, end:'offer'}, end_offer)


Expand Down
2 changes: 1 addition & 1 deletion micro-services/package.json
Expand Up @@ -11,7 +11,7 @@
"dependencies": {
"body-parser": "~1.4.3",
"express": "~4.5.1",
"seneca": "~0.5.19",
"seneca": "~0.5.21",
"seneca-auth": "~0.2.10",
"seneca-user": "~0.2.10"
}
Expand Down
22 changes: 22 additions & 0 deletions micro-services/test/offer.test.js
@@ -0,0 +1,22 @@

var assert = require('assert')

var seneca = require('seneca')()
.use('../lib/offer.js')

seneca.act('role:offer,cmd:provide',function(err,out){
assert.ok(null==err)
assert.equal('Orange',out.product)
})

seneca.act('role:offer,cmd:provide,nick:foo',function(err,out){
assert.ok(null==err)
assert.equal('Apple',out.product)
})



var foo = require('./foo.js')
seneca.add('mod:foo',foo)

seneca.act('mod:foo,bar:1',console.log)
4 changes: 2 additions & 2 deletions plugin-web/package.json
@@ -1,6 +1,6 @@
{
"name": "plugin-web-seneca-example",
"version": "0.0.2",
"version": "0.0.3",
"description": "Plugin web example",
"subdomain": "plugin-web-seneca-example",
"main": "app.js",
Expand All @@ -16,7 +16,7 @@
"cookie-parser": "~1.3.2",
"method-override": "~2.1.0",
"optimist": "~0.6.1",
"seneca": "~0.5.18",
"seneca": "~0.5.21",
"connect": "~3.0.1",
"serve-static": "~1.3.1"
}
Expand Down
59 changes: 36 additions & 23 deletions shopping-cart/app.js
Expand Up @@ -35,12 +35,17 @@ seneca.use('mem-store',{web:{dump:true}})
// return the next day
seneca.use('engage')



// the shopping cart plugin provides standard web shopping cart business logic
// and also a HTTP JSON api
seneca.use('cart')






// set up express
var app = express()
app.enable('trust proxy')
Expand All @@ -53,6 +58,8 @@ app.use(bodyParser.json())

app.use(serveStatic(__dirname + '/public'))



// expose the shopping cart api
// the seneca.export('web') method returns a single function with the signature
// function(req,res,next) that can be used with connect or express
Expand Down Expand Up @@ -87,8 +94,8 @@ function handlecart(req,res,next,action) {
req.seneca.act('role:engage,cmd:get,key:cart',function(err,out) {
if( err ) return next(err);

action(out.value, function(cart){
req.seneca.act('role:engage,cmd:set,key:cart',{value:cart})
action(out.value, function(cart,done){
req.seneca.act('role:engage,cmd:set,key:cart',{value:cart},done)
})
})
}
Expand All @@ -99,8 +106,10 @@ app.get('/', function(req,res,next) {
req.seneca.act('role:cart,cmd:get',{cart:cart},function(err,out) {
if( err ) return next(err);

end(out.cart)
res.render('index.ejs',{locals:{cart:out.cart,formatprice:formatprice}})
end(out.cart,function(err){
if( err ) return next(err);
res.render('index.ejs',{locals:{cart:out.cart,formatprice:formatprice}})
})
})
})
})
Expand All @@ -111,8 +120,10 @@ app.get('/cart', function(req,res,next){
req.seneca.act('role:cart,cmd:get',{cart:cart},function(err,out) {
if( err ) return next(err);

end(out.cart)
res.render('cart.ejs',{locals:{cart:out.cart,formatprice:formatprice}})
end(out.cart,function(err){
if( err ) return next(err);
res.render('cart.ejs',{locals:{cart:out.cart,formatprice:formatprice}})
})
})
})
})
Expand All @@ -123,32 +134,36 @@ app.get('/checkout', function(req,res,next){
req.seneca.act('role:cart,cmd:get',{cart:cart},function(err,out) {
if( err ) return next(err);

end(out.cart)
res.render('checkout.ejs',{locals:{cart:out.cart,formatprice:formatprice}})
end(out.cart,function(err){
if( err ) return next(err);
res.render('checkout.ejs',{locals:{cart:out.cart,formatprice:formatprice}})
})
})
})
})

seneca.ready( function(){

// ensure that cart actions get the cart from the engagement
seneca.wrap({role:'cart',cmd:'*'},function(args,done){
var seneca = this
// ensure that cart actions get the cart from the engagement
seneca.wrap({role:'cart',cmd:'*'},function(args,done){
var seneca = this

// grab the cart from the engagement
handlecart( args.req$, args.res$, done, function(cart,end){
// grab the cart from the engagement
handlecart( args.req$, args.res$, done, function(cart,end){

// set the cart parameter for the cart actions
args.cart = cart
seneca.prior(args,function(err,out){
if(err) return done(err);
// set the cart parameter for the cart actions
args.cart = cart
seneca.prior(args,function(err,out){
if(err) return done(err);

// save the updated cart
end(cart)
done(null,out)
// save the updated cart
end(cart)
done(null,out)
})
})
})
})

})

// use the node.js http api to create a HTTP server
// this allows the admin plugin to use websockets
Expand All @@ -160,8 +175,6 @@ server.listen(conf.port)
seneca.use('data-editor',{admin:{local:true}})
seneca.use('admin',{server:server,local:true})



// set up some test products for the store
// create a product entity object
var product_ent = seneca.make$('shop','product')
Expand Down
8 changes: 4 additions & 4 deletions shopping-cart/package.json
@@ -1,6 +1,6 @@
{
"name": "shopping-cart-seneca-example",
"version": "0.0.2",
"version": "0.0.3",
"description": "Seneca shopping cart example",
"subdomain": "shopping-cart-seneca-example",
"main": "app.js",
Expand All @@ -20,10 +20,10 @@
"optimist": "~0.6.1",
"ejs": "~1.0.0",
"ejs-locals": "~1.0.2",
"seneca": "~0.5.18",
"seneca": "~0.5.21",
"seneca-user": "~0.2.10",
"seneca-auth": "~0.2.10",
"seneca-cart": "~0.1.2",
"seneca-auth": "~0.3.0",
"seneca-cart": "~0.2.0",
"seneca-engage": "~0.2.1",
"seneca-data-editor": "~0.1.6",
"seneca-admin": "~0.1.4"
Expand Down
17 changes: 17 additions & 0 deletions simple-plugin/package.json
@@ -0,0 +1,17 @@
{
"name": "simple-plugin",
"version": "0.0.1",
"description": "Simple plugin example",
"main": "simple.js",
"scripts": {
"test": "./node_modules/.bin/mocha test/*.test.js"
},
"author": "Richard Rodger (http://richardrodger.com)",
"license": "MIT",
"devDependencies": {
"mocha": "^1.21.4"
},
"dependencies": {
"seneca": "^0.5.21"
}
}
28 changes: 28 additions & 0 deletions simple-plugin/simple.js
@@ -0,0 +1,28 @@
"use strict";

module.exports = function simple() {
var seneca = this

var suffix = ''

seneca.add('role:simple,cmd:foo', cmd_foo)

seneca.add('init:simple', init)


function cmd_foo( args, done ) {
done( null, {text:'foo-'+args.text+suffix} )
}


function init( args, done ) {
var seneca = this
seneca.log.info("preparing something...")

setTimeout( function() {
suffix = '-zed'
seneca.log.log("ready!")
done()
}, 111 )
}
}
23 changes: 23 additions & 0 deletions simple-plugin/test/simple.test.js
@@ -0,0 +1,23 @@
"use strict";

var assert = require('assert')

var seneca = require('seneca')


describe('simple', function(){

it('happy', function( fin){

seneca({log:'silent',errhandler:fin})
.use('..')
.act('role:simple,cmd:foo,text:red',
function( err, out ) {
if( err ) return fin(err);

assert.equal( out.text, 'foo-red-zed' )
fin()
})
})

})
14 changes: 7 additions & 7 deletions user-accounts/package.json
@@ -1,6 +1,6 @@
{
"name": "user-accounts-seneca-example",
"version": "0.0.6",
"version": "0.0.7",
"description": "User accounts example",
"subdomain": "user-accounts-seneca-example",
"main": "app.js",
Expand All @@ -11,16 +11,16 @@
"author": "Richard Rodger",
"license": "MIT",
"dependencies": {
"express": "~4.6.0",
"body-parser": "~1.4.3",
"express": "~4.9.5",
"body-parser": "~1.9.0",
"cookie-parser": "~1.3.2",
"method-override": "~2.1.0",
"express-session": "~1.6.4",
"serve-static": "~1.3.1",
"method-override": "~2.2.0",
"express-session": "~1.8.2",
"serve-static": "~1.6.3",
"optimist": "~0.6.1",
"ejs": "~1.0.0",
"ejs-locals": "~1.0.2",
"seneca": "~0.5.20",
"seneca": "~0.5.21",
"seneca-user": "~0.2.10",
"seneca-auth": "~0.3.0",
"seneca-admin": "~0.1.4",
Expand Down

0 comments on commit f08517c

Please sign in to comment.