This repository has been archived by the owner on Sep 28, 2018. It is now read-only.
/
app.js
168 lines (152 loc) · 4.25 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
/**
* Sunrise - Application server, CouchApp x Node.js Integration
*
* Copyright (c) Yohei Sasaki <yssk22@gmail.com>
* MIT Licensed
*/
var path = require('path'),
fs = require('fs');
var staticProvider = require('express').static;
var compiler = require('./middleware/compiler');
var clone = require('./utils').clone,
merge = require('./utils').merge,
abspath = require('./utils').abspath,
parallel = require('./utils').parallel;
var createServer = require('./server').createServer;
var APP_PATH = [abspath(__dirname + '/../apps')];
/**
* Application search path.
* This can be congigured by '-a path' option on booting.
*/
exports.paths = APP_PATH;
/**
* Resolve the given application path
*
* @param {String} apppath the path to CouchApp directory.
* @param {Function} callback
* @api public
*/
exports.resolveAppPath = function(apppath){
if( apppath[0] == '/' ){
// absolute path
if( path.existsSync(apppath) ){
return path.normalize(apppath);
}else{
throw new Error(apppath + ' is not found.');
}
}else{
// relative path
// search the path from APP_PATH.
for(var i=0, len=APP_PATH.length; i<len; i++){
var dir = path.join(APP_PATH[i], apppath);
if( path.existsSync(dir) ){
return path.normalize(dir);
}
}
throw new Error(apppath + ' is not found in ' + JSON.stringify(APP_PATH));
}
};
exports.createAppFromDesignDoc = function(db, designPath, options, callback){
// TODO: create application instance from existing design document.
throw new Error('TBD');
};
/**
* Create a new sunrise application
*
* @param apppath {String} application path.
* @param options {Object} installation option. (TBD)
* @param callback {Function} callback function called after the application created.
**/
exports.createApp = function(apppath, options, callback){
if( typeof(options) == 'function' ){
callback = options;
options = {};
}
if( callback == undefined ){
callback = function(){};
}
apppath = exports.resolveAppPath(apppath);
return makeApp(apppath, options || {}, callback);
};
function makeApp(apppath, options){
var rootUrl = options.rootUrl || '/';
if( rootUrl[0] != '/' ){
throw new Error('rootUrl must start with "/"');
}
if( rootUrl[rootUrl.length-1] != '/' ){
rootUrl = rootUrl + '/';
}
var app = createServer(options);
var ddoc = require(path.join(apppath, 'app.js'));
app.documentRoot = apppath;
app.ddoc = ddoc;
app.init = function(options){
if( app._initialized ){
throw new Error("Duplicate initialization");
}else{
if( typeof(ddoc.init) == 'function' ){
ddoc.init(app, options);
}
app._initialized = true;
}
};
app.deploy = function(cb){
var couchapp = require('./sunrise').couchapp;
var url = app.db.url;
couchapp.createApp(ddoc, url, function(a){
app.db.exists(function(err, exists){
if( err ){
cb(err);
}else{
if( exists ){
a.push(cb);
}else{
app.db.create(function(err, res){
if( err ){
cb(err);
}else{
a.push(cb);
}
});
}
}
});
});
};
app.middleware = {
page: require('./middleware/page')(app),
db: require('./middleware/db')(app)
};
// setup middelwares
if( options.middlewares ){
options.middlewares.forEach(function(m){
app.use(m);
});
}
app.use(app.router);
// directory settings
var dirs = merge({
templates: "_attachments/templates",
errors: "_attachmenets/errors",
m10n: "_attachments/messages",
static: "_attachments/",
}, ddoc.dirs || {});
// templates
for(var i in dirs){
dirs[i] = path.join(app.documentRoot, dirs[i]);
}
app.use(compiler({src: dirs.static, enable: ['less']}));
app.use(staticProvider(dirs.static));
app.set('views', dirs.templates);
app.set('hints', false);
app.set('errors', dirs.errors);
app.set('view engine', 'ejs');
if( path.existsSync(dirs.m10n) ){
app.i18n.installMessages(dirs.m10n);
app.logger.debug("Message resource installed: " + dirs.m10n);
}else{
app.logger.warn("Message resources are not found: " + dirs.m10n);
}
// static file hosting
return app;
};