-
Notifications
You must be signed in to change notification settings - Fork 35
/
app.js
112 lines (92 loc) · 3.79 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
'use strict';
// Module Dependencies
// -------------------
var express = require('express');
var http = require('http');
var JWT = require('./lib/jwtDecoder');
var path = require('path');
var request = require('request');
var routes = require('./routes');
var activityCreate = require('./routes/activityCreate');
var activityUpdate = require('./routes/activityUpdate');
var activityUtils = require('./routes/activityUtils');
var pkgjson = require( './package.json' );
var app = express();
// Register configs for the environments where the app functions
// , these can be stored in a separate file using a module like config
var APIKeys = {
appId : '__insert_your_app_id__',
clientId : '__insert_your_app_client_id__',
clientSecret : '__insert_your_app_client_secret__',
appSignature : '__insert_your_app_signature__',
authUrl : 'https://auth.exacttargetapis.com/v1/requestToken?legacy=1'
};
// Simple custom middleware
function tokenFromJWT( req, res, next ) {
// Setup the signature for decoding the JWT
var jwt = new JWT({appSignature: APIKeys.appSignature});
// Object representing the data in the JWT
var jwtData = jwt.decode( req );
// Bolt the data we need to make this call onto the session.
// Since the UI for this app is only used as a management console,
// we can get away with this. Otherwise, you should use a
// persistent storage system and manage tokens properly with
// node-fuel
req.session.token = jwtData.token;
next();
}
// Use the cookie-based session middleware
app.use(express.cookieParser());
// TODO: MaxAge for cookie based on token exp?
app.use(express.cookieSession({secret: "DeskAPI-CookieSecret0980q8w0r8we09r8"}));
// Configure Express
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(express.favicon());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
// Express in Development Mode
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
// HubExchange Routes
app.get('/', routes.index );
app.post('/login', tokenFromJWT, routes.login );
app.post('/logout', routes.logout );
// Custom Activity Routes for interacting with Desk.com API
app.post('/ixn/activities/create-case/save/', activityCreate.save );
app.post('/ixn/activities/create-case/validate/', activityCreate.validate );
app.post('/ixn/activities/create-case/publish/', activityCreate.publish );
app.post('/ixn/activities/create-case/execute/', activityCreate.execute );
app.post('/ixn/activities/update-case/save/', activityUpdate.save );
app.post('/ixn/activities/update-case/validate/', activityUpdate.validate );
app.post('/ixn/activities/update-case/publish/', activityUpdate.publish );
app.post('/ixn/activities/update-case/execute/', activityUpdate.execute );
app.get('/clearList', function( req, res ) {
// The client makes this request to get the data
activityUtils.logExecuteData = [];
res.send( 200 );
});
// Used to populate events which have reached the activity in the interaction we created
app.get('/getActivityData', function( req, res ) {
// The client makes this request to get the data
if( !activityUtils.logExecuteData.length ) {
res.send( 200, {data: null} );
} else {
res.send( 200, {data: activityUtils.logExecuteData} );
}
});
app.get( '/version', function( req, res ) {
res.setHeader( 'content-type', 'application/json' );
res.send(200, JSON.stringify( {
version: pkgjson.version
} ) );
} );
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});