-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
97 lines (77 loc) · 2.55 KB
/
server.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
var fs = require('fs'),
util = require('util'),
colors = require('colors'),
argv = require('optimist').argv,
restify = require('restify'),
database = require('./lib/database'),
mongoPersister = require('./lib/mongoPersister');;
var help = [
"usage: node server [options] ",
"",
"Starts a contact server ",
"",
"options:",
" --port PORT Port that the xpush server should run on",
" --config OUTFILE Location of the configuration file for the xpush server",
" -h, --help You're staring at it"
].join('\n');
var welcome = [
" ",
" ┌─┐┌─┐┌┐┌┌┬┐┌─┐┌─┐┌┬┐",
" │ │ ││││ │ ├─┤│ │ ",
" └─┘└─┘┘└┘ ┴ ┴ ┴└─┘ ┴ ",
" ┌─┐┌─┐┬─┐┬ ┬┌─┐┬─┐ ",
" └─┐├┤ ├┬┘└┐┌┘├┤ ├┬┘ ",
" └─┘└─┘┴└─ └┘ └─┘┴└─ ",
" "
].join('\n');
if (argv.h || argv.help || Object.keys(argv).length === 2 || !argv.config) {
return util.puts(help);
}
var conf = {},
port = argv.port || 8880;
try {
var data = fs.readFileSync(argv.config);
conf = JSON.parse(data.toString());
} catch (ex) {
util.puts('Error starting xpush server: ' + ex);
process.exit(1);
}
conf.port = port || conf.port;
database.config(
conf && conf.mongodb && conf.mongodb.address ? conf.mongodb.address : '',
'contact',
function (err, message) {
if(!err) console.info(' - Mongodb is connected');
}
);
var server = restify.createServer();
server.use(restify.queryParser());
server.use(restify.bodyParser());
server.use(restify.CORS( {origins: ['*:*']}));
server.use(restify.fullResponse());
//server.use(restify.jsonp());
server.post('/message', function (req, res, next) {
if(!req.params.app || !req.params.name || !req.params.email || !req.params.message){
res.send({status: 'error', message: 'name, email and message must be supplied'});
return;
}
console.log('['+req.params.email+'] send messages');
mongoPersister.createMessage(
req.params.app,
req.params.name,
req.params.email,
req.params.message,
function(err){
if(err){
res.send({status: 'error', message: err});
return;
}
res.send({status: 'ok'});
});
});
server.listen(conf.port, function () {
util.puts(welcome.rainbow.bold);
var startMessage = ' now listening on port: ' + conf.port;
util.puts(startMessage.underline.bold.green + '\n');
});