forked from yujiroarai/node-wao
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
542 lines (529 loc) · 21.3 KB
/
app.js
File metadata and controls
542 lines (529 loc) · 21.3 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
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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
// 使用するモジュールの定義
var http = require('http'),
conf = require('config'),
url = require('url'),
path = require('path'),
querystring = require('querystring'),
fs = require('fs'),
mime = require('mime'),
jsdom = require("jsdom"),
$ = require("jquery")(jsdom.jsdom().createWindow()),
mongo = require('mongodb');
// jQueryの拡張
(function($) {
// 自分自身のhtmlソースを返却する
$.fn.selfHtml = function(options){
if($(this).get(0)){
return $(this).get(0).outerHTML;
}
}
})($);
var WaoAppFactory = function() {
var appname,
appdir,
dbname,
port;
return {
createHttpServer : function(appname, port) {
this.appname = appname;
this.port = port;
this.dbname = appname + '_' + port;
this.appdir = appname + '_' + port;
var myWaoApp = this;
var s = http.createServer(function(request, response) {
console.log('Start http process.');
console.log(' URL="' + request.url + '", method="' + request.method + '"');
console.log('');
var waoPage = WaoPageFactory();
waoPage.init(myWaoApp.dbname, request.url, function(err, data) {
// methodで処理を切り分け
// PUTメソッド・・・データの変更
// DELETEメソッド・・・データの削除
// POSTメソッド・・・データの追加
waoPage.postData(request, function(err, data) {
if (err) throw err;
// GETメソッド・・・データの取得
waoPage.getData(request, function(err, data) {
if (err) throw err;
// テンプレートを読み込む
waoPage.readTemplate(myWaoApp.appdir, request, function(err, data) {
if (err) throw err;
// レスポンス出力
var status = waoPage.getStatusCode();
var mimeType = waoPage.getMimeType();
if (status == 200) {
console.log('Start response. statusCode=' + status + ', mimeType="' + mimeType + '"');
if (mimeType == 'image/png') {
console.log();
var path = waoPage.getLocalFilePath();
var stat = fs.statSync(path);
response.writeHead(200, {
'Content-Type' : mimeType,
'Content-Length': stat.size
});
fs.createReadStream(path).pipe(response);
} else {
response.writeHead(status, { 'Content-Type': mimeType });
response.end(waoPage.getResponseData());
}
} else if (status == 301) {
var redirectUrl = 'http://localhost:' + myWaoApp.port + waoPage.getRedirectUrl(); // TODO:ホスト名とか
console.log('Start response. statusCode=' + status + ', Location="' + redirectUrl + '"');
response.writeHead(status, { 'Location': redirectUrl });
response.end();
} else {
console.log('Start response. statusCode=' + status);
response.statusCode = (status);
response.end();
}
console.log('------------------------------');
console.log('');
});
});
});
});
});
s.listen(port);
listenPorts.push(port);
return s;
}
};
}
var listenPorts = [];
var waoApp = WaoAppFactory();
waoApp.createHttpServer(conf.waoApp.appName, conf.waoApp.port);
var WaoPageFactory = function() {
var responseData,
mimeType,
redirectUrl,
statusCode,
localFilePath,
db,
crudData;
return {
init : function(dbname, request_url, callback) {
var url_parts = url.parse(request_url, true);
this.crudData = { find : {}, insert: {} };
this.findData = {};
var that = this;
// データベースへの接続
var dbconnect = function() {
// TODO:何でもかんでもつなぎにいっちゃうバカなやつ
var mongoServer = new mongo.Server('localhost', mongo.Connection.DEFAULT_PORT, {});
that.db = new mongo.Db(dbname, mongoServer, {});
that.db.open(function(err, db) {
console.log('Success to open db connection. dbname=' + dbname);
callback(null, null);
});
};
if (url_parts.query.hasOwnProperty('_FILE.path')) {
// ディレクトリの情報を取得
WaoPageFactory().readDirRecursive('./templates/' + url_parts.query['_FILE.path'], function(fileList) {
that.findData._FILE = fileList;
dbconnect();
});
} else {
dbconnect();
}
},
// POSTメソッドの処理(データの追加)
// TODO:リファラからテンプレートを選択してフォームの整合性をチェックする処理を追加する
// ・余計なフォームが送信されて来ていないか?
// ・html5 form validationと同じ内容のサーバー側バリデーション処理
postData : function(request, callback) {
if (request.method == 'POST') {
var me = this;
var data = '';
request.on('data', function(chunk) {
data += chunk;
});
request.on('end', function() {
var collectionName;
// POSTデータをJSON化
var query = querystring.parse(data);
// JSON化したPOSTデータをmongoDBに入れられるJSON形式に変換
for (var key in query) {
// <input name="collactionName.propertyName">
collectionName = key.match(/^([^.]+)\./)[1]; // TODO:collectionの決定方法がアホ
if (collectionName == '_APP') {
// アプリ起動オプション
switch (query[key]){
case 'start()':
var appProp = key.replace(collectionName + '.', '').split(':');
if (appProp[0] != 'undefined' && appProp[1] != 'undefined') {
if ($.inArray(appProp[1], listenPorts) < 0) {
var waoApp = WaoAppFactory();
waoApp.createHttpServer(appProp[0], appProp[1]);
}
}
break;
default :
break;
}
} else {
if (!me.crudData.insert[collectionName]) {
var createdDate = new Date().getTime();
me.crudData.insert[collectionName] = {
id: (createdDate + Math.random() + '').replace('.', ''),
_createdDate: createdDate
}
}
me.crudData.insert[collectionName][key.replace(collectionName + '.', '')] = query[key];
}
}
// mongoDBにデータを登録
var colCount = 0;
var maxColCount = Object.keys(me.crudData.insert).length;
if (maxColCount == 0) {
callback(null, null);
} else {
for (var colName in me.crudData.insert) {
me.db.collection(colName, function(err, collection) {
collection.insert(me.crudData.insert[colName], {w:1}, function() {
console.log('WaoPage.postData() : Success to insert a new data.');
console.log(me.crudData.insert[colName]);
console.log('');
colCount++;
if (colCount == maxColCount) {
callback(null, null);
}
});
});
}
}
});
} else {
callback(null, null);
}
},
// GETメソッドあるいはクエリストリングの処理(データの取得)
// TODO:postData()ともう少し共通化できると思う
getData : function(request, callback) {
// GETメソッドじゃなくてもクエリストリングの解析処理を行う
var me = this;
var url_parts = url.parse(request.url, true);
var collectionName;
// GETパラメタ
var query = url_parts.query;
// GETパラメタをmongoDBの検索条件に指定できるJSON形式に変換
for (var key in query) {
if (key.match(/^([^.]+)\./)) {
// ?collactionName.propertyName=xxxx
collectionName = key.match(/^([^.]+)\./)[1]; // TODO:collectionの決定方法がアホ
if (!me.crudData.find[collectionName]) me.crudData.find[collectionName] = {};
me.crudData.find[collectionName][key.replace(collectionName + '.', '')] = query[key];
} else {
// ?collactionName=all
// 検索条件なし
collectionName = key;
if (!me.crudData.find[collectionName]) me.crudData.find[collectionName] = {};
}
}
// mongoDBからデータを取得
var colCount = 0;
var maxColCount = Object.keys(me.crudData.find).length;
if (maxColCount == 0) {
callback(null, null);
} else {
for (var colName in me.crudData.find) {
if (colName == '_FILE') {
colCount++;
if (colCount == maxColCount) {
callback(null, null);
}
} else {
me.db.collection(colName, function(err, collection) {
var findColName = colName;
collection.find(me.crudData.find[colName]).toArray(function(err, docs) {
console.log('WaoPage.getData() : Success to find data[s].');
console.log(' findColName = "' + findColName + '"');
console.log(me.crudData.find[colName]);
console.log(' count=' + docs.length);
me.findData[findColName] = [];
for (var i = 0; i < docs.length; i++) {
console.log(docs[i]);
me.findData[findColName][i] = docs[i];
}
console.log('');
colCount++;
if (colCount == maxColCount) {
callback(null, null);
}
});
});
}
}
}
},
// URLルーティングの処理
// TODO:expressに置き換えるか検証・検討
// → expressだとURLパターンごとに関数を用意しなければならない?
getFilePathByURL : function(urlpath) {
var urlElements = url.parse(urlpath, true);
var filepath = urlElements.pathname;
// URLが"/"で終わる場合はindex.htmlを開く
if (filepath.match(/\/$/)) {
filepath += 'index.html';
}
return filepath;
},
// テンプレートを選択する処理
readTemplate : function(appdir, request, callback) {
var me = this;
var filepath = this.getFilePathByURL(request.url);
me.db.close();
console.log('Success to close db connection.');
console.log();
var path = conf.basepath + '/' + appdir + filepath;
me.mimeType = mime.lookup(filepath);
if (me.mimeType == 'image/png') {
me.statusCode = 200;
me.responseData = null;
me.localFilePath = path;
callback(null, null);
} else {
fs.readFile(conf.basepath + '/' + appdir + filepath, 'utf8', function(err, data){
if (err) {
console.log('WaoPage.readTemplate() : Faild to load this template.');
console.log(' filepath = ' + filepath);
console.log('');
me.statusCode = 404;
} else {
console.log('WaoPage.readTemplate() : Success to load this template.');
console.log(' filepath = ' + filepath);
console.log('');
me.statusCode = 200;
me.responseData = data;
me.localFilePath = null;
if (me.mimeType == "text/html") {
// <meta http-equiv="refresh">の指定があればサーバー側で301リダイレクトする
var $dom = $(data); // TODO:<!DOCTYPE>に対応できてない & parseできない時の処理が必要
var $redirect = $dom.find('meta[http-equiv=refresh]');
if ($redirect) {
var contextMatch = ($redirect.attr('content') + "").match(/^[0-9]+;[ ]+URL=(.*)$/);
if (contextMatch) {
// TODO:URLの組み立てがすげー中途半端
var basepath = request.url.match(/^(.*\/)[^\/]+\.html$/)[1];
var target = contextMatch[1].replace('./',''); // TODO:これがヤバい
me.statusCode = 301;
me.redirectUrl = basepath + me.bindGetParam(target, 0, false);
} else {
me.bind();
}
}
}
}
callback(null, null);
});
}
},
// データバインド処理
bind : function() {
var $dom = $(this.responseData); // TODO:<!DOCTYPE>に対応できてない & parseできない時の処理が必要
var me = this;
// GETパラメタにバインド
$dom.find('a').each(function(){
if ($(this).attr('href') != null) {
$(this).attr('href', me.bindGetParam($(this).attr('href'), 0, false));
}
});
$dom.find('form').each(function(){
if ($(this).attr('action') != null) {
$(this).attr('action', me.bindGetParam($(this).attr('action'), 0, false));
}
});
// data-wao-bind属性にバインド(innerHtml)
$dom.find('[data-wao-bind]').each(function(){
var val = $(this).attr('data-wao-bind');
var col = val.split('.')[0];
var prop = val.split('.')[1];
// data-wao-iteratorに指定されている場合、除外する
if ($(this).closest('[data-wao-iterator="'+ col + '"]').length == 0) {
// TODO:属性に対応
$(this).html(me.getValue(col, prop, 0));
$(this).removeAttr('data-wao-bind');
}
});
// 属性のバインド(data-wao-bind-foobar="col.prop")
var bindAttrList = $dom.html().match(/data-wao-bind-[^=]+/g); // まずは属性を抜き出す
if (bindAttrList != null) {
bindAttrList = bindAttrList.filter(function (x, i, self) { // 重複データは削除
return self.indexOf(x) === i;
});
for (var idx in bindAttrList) {
var bindAttr = bindAttrList[idx];
console.log('bind() : attribute_name=' + bindAttr);
$dom.find('[' + bindAttr + ']').each(function(){
var val = $(this).attr(bindAttr);
var col = val.split('.')[0];
var prop = val.split('.')[1];
// data-wao-iteratorに指定されている場合、除外する
if ($(this).closest('[data-wao-iterator="'+ col + '"]').length == 0) {
$(this).attr(bindAttr.replace('data-wao-bind-',''), me.getValue(col, prop, 0));
$(this).removeAttr(bindAttr);
}
});
}
}
// data-wao-iterator配下のbind処理
$dom.find('[data-wao-iterator]').each(function(){
var col = $(this).attr('data-wao-iterator');
var iteratorTag = $(this).clone();
var appendedTag = $(this);
for (var i = 0; i < me.findData[col].length; i++) {
// GETパラメタにバインド
appendedTag.find('a').each(function(){
if ($(this).attr('href') != null) {
$(this).attr('href', me.bindGetParam($(this).attr('href'), i, true));
}
});
appendedTag.find('form').each(function(){
if ($(this).attr('action') != null) {
$(this).attr('action', me.bindGetParam($(this).attr('action'), i, true));
}
});
// data-wao-bind属性にバインド(innerHtml)
appendedTag.find('[data-wao-bind]').each(function(){
var val = $(this).attr('data-wao-bind');
var col = val.split('.')[0];
var prop = val.split('.')[1];
// TODO:属性に対応
$(this).html(me.getValue(col, prop, i));
$(this).removeAttr('data-wao-bind');
});
if (i + 1 < me.findData[col].length) {
appendedTag = iteratorTag.clone().appendTo($(this).parent());
}
}
$(this).removeAttr('data-wao-iterator');
});
// TODO:each(function(){})を使ってるから同期が必要じゃ???
this.responseData = $dom.selfHtml();
},
// URLに指定されたGETパラメタにデータをバインドする処理
bindGetParam : function(target, index, isIterator) {
// ?hoge=hoge&hoge=hoge....部分を抜き出す
var getparam = target.match(/\?([^=]+=([^&]*)?&?)*/);
var getparamR = '';
if (getparam) { // URLにGETパラメタが含まれていれば
getparamR = '?';
getparam = getparam[0].replace(/^\?/,'').split('&'); // hoge=hogeに分割して配列化
for (var i in getparam) {
var paramName = getparam[i].split('=')[0];
var paramValue = getparam[i].split('=')[1];
// 既にbindされている場合は、対象外とする
if (paramValue.trim() == '') {
var trg = {
collectionName: paramName.split('.')[0],
propertyName: paramName.split('.')[1]
};
// data-wao-iteratorに指定されている場合、除外する
var val = '';
if (!isIterator) {
if (this.findData[trg.collectionName] && this.findData[trg.collectionName].length == 1) {
val = this.getValue(trg.collectionName, trg.propertyName, index);
}
if (this.crudData.insert[trg.collectionName]) {
val = this.getValue(trg.collectionName, trg.propertyName, index);
}
} else {
val = this.getValue(trg.collectionName, trg.propertyName, index);
}
getparamR += paramName + '=' + val + '&';
} else {
getparamR += paramName + '=' + paramValue + '&';
}
}
getparamR = getparamR.slice(0, -1);
}
return target.replace(/\?([^=]+=([^&]*)?&?)*/, '') + getparamR;
},
// 参照文字列から自動的に適切なオブジェクトを選択して、参照文字列に対応するデータを返却する
getValue : function(col, prop, index) {
// TODO:_DBとか_SESって修飾子がついていたら・・・的な処理が今後必要
var val = '(データが見つかりません)';
var key = 'undefined';
if (col == '_FILE') {
key = '_FILE';
val = JSON.stringify(this.findData[col]);
} else {
if (this.findData[col] && this.findData[col][index] && this.findData[col][index][prop]) {
key = '_DB.' + col + '.' + prop;
val = this.findData[col][index][prop];
} else if (this.crudData.insert[col] && this.crudData.insert[col][prop]) {
key = '_DB.' + col + '.' + prop;
val = this.crudData.insert[col][prop];
}
}
console.log('getValue() : ' + key + '=' + val);
return val;
},
// レスポンス出力処理
getResponseData : function() {
// TODO DOCTYPEが無いとCSSが崩れることがあるので・・・ここも暫定対策
var doctype = (this.getMimeType() == 'text/html') ? '<!DOCTYPE html>\n' : '';
return doctype + this.responseData;
},
// HTTPステータスコードを返却する
getStatusCode : function() {
return this.statusCode;
},
// MIMEタイプを返却する
getMimeType : function() {
return this.mimeType;
},
// リダイレクト先URLを返却する
getRedirectUrl : function() {
return this.redirectUrl;
},
// リソースのローカル上のパスを返却する
getLocalFilePath : function() {
return this.localFilePath;
},
// ディレクトリを再帰的に読み込んで配列で返す
readDirRecursive : function(dir, callback) {
var walk = function(p, callback) {
var results = [];
fs.readdir(p, function (err, files) {
if (err) callback(err, []);
var pending = files.length;
if (!pending) return callback(null, results);
files.map(function (file) {
return path.join(p, file);
})
.filter(function (file) {
if(fs.statSync(file).isDirectory()) {
walk(file, function(err, res) {
var stat = fs.statSync(file);
results.push({
name: path.basename(file),
files:res,
type: 'directory',
time: stat.mtime
});
if (!--pending) {
callback(null, results);
}
});
}
return fs.statSync(file).isFile();
})
.forEach(function (file) {
var stat = fs.statSync(file);
results.push({
name: path.basename(file),
type: 'file',
time: stat.mtime,
mime: mime.lookup(path.basename(file))
});
if (!--pending) callback(null, results);
});
});
};
walk(dir, function(err, results) {
if (err) console.log('err: ' + err);
callback({ file_list: results });
});
}
};
};