-
Notifications
You must be signed in to change notification settings - Fork 8
/
app.js
135 lines (106 loc) · 3.89 KB
/
app.js
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
var express = require('express');
var passport = require('passport');
var ImfBackendStrategy = require('passport-imf-token-validation').ImfBackendStrategy;
var imf = require('imf-oauth-user-sdk');
try {
passport.use(new ImfBackendStrategy());
} catch ( e ) {
console.log(e);
}
var app = express();
var server = require('http').Server(app);
var io = require('socket.io')(server);
app.use(passport.initialize());
app.set('view engine', 'jade');
var credentials = {};
if (process.env.hasOwnProperty("VCAP_SERVICES")) {
// Running on Bluemix. Parse out the port and host that we've been assigned.
var env = JSON.parse(process.env.VCAP_SERVICES);
var host = process.env.VCAP_APP_HOST;
var port = process.env.VCAP_APP_PORT;
credentials = env['cloudantNoSQLDB'][0].credentials;
}
else {
//for local node.js server instance
credentials.username = "";
credentials.password = "";
credentials.url = "";
}
var database = "geopix";
var Cloudant = require('cloudant');
var geopix;
Cloudant({account:credentials.username, password:credentials.password}, function(err, cloudant) {
console.log('Connected to Cloudant');
geopix = cloudant.use(database);
var feed = geopix.follow({include_docs: true, since: "now"});
feed.on('change', function (change) {
console.log("change: ", change);
io.sockets.emit('image', change);
});
feed.follow();
})
var prepareData = function(res, template) {
var results = [];
//create the index if it doesn't already exist
var sort_index = {name:'sort', type:'json', index:{fields:['sort']}};
geopix.index(sort_index, function(er, response) {
if (er) {
throw er;
}
console.log('Index creation result: %s', response.result);
//perform the search
var selector = {sort:{"$gt":0}};
geopix.find({selector:selector, sort:["sort"]}, function(er, result) {
if (er) {
throw er;
}
console.log('Found %d documents with type com.geopix.entry', result.docs.length)
for (var x=0; x<result.docs.length; x++) {
var obj = result.docs[x];
for (var key in obj._attachments) {
obj.image = "/image/" + obj._id +"/" + key;
break;
}
results.push( obj );
}
res.render(template, { results:results});
});
});
};
app.get('/', function(req, res){
prepareData(res, 'map');
});
app.get('/list', function(req, res){
prepareData(res, 'list');
});
app.get('/image/:doc/:image', function(req, res){
geopix.attachment.get(req.params.doc, req.params.image).pipe(res);
res.type('jpg');
});
// create a public static content service
app.use("/public", express.static(__dirname + '/public'));
// create another static content service, and protect it with imf-backend-strategy
app.use("/protected", passport.authenticate('imf-backend-strategy', {session: false }));
app.use("/protected", express.static(__dirname + '/protected'));
// create a backend service endpoint
app.get('/publicServices/generateToken', function(req, res){
// use imf-oauth-user-sdk to get the authorization header, which can be used to access the protected resource/endpoint by imf-backend-strategy
imf.getAuthorizationHeader().then(function(token) {
res.send(200, token);
}, function(err) {
console.log(err);
});
}
);
//create another backend service endpoint, and protect it with imf-backend-strategy
app.get('/protectedServices/test', passport.authenticate('imf-backend-strategy', {session: false }),
function(req, res){
res.send(200, "Successfully access to protected backend endpoint.");
}
);
io.on('connection', function (socket) {
console.log('New Client WSS Connection.')
});
var port = (process.env.VCAP_APP_PORT || 3000);
server.listen(port);
console.log("mobile backend app is listening at " + port);