-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.js
executable file
·110 lines (108 loc) · 2.9 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
//
// Nodejs SMS sender with Kannel and PostgerSQL as a backend and dedicated dashboard.
//
// Dariusz Pawlak <pawlakdp@gmail.com>
// 2013.07.15
//
//
var ss = require('socketstream')
, conf = require('./conf')
, events = require('events')
, eventEmitter = new events.EventEmitter()
, http = require('http')
, moment = require('moment')
, util = require('util')
, sendDemoSms = require('./server/libs//sendDemoSms')
// , dbhPgsql = require('./server/libs/dbh_pgsql')
, querystring = require('querystring')
, url = require('url')
, handleSmsQuery = require('./server/libs/smsQueryHandler')
;
// Define a single-page client called 'main'
ss.client.define('main', {
view: 'smsboard.html',
css: [],
code: ['libs', 'app'],
tmpl: '*'
});
// Serve this client on the root URL
ss.http.route('/', function(req, res){
res.serveClient('main');
});
//
//
//
ss.http.route('/smsquery', function(req, res){
//console.log("REQ="+util.inspect(req));
// var qs = querystring.parse(req.url);
// console.log("REQ.qs="+util.inspect(qs));
var uu = url.parse(req.url, true);
console.log("REQ.uu="+util.inspect(uu.query));
handleSmsQuery(uu.query, function(err, ret){
if (err) console.error("Error: "+err);
});
//
res.writeHead(200, {'Content-Type': 'text/plain' });
res.end("OK");
});
// Code Formatters
//ss.client.formatters.add(require('ss-stylus'));
// Use server-side compiled Hogan (Mustache) templates. Others engines available
//ss.client.templateEngine.use(require('ss-hogan'));
// Minimize and pack assets if you type: SS_ENV=production node app.js
if (ss.env === 'production') ss.client.packAssets();
//
// send date and time to client
if (conf.sendDtToClient){
setInterval(function() {
var now = moment().format("YYYY-MM-DD HH:mm");
ss.api.publish.all('dt', now);
}, 10000 );
};
//
// Start web server
var server = http.Server(ss.http.middleware);
var listPort = process.env.PORT || 3001;
console.log("Server listen on port: "+listPort);
server.listen( listPort );
// Start SocketStream
ss.start(server);
//
// run app in configured mode
//
if (conf.mode==="demo"){
console.log("DEMO MODE");
var Puid = require("puid");
puid = new Puid();
//
// send fake sms info
var maxSmsInTestPack = 5;
//
setInterval(function() {
var smsNum = Math.floor((Math.random()*maxSmsInTestPack)+1);
var now = moment().format("YYYY-MM-DD HH:mm:ss");
for (var i=0; i<smsNum; i++){
var sms = {
"id": puid.generate(),
"dtin":now,
"sname": "Demo Sender",
"rname": "Demo Recipient",
"phone": "123456789",
"status":"new",
"text": "bla bla"
};
console.log("New sms id="+util.inspect(sms));
sendDemoSms(sms, function(err, ret){
if (err) return console.error(err);
return true;
});
};
}, 15000 );
} else if (conf.mode==="prod"){
console.log("MODE: PRODUCTION");
//
// readSmsFromQueue();
};
//
// EOF
//