-
Notifications
You must be signed in to change notification settings - Fork 0
/
protolus-combined.js
157 lines (155 loc) · 6.69 KB
/
protolus-combined.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
//todo: events support
var ext = require('prime-ext');
var prime = ext(require('prime'));
var Class = require('Classy');
var type = require('prime/util/type');
var string = ext(require('prime/es5/string'));
var array = ext(require('prime/es5/array'));
var fn = require('prime/es5/function');
var regexp = require('prime/es5/regexp');
var Emitter = require('prime/util/emitter');
var fs = require('fs');
var Options = require('prime-ext/options');
var Registry = require('prime-ext/registry');
var Filters = require('prime-ext/filters');
var InternalWorker = require('prime-ext/internal-worker');
var Smarty = require('tag-template/smarty');
var NPMtrospect = require('npm-trospect');
var Protolus = {};
Protolus.Router = require('protolus-router');
Protolus.Resource = require('protolus-resource');
require('protolus-resource/handler-js');
require('protolus-resource/handler-css');
Protolus.Templates = require('protolus-templates');
Protolus.Application = require('protolus-application');
Protolus.routes = 'App/routes.conf';
var TLRQ = require;
Protolus.PanelServer = function(options){
if(!options) options = {};
if(!options.environment) options.environment = 'production';
var router = new Protolus.Router({
ini : Protolus.routes,
passthru : true
});
Protolus.Templates.set({
base : (options.base || process.cwd()),
templateDirectory : '/App/Panels',
scriptDirectory : '/App/Controllers'
});
var registry = new Protolus.Resource.Registry();
var requirer = {working : false};
var renderHeadAndReturnPage = function(html, request, response, headOptions, path){
Protolus.Resource.head(headOptions, function(tags){
console.log('rendered', path);
var pageTags = tags.join("\n ");
if(pageTags) html = Protolus.Templates.insertTextAtTarget(pageTags, 'head', html);
response.end(html);
});
};
Smarty.registerMacro('require', function(node){
if(node.attributes.name == undefined) throw('require macro requires \'name\' attribute');
var resources = node.attributes.name.split(',');
requirer.working = true;
array.forAllEmissions(resources, function(resource, index, rtrn){
Protolus.Resource.import(resource, registry, function(){
rtrn();
});
}, function(){
if(type(requirer.working) == 'function') requirer.working();
requirer.working = false;
});
//todo:inline
//todo: handle in-browser
return '';
});
var application = new Protolus.Application.WebServer({
port : (options.port || 80),
onServe : function(request, response, connection){
Protolus.Resource.handle(request, response, function(){
if(request.parts.path.substring(-1) == '/') request.parts.path += 'index';
var location = request.parts.path.substring(1); // strip leading slash
router.route(location, request.method.toUpperCase(), function(routedLocation){
if(Protolus.Templates.Panel.exists(routedLocation)){
Protolus.Templates.renderPage(routedLocation, {
onSuccess : function(html){
if(!requirer.working) renderHeadAndReturnPage(html, request, response, {
dependencies : true,
compact : true,
registry : registry
}, routedLocation);
else requirer.working = function(){
renderHeadAndReturnPage(html, request, response, {
dependencies : true,
compact : true,
registry : registry
}, routedLocation);
}
}, env : {
application : connection
}
});
}else{
fs.exists(routedLocation, function(exists){
if(!exists){
response.end('OMG 404');
}else{
fs.readFile(routedLocation, function(err, data){
response.end(data);
})
}
});
}
});
});
}
});
application.addJob();
var configurationFile = 'Configuration/'+options.environment+'.private.json';
application.loadConfiguration(configurationFile, function(conf){
if(Protolus.Data){
prime.filter(application.configurations, function(key){
return key.indexOf('DB:') === 0;
}, function(databasesToRegister){
prime.each(databasesToRegister, function(options, key){
var name = key.substring(3);
var db;
//todo: allow sessions to be linked to datasources
switch(options.type){
case 'mongo':
if(!Protolus.Data.Source.Mongo) Protolus.Data.loadSource('Mongo');
db = new Protolus.Data.Source.Mongo(options);
break;
case 'mysql':
if(!Protolus.Data.Source.MySQL) Protolus.Data.loadSource('MySQL');
db = new Protolus.Data.Source.MySQL(options);
break;
//todo: more types
default : throw('unknown datasource type: '+options.type);
}
Protolus.Data.sources[name] = db;
//console.log('name', db);
})
});
}
application.removeJob();
});
return application;
};
Protolus.internalRequire = function(topLevelRequire){
NPMtrospect.require = topLevelRequire;
if(Protolus.Data) Protolus.Data.internalRequire = topLevelRequire;
Protolus.Templates.internalRequire = topLevelRequire;
Protolus.Resource.internalRequire(topLevelRequire);
TLRQ = topLevelRequire;
};
if(!GLOBAL.Protolus) GLOBAL.Protolus = Protolus;
Protolus.with = function(module){
switch(module.toLowerCase()){
case 'data':
Protolus.Data = require('protolus-data');
break;
default : throw('unknown protolus module: '+module);
}
return Protolus;
};
module.exports = Protolus;