-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.js
273 lines (216 loc) · 9.51 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
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
"use strict";
//SQL MVC SocketStream 0.3 app
var http = require('http');
var ss = require('socketstream'); //without var this become global( and visible without declaration in other modules), with var it is local
var db = require("./server/database/DatabasePool");
var path = require('path');
var fs = require('fs');
var app_utils = require("./server/lib/app_utils");
var app_uploads = require("./server/lib/app_uploads");
var Busboy = require('busboy');
var json_like = require("./server/lib/json_like");
var zxGase;
var ServerProcess = require("./server/rpc/ServerProcess");
var severside_render = require("./server/lib/severside_render");
var crypto = require('crypto');
var winston = require('winston');
const url = require('url');
//winston.add(winston.transports.File, { filename: 'clients.log' });
winston.add(winston.transports.File, {
filename: 'events.log',
handleExceptions: true,
exceptionHandlers: [
new winston.transports.File({ filename: 'exceptions.log' })
]
});
winston.warn('App.js Loaded');
// Define a single-page client called 'main'
ss.client.define('main', {
view : 'app.html', //file under ../client/views
css : [//prefer loading from view
],
code : [//prefer loading from view
'app' //file under client/code/
],
tmpl : '*'
});
//ss.client.set({liveReload: false}) //WORKS FOR PRODUCTION
console.log('add route upload ',ss.http.route('/upload', function (req, res) {
return app_uploads.ajax_upload_with_rpc_feedback(req, res);
}));
ss.http.route('/locked?*', function (req, res) { //files that should not be publicly accessible
var fn = req.url.substr(8);
//console.log('parse ', req.url, fn);
//var path = zx.config.async.public
return app_uploads.ajax_get_secured_file(req, res,fn);
});
ss.http.route('/files?*', function (req, res) {
var fn = req.url.substr(7);
fn = fn.split(/&/)[0];
//console.log('parse ', req.session,req.url, fn);
//var path = zx.config.async.public
//dont have a way to correctly read the config...i.e. this service is not related to any config... console.log('parse ', zx.config.async.public.path);
console.log('Serving file from /files 104503 url:',req.url,'file name :', fn);
if (fn=='') {
app_utils.serveError404(res);
} else {
app_utils.serveBuffer(res, '', fs.readFileSync('./database/files/' + fn),0,fn); //TODO in production the s must be improved - should actually be server from a web server or CDN
}
return true;
});
ss.http.route('/', function (req, res) {
var force_login_first=false;
if (req.url=='/Login') force_login_first=true;
console.log('\r\n\r\n\r\n\r\n=======================================================ss.http.route: '+req.url,force_login_first);
var LoadedInstance = crypto.randomBytes(16).toString('base64');
//console.log('LoadedInstance: ', LoadedInstance);
var queryData = url.parse(req.url, true).query;
//console.log('queryData:', queryData);
// we can also serve url friendly pages from the application
//...rest of normal socket stream code ....
var ip = req.headers['x-forwarded-for'] ||
req.connection.remoteAddress ||
req.socket.remoteAddress ||
req.connection.socket.remoteAddress;
winston.verbose('Connect route/ ',{ip:ip , url:req.url , inst:LoadedInstance});
//console.log('===========================Inital contents of my session is ',ip, LoadedInstance, req.headers.host, req.url);
if (req.session.myStartID === undefined) {
req.session.myStartID = crypto.randomBytes(14).toString('base64').substring(0,18);
console.log('===========================Assigned new session ID ',req.session.myStartID);
req.session.save();
} else {
console.log('===========================Using Existing session ID ',req.session.myStartID);
}
/*
TODO locate the application that wants to be run
within that application we retrieve a config file
organise the application source in this tree... even though the compiler puts it in the database
*/
//format http://10.0.0.254:3000/application/page?username&password
//new format http://10.0.0.254:3000/app=abc,page=1,user=guest,pass=pass
//
//page includes the /
var root_folder = path.resolve('./') + '/';
var host_name = (req.headers.host.match(/(http:\/\/)?(https:\/\/)?(\w+)/) || ["", "",""])[3];
var home_page = (req.url.match(/([\/]\w+)([\w\W]+)/) || ["", "",""]);
var decoded="{"+decodeURIComponent(req.url).substring(1) + "}";
decoded = decoded.replace(/&/g,',');
decoded = decoded.replace(/\?/g,'');
//console.log('serveClient decoded:',decoded);
var params = json_like.parse(decoded);
params.user = params.user|| '';
params.password = params.password|| '';
if (params.user=='') {
//if () //params.invite // with an invite code create/use a temporary unique user that can be converted to a real login
//alt create/use a temporary unique user that can be converted to a real login
//finally default use a guest user
params.user = 'guest';
if (config.run.cookie_guest) {
params.password = req.session.myStartID;
console.log("config.run.cookie_guest :");
} else {
params.password = 'gu35t';
}
}
//console.log('serveClient params:',params);
var Application = params.app || '';
db.databasePooled(root_folder, LoadedInstance,Application, function (err , msg, rambase) {
if (err) {
console.log(err.message);
} else {
try {
//console.log("db.databasePooled :",params,'============================');
rambase.params=params;
if (params.user=='' || config.run.login_first || force_login_first) {
//this is a first page load ... without rendering - will be rendered on the login from the user
//console.log("first page load ... without server-side rendering");
//todo inject LoadedInstance
//res.serveClient('main');
res.serveClient('main',
function (html){
var cxx = {
obj:[
{Stash:"Stash",Session:"Session",Data:{cid:"cid"}}
]};
html = severside_render.render_inject('login',html,"",LoadedInstance,cxx);
return html;
});
} else {
//this is a first page load ... server-side rendered
console.log("first page load ... with server-side rendering, params:",params);
ServerProcess.produce_login(req, res, ss,rambase, '', params.user,params.password,
function (switchPage,target,scriptnamed,jsonstring){
//console.log("severside_render:",switchPage,jsonstring);
severside_render.render(switchPage,target,scriptnamed,jsonstring,"client/views/app.html",
function (cx,html_inject){
//console.log("severside_render html_inject");
res.serveClient('main',
function (html){
html = severside_render.render_inject('FirstPage',html,html_inject,LoadedInstance,cx);
return html;
});
});
});
}
} catch (e) {
console.log('ss.http.route threw:',e);
winston.error('ss.http.route threw:',e);
}
}
});//databasePooled
}); //end of ss.http.route callback
//ss.session.store.use('redis'); //- gives a problem but should be used later
// Code Formatters
// Use server-side compiled Hogan (Mustache) templates. Others engines available
ss.client.templateEngine.use(require('ss-hogan')); //, '/client/templates');
// Minimize and pack assets if you type: SS_ENV=production node app.js
if (ss.env === 'production')
ss.client.packAssets();
ss.client.set({
onChange:
{ DelayTime:2000,
GuardTime:3000,
Validate:function(path, event,action){ //needs fn wrapped else it does not add it to options
console.log('onChangeValidate :', path);
return true;
},
Publish:function(path, event,action,pubs){ //needs fn wrapped else it does not add it to options
console.log('onChange.Publish :', action);
//modify pubs if needed
return pubs;
}
}});
//var bodyParser = require('body-parser');
//ss.http.middleware.prepend( bodyParser() );
//ss.http.middleware.prepend( Busboy () );
// Start web server
var server = http.Server(ss.http.middleware);
var config = db.load_config('', '');
//clear screen and scrollback
console.log('\x1Bc');
console.log('\x1B[3J');
console.log('serveClient config.run.serve_port:', config.run.serve_port);
server.listen(config.run.serve_port);
//start qq file monitor if in dev mode
if (config.run_settings[config.run_mode].monitor_mode === "check") {
app_utils.run_monitor(1000);
}
if (config.run_settings[config.run_mode].monitor_mode === "jit") {
try {
zxGase= require("./server/compiler/quicc-gaze");
zxGase.gaze_start(app_utils.check_zx_depends_list);
}
catch (e) {
console.log("JIT set but compiler not found - app.js:248");
throw e;
}
}
// Start SocketStream
ss.start(server);
if (config.run_mode === "c9") {
console.log('To edit the application home page click '+path.resolve(__dirname)+'/Quale/Standard/Home/Guest/Index.quicc');
console.log('To access the application click https://'+process.env['C9_HOSTNAME']+' or "preview->preview running application" in the menu above');
} else if (config.run_mode !== "win") {
console.log('To edit the application home page edit the file '+path.resolve(__dirname)+'/Quale/Standard/Home/Guest/Index.quicc');
console.log('To access the application open http://'+process.env['C9_HOSTNAME']+':'+config.run.serve_port+'');
} //win has its own IDE start up