forked from quale-quest/sql-mvc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
116 lines (90 loc) · 3.39 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
"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 spawn = require('child_process').spawn;
var fs = require('fs');
var timestamp = function () {
var pad2 = function (number) {
return (number < 10 ? '0' : '') + number;
};
var d = new Date();
return d.getHours() + ':' + pad2(d.getMinutes()) + ':' + pad2(d.getSeconds()) + '.' + pad2(d.getMilliseconds());
};
var run_monitor = function (interval_ms) {
console.log('monitoring for application changes :');
var interval = setInterval(function () {
var command = spawn('./check.sh');
var output = [];
command.stdout.on('data', function (chunk) {
if (chunk.length > 10)
output.push(chunk);
});
command.on('close', function (code) {
if (code === 0) {
var str = output.join('').toString();
if (str.length > 10) {
var fn = path.resolve('output/consol.txt');
fs.writeFileSync(fn, str + "...");
ss.api.publish.all('BuildNotify', '#debugBuildNotify','done'); // Broadcast the message to everyone
}
if (str.length > 0) //don't bother us with small status message
console.log('check.sh result :', str);
} else
console.log('compiler busy :');
});
}, interval_ms);
}
// 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 : '*'
});
// Serve this client on the root URL
ss.http.route('/', function (req, res) {
//console.log('===========================Initial contents of my session is ', req.session.myStartID);
console.log('===========================Inital contents of my session is ', req.headers.host, req.url, 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
*/
if (req.session.myStartID === undefined) {
//ss.session.options.secret = crypto.randomBytes(32).toString();
req.session.myStartID = timestamp();
req.session.save();
}
var root_folder = path.resolve('./Quale/') + '/';
db.databasePooled(root_folder, req.session.myStartID, req.url, function (err /*, msg, dbref*/
) {
if (err) {
console.log(err.message);
} else {
res.serveClient('main');
}
});
}); //end of ss.http.route callback
//ss.session.store.use('redis'); - gves a prolem 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();
// Start web server
var server = http.Server(ss.http.middleware);
var config = JSON.parse(require('fs').readFileSync('Quale/Config/config.json').toString());
server.listen(config.serve_port);
//start qq file monitor if in dev mode
if (config.monitor_mode[config.run_mode] === "check") { //Develop Debug Demo Production
run_monitor(1000);
}
// Start SocketStream
ss.start(server);