-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
187 lines (163 loc) · 4.34 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// dependencies
var express = require( 'express' ),
http = require( 'http' ),
app = express(),
favicon = require( 'serve-favicon' ),
swig = require( 'swig' ),
markedSwig = require( 'swig-marked' ),
swigify = require( 'swigify' ),
marked = require( 'marked' ),
randomstring = require( 'randomstring' ),
_ = require( 'underscore' ),
fs = require( 'fs' ),
livefyre = require( 'livefyre' );
// templating
markedSwig.useFilter( swig );
markedSwig.useTag( swig );
app.engine( 'html', swig.renderFile );
app.set( 'views', __dirname + '/views' );
app.set( 'view engine', 'html' );
// configure environments
var port = process.env.PORT || 9999;
swig.setDefaults( {
cache: false
} );
if ( 'development' === app.get( 'env' ) ) {
// disable cache
app.disable( 'view cache' );
// enable livereload
app.use( require( 'connect-livereload' )( {
port: 99991
} ) );
} else {
app.enable( 'view cache' );
}
// static files
app.use( express.static( __dirname + '/public' ) );
app.use( favicon( __dirname + '/public/img/favicon.ico' ) );
// data: navigation
app.locals = _.extend( app.locals, {
menu: [ {
label: 'Elements',
uri: '/elements'
}, {
label: 'Components',
uri: '/components'
}, {
label: 'Patterns',
uri: '/patterns'
} ],
bodyClass: 'nav-closed',
uuid: randomstring.generate( 12 ),
organization: "MIT Technology Review"
} );
/**
* Dynamic URL Routing
*
* Wildcard handler for all site pages, where thse
* URL path corresponds to a view template.
*/
app.get( '/*', function ( req, res ) {
// what view should we use based on the current route?
var viewPath = app.get( 'views' );
var viewTemplate = ( req.url === '/' ) ? 'index' : req.url.substring( 1 );
var templatePath = viewPath + '/' + viewTemplate + '.html';
var viewTitle = ( req.url === '/' ) ? 'home' : req.url.substring( 1 );
// simple layout
if ( req.url === '/' ) {
res.render( 'layout-simple', {
view: './index.html',
activeRoute: req.url,
title: viewTitle
} );
}
// livefyre-sidenotes
else if ( req.url === '/livefyre-sidenotes' ) {
// livefyre options
var livefyreOpts = {
networkName: "technologyreview.fyre.co",
networkKey: "rEKnOyNhCQq9YmDKkuXgI+V51kw=",
siteId: "296821",
siteKey: "lOBTfAoFgehGXT/1pbxINIH/Rr8=",
title: 'Sample livefyre-sidenotes article',
url: 'http://styleguide.technologyreview.com/livefyre-sidenotes',
articleId: '12345678911',
drupalUserID: 'uid_1048196',
drupalDisplayName: 'erik.pelletier'
};
// livefyre collection meta token generation
if ( livefyreOpts.siteKey && livefyreOpts.networkKey ) {
var network = livefyre.getNetwork( livefyreOpts.networkName, livefyreOpts.networkKey );
var site = network.getSite( livefyreOpts.siteId, livefyreOpts.siteKey );
// collection
var collection = site.buildCommentsCollection(
livefyreOpts.title,
livefyreOpts.articleId,
livefyreOpts.url
);
var collectionMetaToken = collection.buildCollectionMetaToken();
// user auth
var userAuthToken = network.buildUserAuthToken( livefyreOpts.drupalUserID, livefyreOpts.drupalDisplayName, 9999999999 );
} else {
var collectionMetaToken = '***UNSET***';
}
// output template
res.render( 'layout-livefyre-sidenotes', {
activeRoute: req.url,
title: viewTitle,
server: JSON.stringify( {
collectionMetaToken: collectionMetaToken,
networkName: livefyreOpts.networkName,
siteId: livefyreOpts.siteId,
articleId: livefyreOpts.articleId,
userAuthToken: userAuthToken
}, null, "\t" )
} );
}
// defaults
else {
// does the view template for this route exist? if not serve an error page
fs.exists( templatePath, function ( exists ) {
if ( exists ) {
res.render( 'layout-default', {
view: viewTemplate + '.html',
activeRoute: req.url,
title: viewTitle
} );
} else {
res.render( 'layout-error', {
view: './error-404.html',
activeRoute: req.url,
title: viewTitle
} );
}
} );
}
} );
/**
* Error pages
*/
// 500
app.use( function ( error, req, res, next ) {
res.status( 500 );
// HTML
if ( req.accepts( 'html' ) ) {
res.render( 'layout-simple', {
view: './error-500.html'
} );
return;
}
// JSON
if ( req.accepts( 'json' ) ) {
res.send( {
error: 'Internal Server Error'
} );
return;
}
// Plain-text
res.type( 'txt' ).send( 'Internal Server Error' );
} );
/**
* Virtual Host
*/
app.listen( port );