This repository has been archived by the owner on Sep 28, 2018. It is now read-only.
/
server.js
104 lines (94 loc) · 2.54 KB
/
server.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
/**
* Sunrise - HTTP Server extension
*
* Copyright (c) Yohei Sasaki <yssk22@gmail.com>
* MIT Licensed
*/
var join = require('path').join;
var merge = require('./utils').merge,
clone = require('./utils').clone,
i18n = require('./i18n').i18n,
db = require('./db');
var express = require('express');
/**
* Sunrise server constructor. A server object has common features.
*
* - server.namespace : current application namespace
* - server.db : database connection instance.
* - server.logger : loggger instance
* - server.i18n : i18n instance
*
* Sunrise site and applications are based on this `server`.
*
*/
function constructor(options, middleware){
options = merge({
database: 'sunrise',
locale: 'en'
}, options);
// common features
this._logger = require('./utils').getLogger(options.database);
this._i18n = new i18n(options.locale); // TODO default lang
this._db = db.createConnection(options.database);
};
function defineProperties(klass){
/**
* Return the database connection
*
* @return {Object}
* @api public
*/
klass.prototype.__defineGetter__('db', function(){
return this._db;
});
/**
* Return the i18n object.
*
* @return {String}
* @api public
*/
klass.prototype.__defineGetter__('i18n', function(){
return this._i18n;
});
/**
* Return the logger object.
*
* @return {String}
* @api public
*/
klass.prototype.__defineGetter__('logger', function(){
return this._logger;
});
/**
* Return the namespace path.
*
* @return {String}
* @api public
*/
klass.prototype.__defineGetter__('namespace', function(){
return this._namespace;
});
}
var HTTPServer = function HTTPServer(options, middleware){
express.HTTPServer.call(this, middleware);
constructor.call(this, options, middleware);
};
HTTPServer.prototype.__proto__ = express.HTTPServer.prototype;
defineProperties(HTTPServer);
var HTTPSServer = function HTTPSServer(options, middleware){
express.HTTPSServer.call(this, options, middleware);
constructor.call(this, options, middleware);
};
HTTPSServer.prototype.__proto__ = express.HTTPSServer.prototype;
defineProperties(HTTPSServer);
exports.HTTPServer = HTTPServer;
exports.HTTPSServer = HTTPSServer;
exports.createServer = function(){
var options = arguments[0];
if( typeof(options) == 'object' ){
var cstr = options.ssl ? HTTPSServer : HTTPServer;
return new cstr(options, Array.prototype.slice.call(arguments, 1));
} else {
return new HTTPServer(Array.prototype.slice.call(arguments));
}
};