-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
123 lines (94 loc) · 2.89 KB
/
index.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
#!/usr/bin/env node
/**
* Module dependencies.
*/
//process.env.NODE_ENV = 'production';
process.env.NODE_ENV = 'development';
const cluster = require('cluster');
const config = require('config.json')('./config/config.json');
const CORS = require('cors')();
const fileStreamRotator = require('file-stream-rotator');
const fs = require('fs');
const morgan = require('morgan');
const mqtt = require('mqtt');
const os = require('os');
const restify = require('restify');
const routes = require('./routes');
const handler = require('./handler');
const makeGatewayURL = require('./js/make_gateway_URL');
const constants = require('./lib/constants');
// TEMP Gateway IP URL
global.BASE_URL = makeGatewayURL(config.sl.gw.ip, config.sl.gw.port, config.sl.gw.version);
global.APP_NAME = config.app.title;
const port = config.app.port;
const version = config.app.version;
const cpuCount = os.cpus().length;
const logDirectory = './logs';
var worker = [];
// Cluster
cluster.schedulingPolicy = cluster.SCHED_RR;
if(cluster.isMaster) {
// ensure log directory exists
if (!fs.existsSync(logDirectory)){
fs.mkdirSync(logDirectory);
}
// create a rotating write stream
var accessLogStream = fileStreamRotator.getStream({
date_format: 'YYYYMMDD',
filename: logDirectory + '/access-%DATE%.log',
frequency: 'daily',
verbose: false
});
cluster.on('exit', function(worker) {
console.log('worker' + worker.pid + ' died --> start again');
cluster.fork();
});
console.log("-----------------------------");
console.log("\t" + global.APP_NAME + " start!\n");
console.log('CPU Count:', cpuCount);
var url = "wss://b-8b6a530d-9792-461c-9b07-7103bebe99ec-1.mq.us-east-1.amazonaws.com:61619";
var option = {
username : "mac",
password : "9372153rlaA@"
}
var client = mqtt.connect(url, option);
client.on('connect', function () {
const topic = constants.MQTT_REQUEST_TOPIC;
console.log("subscribe :", topic);
client.subscribe(topic);
client.on('message', function (topic, message) {
// message is Buffer
messageString = message.toString();
console.log(messageString);
handler(messageString);
});
});
for(var i = 0; i < cpuCount; i++) {
worker[i] = cluster.fork();
}
}else {
/**
* Initialize Server
*/
const server = restify.createServer({
name: global.APP_NAME,
version: version
});
/**
* Middleware
*/
server.use(morgan('dev'));
server.use(restify.plugins.jsonBodyParser({ mapParams: true }));
server.use(restify.plugins.acceptParser(server.acceptable));
server.use(restify.plugins.queryParser({ mapParams: true }));
server.use(restify.plugins.fullResponse());
server.use(CORS);
server.use(function(req, res, next){
res.setHeader('content-type','application/json');
next()
});
server.listen(port, function(){
console.log(`Server Listen ${port} port`);
routes(server);
});
}