1
0
mirror of https://github.com/mainflux/mainflux.git synced 2025-04-26 13:48:53 +08:00

Add MQTT, HTTP and WS API servers

Signed-off-by: Drasko DRASKOVIC <drasko.draskovic@gmail.com>
This commit is contained in:
Drasko DRASKOVIC 2016-04-27 23:02:28 +02:00
parent 8fee81d8bc
commit cf17b53bb6
4 changed files with 141 additions and 54 deletions

72
httpServer.js Normal file
View File

@ -0,0 +1,72 @@
/**
* Copyright (c) Mainflux
*
* Mainflux server is licensed under an Apache license, version 2.0 license.
* All rights not explicitly granted in the Apache license, version 2.0 are reserved.
* See the included LICENSE file for more details.
*/
var restify = require('restify');
var domain = require('domain');
var config = require('./config');
var log = require('./app/logger');
/**
* HTTP Restify
*/
/** Create httpServer */
var httpServer = restify.createServer({
name: "Mainflux"
});
httpServer.pre(restify.pre.sanitizePath());
httpServer.use(restify.acceptParser(httpServer.acceptable));
httpServer.use(restify.bodyParser());
httpServer.use(restify.queryParser());
httpServer.use(restify.authorizationParser());
httpServer.use(restify.CORS());
httpServer.use(restify.fullResponse());
/** Global error handler */
httpServer.use(function(req, res, next) {
var domainHandler = domain.create();
domainHandler.on('error', function(err) {
var errMsg = 'Request: \n' + req + '\n';
errMsg += 'Response: \n' + res + '\n';
errMsg += 'Context: \n' + err;
errMsg += 'Trace: \n' + err.stack + '\n';
console.log(err.message);
log.info(err);
});
domainHandler.enter();
next();
});
/**
* ROUTES
*/
var route = require('./app/routes');
route(httpServer);
/**
* SERVER START
*/
var port = process.env.PORT || config.http.port;
httpServer.listen(port, function() {
console.log('HTTP magic happens on port ' + port);
});
/**
* Exports
*/
module.exports = httpServer;

View File

@ -5,61 +5,17 @@
* All rights not explicitly granted in the Apache license, version 2.0 are reserved.
* See the included LICENSE file for more details.
*/
var restify = require('restify');
var domain = require('domain');
var config = require('./config/config');
var config = require('./config');
var log = require('./app/logger');
/**
* RESTIFY
*/
var httpApi = require('./httpServer');
var mqttApi = require('./mqttServer');
var mqttApi = require('./wsServer');
/** Create server */
var server = restify.createServer({
name: "Mainflux"
});
var mainflux = {};
server.pre(restify.pre.sanitizePath());
server.use(restify.acceptParser(server.acceptable));
server.use(restify.bodyParser());
server.use(restify.queryParser());
server.use(restify.authorizationParser());
server.use(restify.CORS());
server.use(restify.fullResponse());
/** Global error handler */
server.use(function(req, res, next) {
var domainHandler = domain.create();
domainHandler.on('error', function(err) {
var errMsg = 'Request: \n' + req + '\n';
errMsg += 'Response: \n' + res + '\n';
errMsg += 'Context: \n' + err;
errMsg += 'Trace: \n' + err.stack + '\n';
console.log(err.message);
log.info(err);
});
domainHandler.enter();
next();
});
/**
* ROUTES
*/
var route = require('./app/routes');
route(server);
/**
* SERVER START
*/
var port = process.env.PORT || config.port;
var banner = `
oocccdMMMMMMMMMWOkkkkoooolcclX
llc:::0MMMMMMMM0xxxxxdlllc:::d
@ -77,13 +33,10 @@ MMWkxxxxxxdlllc:::oNMMMMMM0xxx
MMMXxxxxxdllllc::cXMMMMMMMWOxx
MMMM0xxxxolllc:::kMMMMMMMMMXxx
`
server.listen(port, function() {
console.log(banner);
console.log('Magic happens on port ' + port);
});
console.log(banner);
/**
* Exports
*/
module.exports = server;
module.exports = mainflux;

37
mqttServer.js Normal file
View File

@ -0,0 +1,37 @@
/**
* Copyright (c) Mainflux
*
* Mainflux server is licensed under an Apache license, version 2.0 license.
* All rights not explicitly granted in the Apache license, version 2.0 are reserved.
* See the included LICENSE file for more details.
*/
var config = require('./config');
/***
* MQTT Mosca
*/
var mosca = require('mosca');
var mqttServer = new mosca.Server(config.mosca);
mqttServer.on('clientConnected', function(client) {
console.log('client connected', client.id);
});
/** Message received */
mqttServer.on('published', function(packet, client) {
console.log('Published', packet.payload);
});
mqttServer.on('ready', setupMqtt);
/** Mqtt server ready */
function setupMqtt() {
console.log('MQTT magic happens on port 1883');
}
/**
* Exports
*/
module.exports = mqttServer;

25
wsServer.js Normal file
View File

@ -0,0 +1,25 @@
/**
* Copyright (c) Mainflux
*
* Mainflux server is licensed under an Apache license, version 2.0 license.
* All rights not explicitly granted in the Apache license, version 2.0 are reserved.
* See the included LICENSE file for more details.
*/
var WebSocketServer = require('ws').Server
, wsServer = new WebSocketServer({ port: 5152 });
wsServer.on('connection', function connection(ws) {
ws.on('message', function incoming(message) {
console.log('received: %s', message);
});
ws.send('something');
});
console.log('WS magic happens on port 5152');
/**
* Exports
*/
module.exports = wsServer;