forked from davglass/yui-express
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_fugue.js
executable file
·506 lines (456 loc) · 17.5 KB
/
server_fugue.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
#!/usr/bin/env node
//Needed for monit/upstart
//Change directory into the script directory so includes resolve
process.chdir(__dirname);
var fugue = require('fugue'),
express = require('express'),
extras = require('express-extras'),
path = require('path'),
fs = require('fs'),
YUI = require('yui3').YUI,
TITLE = 'YUI/Express JS Demo',
DEBUG = true;
//Create the express application and allow the use of Spark (http://github.com/senchalabs/spark)
var app = module.exports = express.createServer();
/**
* Create the external instance that will host our "express" server.
* For a performance gain, you can "use" common modules here, so they
* are available when a new instance is created per request.
*/
YUI({ debug: true }).use('express', 'node', function(Y) {
//Configure it with some simple configuration options.
app.configure(function(){
//app.use(YUI.express);
app.use(extras.fixIP());
app.use(extras.throttle({ holdTime: 5 }));
app.use(express.favicon(__dirname + '/assets/favicon.ico'));
app.use(express.logger({ stream: fs.createWriteStream(__dirname + '/logs/access' + process.pid + '.log') }));
app.use(express.methodOverride());
app.use(express.bodyDecoder());
app.use(express.cookieDecoder());
app.use(express.conditionalGet());
app.use(express.cache());
app.use(express.gzip());
app.use(express.staticProvider(__dirname + '/assets'));
app.use(app.router);
});
//Set the development environment to debug, so YUI modues echo log statements
app.configure('development', function(){
DEBUG = true;
});
//Set the production environment to halt all debug logs.
app.configure('production', function(){
DEBUG = false;
});
/**
* This version of the YUIExpress engine comes with a simple YUI combo handler
* So you can put "use" inside your locals var on render:
*
* res.render('index.html', {
* locals: {
* use: ['dd', 'tabview']
* }
* });
*
* This will load a URL into the page like this:
* <script src="/combo?dd&tabview"></script>
*
* Note, currently it has to be "/combo", the internal renderer doesn't
* know what you set this to. Eventually we can add it to YUI.configure.
*/
app.get('/combo', YUI.combo);
/**
* This is the "black magic" part. This tells Express to use YUI to render
* all HTML pages
*/
app.register('.html', YUI);
//The same as other Express View Engines
//app.register('.html', require('jade'));
//app.register('.haml', require('haml-js'));
/**
* These partials will be added to every page served by YUI, good for templating.
* They can be added to by locals.partials on a per page basis. A partial looks like this:
* {
* name: 'header', //Name of the /views/partial/{name}.html file to load
* method: 'append', //append,prepend,appendChild
* node: '#conent', //Any valid selector
* enum: 'one', //one,all
* fn: function //The callback function to run after the action.
* }
* Defaults to enum: "one" and method: "append"
*/
YUI.partials = [
{
name: 'layout_head',
node: 'head'
},
{
test: function(Y, options) {
return (Y.UA.mobile);
},
name: 'mobile',
node: 'head'
},
{
ua: 'webkit',
name: 'webkit',
node: 'head'
},
{
test: function(Y, options) {
var u = Y.UA;
if (u.ie && u.ie < 8) {
return true;
}
if (options.scope.originalUrl === '/nag') {
return true;
}
return false;
},
name: 'nag',
node: 'body',
method: 'prepend',
fn: function(Y) {
Y.Get.css('/nag.css');
}
}
];
/**
* YUI.configure allows you to configure routes for the yui2 & yui3 assests.
* With this config you will serve yui2 assets from /yui2/ and yui3 assets from
* /yui3
*
*/
YUI.configure(app, {
yui2: '/yui2/',
yui3: '/yui3/'
});
/**
* The route controller for the default page: /
* This is a simple example of serving a static HTML page with a little
* Javascript to enhance the page.
*/
app.get('/', function(req, res) {
//Render from ./views/index.html
res.render('index.html', {
//Locals used by the YUI renderer
locals: {
/**
* This is the content placeholder in your ./views/layout.html file.
* The content of index.html will be inserted here.
*/
content: '#content',
/**
* Standard object hash to be passed to Y.Lang.sub after the
* content has been loaded, but before it's inserted into the YUI
* instance on render.
*/
sub: {
title: TITLE
},
/**
* The after method will be invoked after the layout.html file
* has been loaded into the instance. This allows you to change
* the total layout, after all the peices have been assembled.
*/
after: function(Y, options, partial) {
Y.one('title').set('innerHTML', TITLE);
Y.one('#nav li.home').addClass('selected');
}
}
});
});
/**
* Pages 1, 2 & 3 are all the same HTML page template.
* The simply change the content of that page after it's
* loaded into the YUI instance. They use some of the same
* properties as the one above, only they also use the "before" method.
*/
app.get('/one', function(req, res){
res.render('same.html', {
locals: {
content: '#content',
sub: {
title: TITLE
},
/**
* The before method is similar to the "after" method, only it's action is to
* only deal with the content of the partial that was loaded. It allows you
* to modify the partials HTML after that template was loaded into an instance.
*/
before: function(Y) {
Y.one('h1').set('innerHTML', 'Welcome to Page #1');
},
after: function(Y, options, partial) {
//Set the title of the page
Y.one('title').set('innerHTML', TITLE + ' :: Page #1');
//Grab the one for page one and add selected.
Y.one('#nav li.one').addClass('selected');
}
}
});
});
app.get('/two', function(req, res){
res.render('same.html', {
locals: {
content: '#content',
sub: {
title: TITLE
},
before: function(Y) {
Y.one('h1').set('innerHTML', 'Welcome to Page #2');
},
after: function(Y, options, partial) {
Y.one('title').set('innerHTML', TITLE + ' :: Page #2');
Y.one('#nav li.two').addClass('selected');
}
}
});
});
app.get('/three', function(req, res){
res.render('same.html', {
locals: {
content: '#content',
sub: {
title: TITLE
},
before: function(Y) {
Y.one('h1').set('innerHTML', 'Welcome to Page #3');
},
after: function(Y, options, partial) {
Y.one('title').set('innerHTML', TITLE + ' :: Page #3');
Y.one('#nav li.three').addClass('selected');
}
}
});
});
/**
* This handler does a couple special things.
* Uses an external module (CODE REUSE)
* Uses YUI2
* Handles user action from requests
*/
app.get('/calendar', function(req, res) {
YUI({
debug: DEBUG,
modules: {
'local-cal': {
//For more detail see this file..
fullpath: __dirname + '/modules/local-cal.js'
}
}
}).use('node', 'local-cal', function(page) {
//Calling a method inside an external module
page.localCal(req, res);
});
});
/**
* This handler is similar to the /calendar, but it renders a DataTable instead.
*/
app.get('/datatable', function(req, res) {
YUI({
debug: DEBUG,
modules: {
'local-dt': {
fullpath: __dirname + '/modules/local-dt.js'
}
},
}).use('node', 'local-dt', function(page) {
//Calling a method inside an external module
page.localDT(req, res);
});
});
var diggCache = null;
app.get('/digg', function(req, res) {
YUI({ debug: DEBUG }).use('node', 'io', function(page) {
var ul = page.one('body').addClass('digg').appendChild(page.Node.create('<ul></ul>'));
var sendRequest = function() {
res.render('digg.html', {
locals: {
instance: page,
content: '#content',
sub: {
title: TITLE
},
after: function(Y, options, partial) {
Y.one('title').set('innerHTML', TITLE + ' :: Digg Scrapping');
Y.one('#nav li.digg').addClass('selected');
}
}
});
};
if (diggCache) {
//console.log('Loading Digg from cache');
ul.set('innerHTML', diggCache);
sendRequest();
} else {
//console.log('Fetching News');
YUI({ debug: DEBUG }).use('node', function(remotePage) {
var url = 'http://digg.com/news';
//This will call io under the hood and get the content of the URL,
//It will then dump the content of that page into this sandboxed document.
remotePage.fetch(url, function() {
//Get all the news items from the remote page.
var newsItems = remotePage.all('#story-items h3');
//Iterate them
newsItems.each(function(n) {
//Import this "A" node into the outside instances document
var a = ul.importNode(n.one('a'), true);
//Clean up the relative URL's of hrefs
a.set('href', 'http://digg.com' + a.get('href'));
//Append the new node to the list
ul.appendChild(page.Node.create('<li></li>')).append(a);
});
diggCache = ul.get('innerHTML');
sendRequest();
setTimeout(function() {
//console.log('clear digg cache');
diggCache = null;
}, (1000 * 60 * 5));
});
});
}
});
});
app.get('/notice', function(req, res) {
res.render('notice.html', {
locals: {
content: '#content',
sub: {
title: 'This is a notice'
},
after: function(Y) {
Y.one('#nav').remove();
Y.one('#doc').replaceClass('yui-t1', 'yui-t7');
Y.one('title').set('innerHTML', TITLE + ' :: Full Page Notice');
}
}
});
});
app.get('/tabview', function(req, res) {
var selectedTab = 0;
if (req.cookies.tabview) {
selectedTab = req.cookies.tabview;
}
if (req.query.tab > -1) {
selectedTab = req.query.tab;
}
YUI({ debug: DEBUG }).use('tabview', 'yql', function(page) {
var div = page.Node.create('<div id="demo"></div>');
page.one('body').addClass('yui3-skin-sam').appendChild(div);
page.log('Creating the TabView from script..');
var tabview = new page.TabView({
children: [{
label: 'foo',
content: '<p>foo content</p>'
}, {
label: 'bar',
content: '<p>bar content</p>'
}, {
label: 'baz',
content: '<p>baz content</p>'
}]
});
tabview.render('#demo');
var as = page.all('#demo .yui3-tab-label');
as.each(function(v, k) {
v.set('href', '/tabview?tab=' + k);
});
if (selectedTab) {
tabview.selectChild(selectedTab);
}
res.render('tabview.html', {
locals: {
instance: page,
use: ['tabview', 'cookie'],
//filter: 'debug',
content: '#content',
sub: {
title: TITLE
},
after: function(Y, options, partial) {
Y.Get.domScript('/tabview.js');
Y.one('title').set('innerHTML', TITLE + ' :: YUI 3.x TabView');
Y.one('#nav li.tabview').addClass('selected');
}
}
});
});
});
app.get('/github/:id?', function(req, res) {
var sql = "select * from github.repo.network where id='yui' and repo='yui3'";
if (req.params.id) {
sql = Y.Lang.sub("select * from github.user.info where id='{id}'", req.params);
}
YUI({ debug: DEBUG }).use('yql', 'node', function(page) {
var title = 'Github YUI Followers';
page.YQL(sql, function(r) {
if (r.query.results.user) {
var parts = res.partial('github_member.html');
title = r.query.results.user.name;
parts = page.Lang.sub(parts, r.query.results.user);
page.one('body').append(parts);
var lis = page.all('#member li');
lis.each(function(n) {
//Need a sub method that fixes this stuff...
if (n.get('innerHTML').indexOf('Object') > -1) {
n.remove();
}
if (n.get('innerHTML').indexOf('{') > -1) {
n.remove();
}
if (n.get('innerHTML').indexOf('null') > -1) {
n.remove();
}
});
} else {
var parts = res.partial('github_list.html');
var ul = page.Node.create('<ul></ul>');
page.one('body').append(ul);
page.each(r.query.results.network.network, function(d) {
ul.append(page.Lang.sub(parts, d));
});
}
res.render('github.html', {
locals: {
instance: page,
content: '#content',
sub: {
title: 'YUI3 Github Network'
},
after: function(Y) {
Y.one('title').set('innerHTML', TITLE + ' :: YUI3 Github Network');
Y.one('#content h1').set('innerHTML', title);
Y.one('#nav li.github').addClass('selected');
}
}
});
});
});
});
app.get('/pre', YUI.express({ render: 'pre.html', locals: {} }), function(req, res, next) {
req.Y.one('doc').set('title', TITLE + ' :: Pre-Render Layout');
req.Y.one('#nav li.pre').addClass('selected');
res.sub({
title: 'Pre-Render Layout'
});
res.send();
});
app.get('/ua', YUI.express({ render: 'ua.html', locals: {} }), function(req, res, next) {
req.Y.one('doc').set('title', TITLE + ' :: Dynamic Partials');
req.Y.one('#nav li.ua').addClass('selected');
res.sub({
title: 'Dynamic Partials'
});
res.send();
});
app.get('/nag', YUI.express({ render: 'nag.html', locals: {} }), function(req, res, next) {
req.Y.one('doc').set('title', TITLE + ' :: Nag Bar');
req.Y.one('#nav li.nag').addClass('selected');
res.sub({
title: 'Nag Bar'
});
res.send();
});
});
fugue.start(app, 3200, null, 10, { daemonize: true, working_path: __dirname, verbose: false, master_pid_path: '/tmp/yui-express.pid' });