-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Kristina Matuleviciute
committed
Jun 1, 2016
1 parent
61ff93c
commit 4010d45
Showing
40 changed files
with
486 additions
and
533 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"extends": ["seneca"] | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,114 +1,99 @@ | ||
// PUBLIC DOMAIN | ||
"use strict"; | ||
'use strict' | ||
|
||
module.exports = function api( options ) { | ||
module.exports = function api (options) { | ||
var seneca = this | ||
|
||
seneca.add('role:api,info:hello', hello) | ||
seneca.add('role:api,product:star', get_star) | ||
seneca.add('role:api,product:handle_star', handle_star) | ||
|
||
|
||
seneca.add('init:api',function(args,done){ | ||
|
||
seneca.add('init:api', function (args, done) { | ||
// Order is significant here! | ||
|
||
seneca.act('role:web',{use:{ | ||
prefix:'/', | ||
pin:'role:api,info:*', | ||
map:{ | ||
hello:true | ||
seneca.act('role:web', {use: { | ||
prefix: '/', | ||
pin: 'role:api,info:*', | ||
map: { | ||
hello: true | ||
} | ||
}}) | ||
|
||
seneca.use( | ||
{name:'jsonrest-api',tag:'product'}, | ||
{name: 'jsonrest-api', tag: 'product'}, | ||
{ | ||
prefix: '/', | ||
list: {embed:'list'}, | ||
pin: { name:'product' }, | ||
list: {embed: 'list'}, | ||
pin: { name: 'product' }, | ||
startware: verify_token, | ||
allow_id: true | ||
}) | ||
|
||
seneca.act('role:web',{use:{ | ||
prefix:'/product', | ||
pin:'role:api,product:*', | ||
startware: verify_token, | ||
map:{ | ||
star: { | ||
alias:'/:id/star' | ||
}, | ||
handle_star:{ | ||
PUT:true, | ||
DELETE:true, | ||
alias:'/:id/star' | ||
} | ||
seneca.act('role:web', {use: { | ||
prefix: '/product', | ||
pin: 'role:api,product:*', | ||
startware: verify_token, | ||
map: { | ||
star: { | ||
alias: '/:id/star' | ||
}, | ||
handle_star: { | ||
PUT: true, | ||
DELETE: true, | ||
alias: '/:id/star' | ||
} | ||
}}) | ||
|
||
done() | ||
}) | ||
|
||
|
||
var internal_err = { | ||
http$: {status:500}, | ||
why: 'Internal error.' | ||
} | ||
|
||
var bad_input = { | ||
http$: {status:400}, | ||
why: 'Bad input.' | ||
} | ||
|
||
|
||
function verify_token(req,res,next) { | ||
var token = req.headers['api-access-token'] | ||
|
||
} | ||
}}) | ||
|
||
done() | ||
}) | ||
|
||
var internal_err = { | ||
http$: {status: 500}, | ||
why: 'Internal error.' | ||
} | ||
|
||
var bad_input = { | ||
http$: {status: 400}, | ||
why: 'Bad input.' | ||
} | ||
|
||
function verify_token (req, res, next) { | ||
var token = req.headers['api-access-token'] | ||
// Hard coded token for this example!!! | ||
if( '1234' == token ) return next(); | ||
|
||
res.writeHead(401,"Access denied.") | ||
res.end() | ||
} | ||
|
||
|
||
function hello( args, done ) { | ||
done(null,{msg:'hello!'}) | ||
} | ||
|
||
function get_star( args, done ) { | ||
this.make$('product').load$(args.id,function(err,res){ | ||
if(err) return done(null,internal_err); | ||
if(!res) return done(null,bad_input); | ||
|
||
return done(null,{ | ||
product: res.id, | ||
star: res.star | ||
}) | ||
if('1234' === token) return next() | ||
res.writeHead(401, 'Access denied.') | ||
res.end() | ||
} | ||
|
||
function hello (args, done) { | ||
done(null, {msg: 'hello!'}) | ||
} | ||
|
||
function get_star (args, done) { | ||
this.make$('product').load$(args.id, function (err, res) { | ||
if(err) return done(null, internal_err) | ||
if(!res) return done(null, bad_input) | ||
return done(null, { | ||
product: res.id, | ||
star: res.star | ||
}) | ||
} | ||
|
||
function handle_star( args, done ) { | ||
this.act( | ||
}) | ||
} | ||
|
||
function handle_star (args, done) { | ||
this.act( | ||
'role:product,cmd:star', | ||
{ | ||
// product id | ||
id:args.id, | ||
|
||
// PUT means star, DELETE means unstar | ||
star:('PUT'===args.req$.method) | ||
}, | ||
|
||
function(err,res){ | ||
if(err) return done(null,internal_err); | ||
if(!res.ok) return done(null,bad_input) | ||
|
||
return done(null,{ | ||
{ | ||
// product id | ||
id: args.id, | ||
// PUT means star, DELETE means unstar | ||
star: ('PUT' === args.req$.method) | ||
}, | ||
|
||
function (err, res) { | ||
if(err) return done(null, internal_err) | ||
if(!res.ok) return done(null, bad_input) | ||
return done(null, { | ||
product: res.id, | ||
star: res.star | ||
star: res.star | ||
}) | ||
} | ||
) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,14 @@ | ||
// PUBLIC DOMAIN | ||
|
||
var seneca = require('seneca')() | ||
.use('./product_catalog') | ||
.use('./api') | ||
.ready(function(){ | ||
.ready(function () { | ||
this.make$('product') | ||
.make$({id$:0,name:'Apple',price:99,star:0}).save$() | ||
.make$({id$:1,name:'Orange',price:199,star:0}).save$() | ||
.make$({id$: 0, name: 'Apple', price: 99, star: 0}).save$() | ||
.make$({id$: 1, name: 'Orange', price: 199, star: 0}).save$() | ||
}) | ||
|
||
var app = require('express')() | ||
.use( require('body-parser').json() ) | ||
.use( seneca.export('web') ) | ||
require('express')() | ||
.use(require('body-parser').json()) | ||
.use(seneca.export('web')) | ||
.listen(3000) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,39 @@ | ||
var util = require('util') | ||
var util = require ('util') | ||
|
||
var request = require('request') | ||
var _ = require('lodash') | ||
var Request = require('request') | ||
var _ = require('lodash') | ||
|
||
var base = 'http://localhost:3000/' | ||
var headers = {'api-access-token':'1234'} | ||
|
||
|
||
;print('get',{url:base+'hello',headers:headers},function(){ | ||
|
||
;print('get',{url:base+'product',headers:headers},function(){ | ||
;print('get',{url:base+'product/0',headers:headers},function(){ | ||
;print('get',{url:base+'product/1',headers:headers},function(){ | ||
;print('get',{url:base+'product/2',headers:headers},function(){ | ||
|
||
;print('post',{url:base+'product',headers:headers,json:{ | ||
id$:2, name:'Pear', price:299 | ||
}},function(){ | ||
|
||
;print('get',{url:base+'product/2',headers:headers},function(){ | ||
|
||
;print('put',{url:base+'product/2',headers:headers,json:{ | ||
name:'Pear', price:Math.floor(300*Math.random()) | ||
}},function(){ | ||
|
||
;print('get',{url:base+'product/2',headers:headers},function(){ | ||
|
||
;print('del',{url:base+'product/1',headers:headers},function(){ | ||
;print('get',{url:base+'product/1',headers:headers},function(){ | ||
|
||
;print('get',{url:base+'product/0/star',headers:headers},function(){ | ||
;print('put',{url:base+'product/0/star',headers:headers},function(){ | ||
;print('get',{url:base+'product/0/star',headers:headers},function(){ | ||
|
||
})})})})})})})})})})})})})}) | ||
|
||
|
||
|
||
// utility function to pretty print request results | ||
function print() { | ||
var headers = {'api-access-token': '1234'} | ||
|
||
|
||
;print('get', {url: base + 'hello', headers: headers}, function () { | ||
;print('get', {url: base + 'product', headers: headers}, function () { | ||
;print('get', {url: base + 'product/0', headers: headers}, function () { | ||
;print('get', {url: base + 'product/1', headers: headers}, function () { | ||
;print('get', {url: base + 'product/2', headers: headers}, function () { | ||
;print('post', {url: base + 'product', headers: headers, json: { | ||
id$: 2, name: 'Pear', price: 299 | ||
}}, function () { | ||
;print('get', {url: base + 'product/2', headers: headers}, function () { | ||
;print('put', {url: base + 'product/2', headers: headers, json: { | ||
name: 'Pear', price: Math.floor(300 * Math.random()) | ||
}}, function () { | ||
;print('get', {url: base + 'product/2', headers: headers}, function () { | ||
;print('del', {url: base + 'product/1', headers: headers}, function () { | ||
;print('get', {url: base + 'product/1', headers: headers}, function () { | ||
;print('get', {url: base + 'product/0/star', headers: headers}, function () { | ||
;print('put', {url: base + 'product/0/star', headers: headers}, function () { | ||
;print('get', {url: base + 'product/0/star', headers: headers}, function () { | ||
}) }) }) }) }) }) }) }) }) }) }) }) }) }) | ||
|
||
// utility function to pretty print request results | ||
function print () { | ||
var a = Array.prototype.slice.call(arguments) | ||
var mstr = a[0] | ||
|
||
var n = _.isFunction( a[a.length-1] ) ? a.pop() : null | ||
|
||
request[mstr].apply(request, a.slice(1).concat(function(err,res,body){ | ||
console.log(mstr, res.req.path, res.statusCode, err||body) | ||
var n = _.isFunction(a[ a.length - 1 ]) ? a.pop() : null | ||
Request[mstr].apply(Request, a.slice(1).concat(function (err, res, body) { | ||
console.log(mstr, res.req.path, res.statusCode, err || body) | ||
!err && n && n() | ||
})) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,31 @@ | ||
// PUBLIC DOMAIN | ||
"use strict"; | ||
'use strict' | ||
|
||
module.exports = function product_catalog( options ) { | ||
module.exports = function product_catalog (options) { | ||
var seneca = this | ||
|
||
seneca.add('role:product,cmd:star', cmd_star) | ||
|
||
|
||
function cmd_star( args, done ) { | ||
function cmd_star (args, done) { | ||
var seneca = this | ||
|
||
seneca | ||
.make$('product') | ||
.load$(args.id,function( err, product ) { | ||
if(err) return done(err); | ||
|
||
if( product ) { | ||
product.star = Math.max(0,( | ||
(product.star||0) + | ||
((args.star&&true) ? +1 : -1 ))) | ||
|
||
product.save$(function(err,product){ | ||
if(err) return done(err); | ||
|
||
return done(null,{ | ||
ok: true, | ||
id: product.id, | ||
.load$(args.id, function (err, product) { | ||
if(err) return done(err) | ||
if(product) { | ||
product.star = Math.max(0, ( | ||
(product.star || 0) + | ||
((args.star && true) ? +1 : -1))) | ||
|
||
product.save$(function (err, product) { | ||
if(err) return done(err) | ||
|
||
return done(null, { | ||
ok: true, | ||
id: product.id, | ||
star: product.star | ||
}) | ||
}) | ||
} | ||
else return done(null,{ok:false}) | ||
else return done(null, {ok: false}) | ||
}) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
module.exports = { | ||
'mem-store': { web: { dump:true } }, | ||
web: {debug: { service:true } }, | ||
'mem-store': {web: {dump: true}}, | ||
'web': {debug: {service: true}} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"extends": ["seneca"] | ||
} | ||
|
Oops, something went wrong.