-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathapp.js
More file actions
107 lines (88 loc) · 2.56 KB
/
app.js
File metadata and controls
107 lines (88 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/**
* Module dependencies.
*/
if (!process.env.NINJA_CLIENT_ID||!process.env.NINJA_CLIENT_SECRET)
throw new Error('Ninja client credentials have not been set!')
var express = require('express')
, routes = require('./routes')
, http = require('http')
, path = require('path')
, redisClient = require('redis-url').connect(process.env.REDISTOGO_URL)
, RedisStore = require('connect-redis')(express);
var app = express();
var authom = require('authom');
app.configure(function(){
app.set('port', process.env.PORT || 8000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.cookieParser())
app.use(express.session({secret:"ninjaALLTHETHINGS",store: new RedisStore({client:redisClient})}));
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(function(req,res,next) {
req.redis = redisClient;
next();
});
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
});
app.configure('development', function(){
app.use(express.errorHandler());
});
authom.createServer({
service:"ninjablocks",
id:process.env.NINJA_CLIENT_ID,
secret:process.env.NINJA_CLIENT_SECRET,
scope:['all']
});
/*
Authom configuration
*/
authom.on('auth',function(req,res,user) {
/*
* `user` is an object containing your accesss `token`,
* the user's unique `id`, and `data` about the user (`id`,
* `name`, `email`, and their `pusherChannel`).
*
* @param {String} token
* @param {String} id
* @param {Object} data
*/
var ninja = user.data;
ninja.access_token = user.token;
var toSet = {};
for (var i in ninja) {
if (typeof ninja[i] !== "undefined" && null !== ninja[i]) {
toSet[i]=ninja[i].toString();
}
}
req.session.ninja = toSet;
redisClient.hmset('user:'+user.id, toSet,function(err) {
if (err) throw err;
else routes.subscribeToDataFeed(req,res);
});
});
authom.on('error',function(req,res,data) {
console.log(data);
});
/*
App Routes
*/
app.get('/', routes.index);
app.put('/device/:deviceGuid/colour', routes.sendLedValue);
app.post('/data_callback',routes.handleInboundData);
/*
Authom routes
*/
app.get("/auth/:service",authom.app);
app.get('/user',function(req,res) {
var ninja = require('ninja-blocks').app({access_token:req.session.ninja.access_token});
ninja.user(function(err,data) {
res.json(data);
});
});
http.createServer(app).listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
});