mirror of
https://github.com/mainflux/mainflux.git
synced 2025-04-27 13:48:49 +08:00
Refractored in Microservices
Signed-off-by: Drasko DRASKOVIC <drasko.draskovic@gmail.com>
This commit is contained in:
parent
0dca37b656
commit
d950002593
37
Dockerfile
37
Dockerfile
@ -1,37 +0,0 @@
|
||||
###
|
||||
# Mainflux Dockerfile
|
||||
###
|
||||
# Set the base image to Node, onbuild variant: https://registry.hub.docker.com/_/node/
|
||||
|
||||
FROM node:4.2.3
|
||||
MAINTAINER Mainflux
|
||||
|
||||
ENV MAINFLUX_PORT=7070
|
||||
|
||||
RUN apt-get update -qq && apt-get install -y build-essential
|
||||
|
||||
RUN mkdir /mainflux
|
||||
|
||||
###
|
||||
# Installations
|
||||
###
|
||||
# Add Gulp globally
|
||||
|
||||
RUN npm install -g gulp
|
||||
RUN npm install -g nodemon
|
||||
|
||||
# Finally, install all project Node modules
|
||||
COPY . /mainflux
|
||||
WORKDIR /mainflux
|
||||
RUN npm install
|
||||
|
||||
EXPOSE $MAINFLUX_PORT
|
||||
|
||||
###
|
||||
# Run main command from entrypoint and parameters in CMD[]
|
||||
###
|
||||
|
||||
CMD [""]
|
||||
|
||||
# Set default container command
|
||||
ENTRYPOINT gulp
|
@ -1,104 +0,0 @@
|
||||
var mongojs = require('mongojs');
|
||||
var devicesDb = require('../database').collection('devices');
|
||||
|
||||
var jwt = require('jsonwebtoken');
|
||||
var config = require('../../config/config');
|
||||
var log = require('../logger');
|
||||
|
||||
var os = require('os');
|
||||
|
||||
/** createDevice() */
|
||||
exports.createDevice = function(req, res, next) {
|
||||
|
||||
console.log("req.headers['x-auth-token'] = ", req.headers['x-auth-token']);
|
||||
console.log("req.headers['content-type'] = ", req.headers['content-type']);
|
||||
|
||||
/** Save the device and check for errors */
|
||||
devicesDb.save(req.body, function(err, device) {
|
||||
if (err)
|
||||
return next(err);
|
||||
|
||||
var signaturePayload = {
|
||||
version: config.version
|
||||
}
|
||||
|
||||
var token = jwt.sign(signaturePayload, config.tokenSecret, {
|
||||
subject: 'Device Auth Token',
|
||||
issuer: req.headers.host,
|
||||
audience: device._id.toString()
|
||||
});
|
||||
|
||||
res.json({
|
||||
status: 200,
|
||||
message: 'Device created',
|
||||
token: token,
|
||||
_id: device._id.toString()
|
||||
});
|
||||
});
|
||||
|
||||
return next();
|
||||
}
|
||||
|
||||
/** getAllDevices() */
|
||||
exports.getAllDevices = function(req, res, next) {
|
||||
|
||||
console.log("req.headers['x-auth-token'] = ", req.headers['x-auth-token']);
|
||||
|
||||
log.info('hi');
|
||||
|
||||
devicesDb.find(req.body, function(err, devices) {
|
||||
if (err)
|
||||
return next(err);
|
||||
|
||||
res.json(devices);
|
||||
return next();
|
||||
});
|
||||
}
|
||||
|
||||
/** getDevice() */
|
||||
exports.getDevice = function(req, res, next) {
|
||||
|
||||
devicesDb.findOne({_id: mongojs.ObjectId(req.params.device_id)}, function(err, device) {
|
||||
if (err)
|
||||
return next(err);
|
||||
|
||||
if (device) {
|
||||
res.json(device);
|
||||
} else {
|
||||
res.send("NOT FOUND");
|
||||
}
|
||||
return next();
|
||||
});
|
||||
}
|
||||
|
||||
/** updateDevice() */
|
||||
exports.updateDevice = function(req, res, next) {
|
||||
/** Use our device model to find the device we want */
|
||||
console.log(req.body);
|
||||
devicesDb.update({
|
||||
_id: mongojs.ObjectId(req.params.device_id)
|
||||
},
|
||||
{$set: req.body},
|
||||
function(err, device) {
|
||||
if (err)
|
||||
return next(err);
|
||||
|
||||
res.send('OK');
|
||||
return next();
|
||||
});
|
||||
}
|
||||
|
||||
/** deleteDevice() */
|
||||
exports.deleteDevice = function(req, res, next) {
|
||||
|
||||
devicesDb.remove({
|
||||
_id: mongojs.ObjectId(req.params.device_id)
|
||||
}, function(err, device) {
|
||||
if (err)
|
||||
return next(err);
|
||||
|
||||
res.send('OK');
|
||||
return next();
|
||||
});
|
||||
}
|
||||
|
@ -1,12 +0,0 @@
|
||||
/**
|
||||
* STATUS
|
||||
*/
|
||||
exports.getStatus = function(req, res, next) {
|
||||
|
||||
console.log("req.headers['x-auth-token'] = ", req.headers['x-auth-token']);
|
||||
|
||||
var stat = {"status":"running"};
|
||||
res.send(stat);
|
||||
|
||||
return next();
|
||||
}
|
@ -1,26 +0,0 @@
|
||||
var config = require('../config');
|
||||
|
||||
/**
|
||||
* MONGO DB
|
||||
*/
|
||||
var mongojs = require('mongojs');
|
||||
|
||||
/** Connect to DB */
|
||||
var collections = ['devices'];
|
||||
|
||||
/** Check if we run with Docker compose */
|
||||
var dockerMongo = process.env.MONGODB_NAME;
|
||||
var dbUrl = '';
|
||||
if (dockerMongo && dockerMongo == '/mainflux-api/mongodb') {
|
||||
dbUrl = 'mongodb://' + process.env.MONGODB_PORT_27017_TCP_ADDR + ':' + process.env.MONGODB_PORT_27017_TCP_PORT + '/' + config.db.name;
|
||||
} else {
|
||||
dbUrl = 'mongodb://' + config.db.host + ':' + config.db.port + '/' + config.db.name;
|
||||
}
|
||||
|
||||
var db = mongojs(dbUrl);
|
||||
|
||||
|
||||
/**
|
||||
* EXPORTS
|
||||
*/
|
||||
module.exports = db;
|
@ -1,4 +0,0 @@
|
||||
var bunyan = require('bunyan');
|
||||
var log = bunyan.createLogger({name: "Mainflux"});
|
||||
|
||||
module.exports = log;
|
@ -1,146 +0,0 @@
|
||||
/**
|
||||
* Dependencies
|
||||
*/
|
||||
var mongoose = require('mongoose');
|
||||
|
||||
|
||||
/**
|
||||
* Private variables and functions
|
||||
*/
|
||||
var Schema = mongoose.Schema;
|
||||
|
||||
|
||||
/**
|
||||
* Exports
|
||||
*/
|
||||
|
||||
/**
|
||||
* \b Device Schema
|
||||
*
|
||||
* @param name {String} Friendly name of the device
|
||||
* @param description {String} Description of the device
|
||||
* @param creator {String} Device creator
|
||||
* @param owner {String} Owner of the device
|
||||
* @param group {String} Device group that device belongs to
|
||||
* @param deviceId {String} UUID of the device
|
||||
* @param apiKey {String} Authentication token for accessing Mainflux API
|
||||
* @param createdAt {Date} Timestamp of the device creation
|
||||
* @param isPublic {Boolean} Is device publicly shared (not claimed yet)
|
||||
* @param online {Boolean} Is device currently connected
|
||||
* @param lastSeen {Date} When was the device last time connected
|
||||
* @param updatedAt {Date} Timestamp of the last interaction between device and cloud
|
||||
* @param manufacturerId {String} UUID of the manufacturing company
|
||||
* @param serialNumber {String} Manufacturer marks devices by serial number
|
||||
* @param productId {String} devices belong to some product (ex. HUE lights)
|
||||
* @param activationCode {String} 3rd party apps might prefer codes for device claiming
|
||||
* @param deviceLocation {String} Physical location of the device
|
||||
* @param firmwareVersion {String} Needed for the OTA updates
|
||||
*/
|
||||
var DeviceSchema = new Schema({
|
||||
name: {
|
||||
type: String,
|
||||
required: false
|
||||
},
|
||||
description: {
|
||||
type: String,
|
||||
required: false
|
||||
},
|
||||
creator: {
|
||||
type: String,
|
||||
required: false,
|
||||
},
|
||||
owner: {
|
||||
type: String,
|
||||
required: false,
|
||||
},
|
||||
group: {
|
||||
type: Array,
|
||||
default: []
|
||||
},
|
||||
deviceId: {
|
||||
type: String,
|
||||
required: false,
|
||||
index: true,
|
||||
match: /^[0-9a-f]{10}$/
|
||||
},
|
||||
apiKey: {
|
||||
type: String,
|
||||
required: false,
|
||||
index: true
|
||||
},
|
||||
createdAt: {
|
||||
type: Date,
|
||||
index: true,
|
||||
default: Date.now
|
||||
},
|
||||
isPublic: {
|
||||
type: Boolean,
|
||||
index: true,
|
||||
default: false
|
||||
},
|
||||
online: {
|
||||
type: Boolean,
|
||||
index: true,
|
||||
default: false
|
||||
},
|
||||
lastSeen: {
|
||||
type: Date
|
||||
},
|
||||
updatedAt: {
|
||||
type: Date
|
||||
},
|
||||
manufacturerId: {
|
||||
type: String,
|
||||
required: false,
|
||||
index: true,
|
||||
match: /^[0-9a-f]{10}$/
|
||||
},
|
||||
serialNumber: {
|
||||
type: String,
|
||||
required: false,
|
||||
index: true,
|
||||
match: /^[0-9a-f]{10}$/
|
||||
},
|
||||
productId: {
|
||||
type: String,
|
||||
required: false,
|
||||
index: true,
|
||||
match: /^[0-9a-f]{10}$/
|
||||
},
|
||||
activationCode: {
|
||||
type: String,
|
||||
required: false,
|
||||
index: true,
|
||||
match: /^[0-9a-f]{10}$/
|
||||
},
|
||||
deviceLocation: {
|
||||
type: String,
|
||||
required: false,
|
||||
index: true,
|
||||
match: /^[0-9a-f]{10}$/
|
||||
},
|
||||
firmwareVersion: {
|
||||
type: String,
|
||||
required: false,
|
||||
index: true,
|
||||
match: /^[0-9a-f]{10}$/
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
|
||||
DeviceSchema.static('exists', function (apikey, deviceid, callback) {
|
||||
this.where({ apiKey: apikey, deviceId: deviceid }).findOne(callback);
|
||||
});
|
||||
|
||||
DeviceSchema.static('getDeviceByDeviceId', function (deviceid, callback) {
|
||||
this.where({ deviceId: deviceid }).findOne(callback);
|
||||
});
|
||||
|
||||
DeviceSchema.static('getDevicesByApikey', function (apikey, callback) {
|
||||
this.where('apiKey', apikey).find(callback);
|
||||
});
|
||||
|
||||
|
||||
module.exports = mongoose.model('Device', DeviceSchema);
|
||||
|
@ -1,73 +0,0 @@
|
||||
// app/models/user.js
|
||||
|
||||
var mongoose = require('mongoose');
|
||||
var Schema = mongoose.Schema;
|
||||
var bcrypt = require('bcrypt');
|
||||
var _ = require('lodash');
|
||||
|
||||
var UserSchema = new Schema({
|
||||
firstName: {
|
||||
type: String,
|
||||
trim: true,
|
||||
default: '',
|
||||
},
|
||||
lastName: {
|
||||
type: String,
|
||||
trim: true,
|
||||
default: '',
|
||||
},
|
||||
displayName: {
|
||||
type: String,
|
||||
trim: true
|
||||
},
|
||||
email: {
|
||||
type: String,
|
||||
trim: true,
|
||||
unique: true,
|
||||
},
|
||||
password: {
|
||||
type: String,
|
||||
default: '',
|
||||
require: true
|
||||
},
|
||||
created: {
|
||||
type: Date,
|
||||
default: Date.now
|
||||
},
|
||||
});
|
||||
|
||||
/**
|
||||
* Hook a pre save method to hash the user password
|
||||
*/
|
||||
UserSchema.pre('save', function(next) {
|
||||
var user = this;
|
||||
// only hash the password if it has been modified (or is new)
|
||||
if (!user.isModified('password')) return next()
|
||||
bcrypt.genSalt(10, function(err, salt) {
|
||||
bcrypt.hash(user.password, salt, function(err, hash) {
|
||||
if(err) return next(err);
|
||||
// set the hashed password back on our user document
|
||||
user.password = hash;
|
||||
//user.save();
|
||||
next();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
// User methods
|
||||
|
||||
/**
|
||||
* Validate user password on singin and compare with hased password stored in DB
|
||||
*/
|
||||
|
||||
UserSchema.methods.validateUserPassword = function(plainPassword, cb) {
|
||||
var user = this;
|
||||
bcrypt.compare(plainPassword, user.password, function(err, isMatch) {
|
||||
if (err) return cb(err);
|
||||
cb(null, isMatch);
|
||||
});
|
||||
};
|
||||
|
||||
// TODO: check if user with email already exist
|
||||
|
||||
module.exports = mongoose.model('User', UserSchema);
|
@ -1,36 +0,0 @@
|
||||
var restify = require('restify');
|
||||
var config = require('../config');
|
||||
var devicesController = require('./controllers/devices');
|
||||
var statusController = require('./controllers/status');
|
||||
|
||||
function routes(api) {
|
||||
/**
|
||||
* /STATUS
|
||||
*/
|
||||
api.get('/status', statusController.getStatus);
|
||||
|
||||
|
||||
/**
|
||||
* /DEVICES
|
||||
*/
|
||||
/** Create a device (accessed at POST http://localhost:8080/devices) */
|
||||
api.post('/devices', devicesController.createDevice);
|
||||
|
||||
/** Get all the devices (accessed at GET http://localhost:8080/devices) */
|
||||
api.get('/devices', devicesController.getAllDevices);
|
||||
|
||||
/**
|
||||
* /devices/:device_id
|
||||
* N.B. Colon (`:`) is needed because of Express `req.params`: http://expressjs.com/api.html#req.params
|
||||
*/
|
||||
/** Get the device with given id (accessed at GET http://localhost:8080/devices/:device_id) */
|
||||
api.get('/devices/:device_id', devicesController.getDevice)
|
||||
|
||||
/** Update the device with given id (accessed at PUT http://localhost:8080/devices/:device_id) */
|
||||
api.put('/devices/:device_id', devicesController.updateDevice)
|
||||
|
||||
/** Delete the device with given id (accessed at DELETE http://localhost:8080/devices/:device_id) */
|
||||
api.del('/devices/:device_id', devicesController.deleteDevice);
|
||||
}
|
||||
|
||||
module.exports = routes;
|
@ -1,36 +0,0 @@
|
||||
var express = require('express');
|
||||
var router = express.Router(); // get an instance of the express Router
|
||||
|
||||
var devices = require('../controllers/devices');
|
||||
|
||||
/**
|
||||
* /devices
|
||||
*/
|
||||
router.route('/')
|
||||
|
||||
/** Create a device (accessed at POST http://localhost:8080/devices) */
|
||||
.post(devices.createDevice)
|
||||
|
||||
/** Get all the devices (accessed at GET http://localhost:8080/devices) */
|
||||
.get(devices.getAllDevices);
|
||||
|
||||
|
||||
/**
|
||||
* /devices/:device_id
|
||||
* N.B. Colon (`:`) is needed because of Express `req.params`: http://expressjs.com/api.html#req.params
|
||||
*/
|
||||
router.route('/:device_id')
|
||||
|
||||
/** Get the device with that id (accessed at GET http://localhost:8080/devices/:device_id) */
|
||||
.get(devices.getDevice)
|
||||
|
||||
/** Update the device with this id (accessed at PUT http://localhost:8080/devices/:device_id) */
|
||||
.put(devices.updateDevice)
|
||||
|
||||
/** Delete the device with this id (accessed at DELETE http://localhost:8080/devices/:device_id) */
|
||||
.delete(devices.deleteDevice);
|
||||
|
||||
/**
|
||||
* Export router module
|
||||
*/
|
||||
module.exports = router;
|
@ -1,51 +0,0 @@
|
||||
var express = require('express');
|
||||
var router = express.Router(); // get an instance of the express Router
|
||||
var _ = require('lodash');
|
||||
var jwt = require('jsonwebtoken'); // used to create, sign, and verify tokens
|
||||
|
||||
var config = require('../../config/config');
|
||||
var User = require('../models/user');
|
||||
|
||||
// on routes that end in /things
|
||||
// ----------------------------------------------------
|
||||
router.route('/')
|
||||
|
||||
// Authenticate the user and return user token (accessed at POST http://localhost:8080/sessions)
|
||||
.post(function(req, res) {
|
||||
if(req.body.email && req.body.password) {
|
||||
// Find the user
|
||||
User.findOne({
|
||||
email: req.body.email
|
||||
}, function(err, user) {
|
||||
if (err) throw err;
|
||||
if(!user){
|
||||
// User with this email does not exist
|
||||
res.json({status:404, message: 'Authentication failed. User not found.' });
|
||||
}
|
||||
// Validate user password
|
||||
user.validateUserPassword(req.body.password,function (err, isMatch) {
|
||||
if(err){
|
||||
res.json({status:401, message: 'Unauthorized.' });
|
||||
}
|
||||
if(isMatch){
|
||||
// Generate user token
|
||||
var token = jwt.sign(user, config.secretToken, {
|
||||
expiresInMinutes: config.userTokenExpirePeriod
|
||||
});
|
||||
res.json({status:200, token: token, message: 'Authentication succeeded.' });
|
||||
} else {
|
||||
res.json({status:401, message: 'Unauthorized.' });
|
||||
}
|
||||
});
|
||||
})
|
||||
}
|
||||
else {
|
||||
// Email or password are not provided
|
||||
res.json({status:400, message: 'Bad request.' });
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
// export router module
|
||||
module.exports = router;
|
||||
|
@ -1,15 +0,0 @@
|
||||
var express = require('express');
|
||||
var router = express.Router(); // get an instance of the express Router
|
||||
|
||||
// on routes that end in /things
|
||||
// ----------------------------------------------------
|
||||
router.route('/')
|
||||
|
||||
// get the status (accessed at GET http://localhost:8080/status)
|
||||
.get(function(req, res) {
|
||||
var stat = {"status":"running"}
|
||||
res.send(stat);
|
||||
});
|
||||
|
||||
// export router module
|
||||
module.exports = router;
|
@ -1,42 +0,0 @@
|
||||
var express = require('express');
|
||||
var router = express.Router(); // get an instance of the express Router
|
||||
var _ = require('lodash');
|
||||
var jwt = require('jsonwebtoken'); // used to create, sign, and verify tokens
|
||||
|
||||
var config = require('../../config/config');
|
||||
var User = require('../models/user');
|
||||
|
||||
// on routes that end in /things
|
||||
// ----------------------------------------------------
|
||||
router.route('/')
|
||||
|
||||
// create a new user account and return user token (accessed at POST http://localhost:8080/users)
|
||||
.post(function(req, res) {
|
||||
|
||||
if (!req.body.email || !req.body.password) { // Check for email and password in request
|
||||
return res.json({status:400, message: 'Bad request.' });
|
||||
}
|
||||
//TODO: Check if user with this email already exist
|
||||
var user = new User({
|
||||
email: req.body.email,
|
||||
password: req.body.password
|
||||
});
|
||||
// save the user and generate token
|
||||
user.save(function(err) {
|
||||
if (err)
|
||||
res.send(err);
|
||||
// Create user token
|
||||
var token = jwt.sign(user, config.secretToken, {
|
||||
expiresInMinutes: config.userTokenExpirePeriod
|
||||
});
|
||||
res.json({
|
||||
status: 200,
|
||||
message: 'Account created!',
|
||||
token: token
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
// export router module
|
||||
module.exports = router;
|
||||
|
51
config.js
51
config.js
@ -1,51 +0,0 @@
|
||||
/**
|
||||
* 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 mosca = require('mosca');
|
||||
|
||||
var config = {};
|
||||
|
||||
/**
|
||||
* HTTP
|
||||
*/
|
||||
config.http = {
|
||||
message : 'We are in development',
|
||||
port : 7070,
|
||||
version: 0.1
|
||||
}
|
||||
|
||||
/**
|
||||
* MongoDB
|
||||
*/
|
||||
config.db = {
|
||||
host : 'localhost',
|
||||
port : 27017,
|
||||
name : 'test'
|
||||
}
|
||||
|
||||
/**
|
||||
* Mosca
|
||||
*/
|
||||
var moscaBackend = {
|
||||
type: 'redis',
|
||||
redis: require('redis'),
|
||||
db: 7,
|
||||
port: 6379,
|
||||
return_buffers: true, // to handle binary payloads
|
||||
host: "localhost"
|
||||
}
|
||||
|
||||
config.mosca = {
|
||||
port: 1883,
|
||||
backend: moscaBackend,
|
||||
persistence: {
|
||||
factory: mosca.persistence.Redis
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
module.exports = config;
|
@ -1,4 +0,0 @@
|
||||
/*
|
||||
* Following recipe here: http://dailyjs.com/2014/01/02/recipe-for-express-configuration/
|
||||
*/
|
||||
module.exports = require('./' + (process.env.NODE_ENV || 'development') + '.json');
|
@ -1,16 +0,0 @@
|
||||
{
|
||||
"message" : "We are in development",
|
||||
"db" : {
|
||||
"addr" : "localhost",
|
||||
"port" : "27017",
|
||||
"name" : "test"
|
||||
},
|
||||
"port" : "7070",
|
||||
"tokenSecret": "Pariz-Beograd",
|
||||
"userTokenExpirePeriod": "10080",
|
||||
"limiter" : {
|
||||
"defaultBurstRate": 50,
|
||||
"defaultRatePerSec": 0.5
|
||||
},
|
||||
"version": "0.1"
|
||||
}
|
35
gulpfile.js
35
gulpfile.js
@ -1,35 +0,0 @@
|
||||
/* File: gulpfile.js */
|
||||
|
||||
// grab our packages
|
||||
var gulp = require('gulp');
|
||||
var jshint = require('gulp-jshint');
|
||||
var nodemon = require('gulp-nodemon');
|
||||
var mocha = require('gulp-mocha');
|
||||
var exit = require('gulp-exit');
|
||||
|
||||
// define the default task and add the watch task to it
|
||||
gulp.task('default', ['watch']);
|
||||
|
||||
// configure the jshint task
|
||||
gulp.task('lint', function() {
|
||||
return gulp.src('app/**/*.js')
|
||||
.pipe(jshint())
|
||||
.pipe(jshint.reporter('jshint-stylish'));
|
||||
});
|
||||
|
||||
// configure which files to watch and what tasks to use on file changes
|
||||
gulp.task('watch', function() {
|
||||
gulp.watch('app/**/*.js', ['jshint']);
|
||||
|
||||
// Start up the server and have it reload when anything in the
|
||||
// ./build/ directory changes
|
||||
nodemon({script: 'mainflux.js', watch: 'app/**'});
|
||||
});
|
||||
|
||||
gulp.task('test', function() {
|
||||
return gulp
|
||||
.src('test/*.js')
|
||||
.pipe(mocha())
|
||||
.pipe(exit());
|
||||
});
|
||||
|
@ -1,72 +0,0 @@
|
||||
/**
|
||||
* 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;
|
42
mainflux.js
42
mainflux.js
@ -1,42 +0,0 @@
|
||||
/**
|
||||
* 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 domain = require('domain');
|
||||
var config = require('./config');
|
||||
var log = require('./app/logger');
|
||||
|
||||
var httpApi = require('./httpServer');
|
||||
var mqttApi = require('./mqttServer');
|
||||
var mqttApi = require('./wsServer');
|
||||
|
||||
var mainflux = {};
|
||||
|
||||
|
||||
var banner = `
|
||||
oocccdMMMMMMMMMWOkkkkoooolcclX
|
||||
llc:::0MMMMMMMM0xxxxxdlllc:::d
|
||||
lll:::cXMMMMMMXxxxxxxxdlllc:::
|
||||
lllc:::cXMMMMNkxxxdxxxxolllc::
|
||||
olllc:::oWMMNkxxxdloxxxxolllc: ## ## ### #### ## ## ######## ## ## ## ## ##
|
||||
xolllc:::xWWOxxxdllloxxxxolllc ### ### ## ## ## ### ## ## ## ## ## ## ##
|
||||
xxolllc:::x0xxxdllll:oxxxxllll #### #### ## ## ## #### ## ## ## ## ## ## ##
|
||||
xxxolllc::oxxxxllll:::dxxxdlll ## ### ## ## ## ## ## ## ## ###### ## ## ## ###
|
||||
xxxdllll:lxxxxolllc:::Okxxxdll ## ## ######### ## ## #### ## ## ## ## ## ##
|
||||
0xxxdllloxxxxolllc:::OMNkxxxdl ## ## ## ## ## ## ### ## ## ## ## ## ##
|
||||
W0xxxdllxxxxolllc:::xMMMXxxxxd ## ## ## ## #### ## ## ## ######## ####### ## ##
|
||||
MWOxxxdxxxxdlllc:::oWMMMMKxxxx
|
||||
MMWkxxxxxxdlllc:::oNMMMMMM0xxx
|
||||
MMMXxxxxxdllllc::cXMMMMMMMWOxx
|
||||
MMMM0xxxxolllc:::kMMMMMMMMMXxx
|
||||
`
|
||||
|
||||
console.log(banner);
|
||||
|
||||
/**
|
||||
* Exports
|
||||
*/
|
||||
module.exports = mainflux;
|
@ -1,37 +0,0 @@
|
||||
/**
|
||||
* 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;
|
37
package.json
37
package.json
@ -1,37 +0,0 @@
|
||||
{
|
||||
"name": "node-api",
|
||||
"description": "Mainflux is an open source MIT licensed IoT cloud written in NodeJS",
|
||||
"main": "server.js",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/Mainflux/mainflux"
|
||||
},
|
||||
"license": "Apache-2.0",
|
||||
"scripts": {
|
||||
"test": "node_modules/.bin/mocha"
|
||||
},
|
||||
"dependencies": {
|
||||
"bcrypt": "~0.8.5",
|
||||
"body-parser": "~1.0.1",
|
||||
"bunyan": "^1.5.1",
|
||||
"jsonwebtoken": "~5.0.5",
|
||||
"lodash": "~3.10.1",
|
||||
"mongojs": "^1.4.1",
|
||||
"mosca": "^1.2.0",
|
||||
"nats": "^0.6.2",
|
||||
"restify": "^4.0.3",
|
||||
"ws": "^1.1.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"chai": "^3.4.0",
|
||||
"gulp": "^3.9.0",
|
||||
"gulp-exit": "0.0.2",
|
||||
"gulp-jshint": "^1.11.2",
|
||||
"gulp-mocha": "^2.1.3",
|
||||
"gulp-nodemon": "^2.0.3",
|
||||
"jshint-stylish": "^2.0.1",
|
||||
"mocha": "^2.3.3",
|
||||
"restify-jwt": "^0.4.0",
|
||||
"supertest": "^1.1.0"
|
||||
}
|
||||
}
|
167
swagger.yaml
167
swagger.yaml
@ -1,167 +0,0 @@
|
||||
swagger: '2.0'
|
||||
info:
|
||||
version: 0.0.1
|
||||
title: Mainflux
|
||||
termsOfService: 'http://mainflux.com/tos'
|
||||
contact:
|
||||
name: Mainflux
|
||||
url: 'http://wwww.mainflux.com'
|
||||
email: info@mainflux.com
|
||||
license:
|
||||
name: Apache-2.0
|
||||
url: 'http://opensource.org/licenses/Apache-2.0'
|
||||
host: api.mainflux.com
|
||||
basePath: /
|
||||
schemes:
|
||||
- http
|
||||
- https
|
||||
consumes:
|
||||
- application/json
|
||||
- text/xml
|
||||
produces:
|
||||
- application/json
|
||||
- text/html
|
||||
paths:
|
||||
/status:
|
||||
get:
|
||||
description: |
|
||||
Gets Mainflux server status.
|
||||
tags:
|
||||
- status
|
||||
responses:
|
||||
'200':
|
||||
description: Server is running
|
||||
schema:
|
||||
title: Status
|
||||
type: string
|
||||
/devices:
|
||||
get:
|
||||
description: |
|
||||
Gets all of the existing `Device` objects.
|
||||
tags:
|
||||
- devices
|
||||
parameters:
|
||||
- name: X-Auth-Token
|
||||
in: header
|
||||
description: authentification token
|
||||
required: true
|
||||
type: number
|
||||
format: double
|
||||
responses:
|
||||
'200':
|
||||
description: Successful response
|
||||
schema:
|
||||
title: ArrayOfDevices
|
||||
type: array
|
||||
items:
|
||||
title: Device
|
||||
type: object
|
||||
properties:
|
||||
uuid:
|
||||
type: number
|
||||
post:
|
||||
description: |
|
||||
Creates `Device` object.
|
||||
Returns newly created Device object.
|
||||
tags:
|
||||
- devices
|
||||
parameters:
|
||||
- name: X-Auth-Token
|
||||
in: header
|
||||
description: authentification token
|
||||
required: true
|
||||
type: number
|
||||
format: double
|
||||
responses:
|
||||
'200':
|
||||
description: Successful response
|
||||
schema:
|
||||
title: Device
|
||||
type: object
|
||||
properties:
|
||||
uuid:
|
||||
type: number
|
||||
'/devices/{device_id}':
|
||||
get:
|
||||
description: |
|
||||
Gets `Device` object from the database by `deviceUuid`.
|
||||
tags:
|
||||
- devices
|
||||
parameters:
|
||||
- name: X-Auth-Token
|
||||
in: header
|
||||
description: authentification token
|
||||
required: true
|
||||
type: number
|
||||
format: double
|
||||
- name: device_id
|
||||
in: path
|
||||
description: Device UUID
|
||||
required: true
|
||||
type: string
|
||||
responses:
|
||||
'200':
|
||||
description: Successful response
|
||||
schema:
|
||||
title: Thing
|
||||
type: object
|
||||
properties:
|
||||
uuid:
|
||||
type: number
|
||||
put:
|
||||
description: |
|
||||
Updates Thing object from the database.
|
||||
tags:
|
||||
- devices
|
||||
parameters:
|
||||
- name: X-Auth-Token
|
||||
in: header
|
||||
description: authentification token
|
||||
required: true
|
||||
type: number
|
||||
format: double
|
||||
- name: device_id
|
||||
in: path
|
||||
description: Device UUID
|
||||
required: true
|
||||
type: string
|
||||
- name: params
|
||||
in: query
|
||||
description: Device parameters
|
||||
required: true
|
||||
type: string
|
||||
responses:
|
||||
'200':
|
||||
description: Successful response
|
||||
schema:
|
||||
title: Thing
|
||||
type: object
|
||||
properties:
|
||||
uuid:
|
||||
type: number
|
||||
delete:
|
||||
description: |
|
||||
Deletes Thing object from the database.
|
||||
tags:
|
||||
- devices
|
||||
parameters:
|
||||
- name: X-Auth-Token
|
||||
in: header
|
||||
description: authentification token
|
||||
required: true
|
||||
type: number
|
||||
format: double
|
||||
- name: device_id
|
||||
in: path
|
||||
description: Device UUID
|
||||
required: true
|
||||
type: string
|
||||
responses:
|
||||
'200':
|
||||
description: Successful response
|
||||
schema:
|
||||
title: Thing
|
||||
type: object
|
||||
properties:
|
||||
uuid:
|
||||
type: number
|
@ -1,40 +0,0 @@
|
||||
/** Chai stuff */
|
||||
var should = require('chai').should;
|
||||
var expect = require('chai').expect;
|
||||
|
||||
/** Supertest for API */
|
||||
var supertest = require('supertest');
|
||||
var server = require('../server');
|
||||
var api = supertest(server);
|
||||
|
||||
/**
|
||||
* API test description
|
||||
*/
|
||||
describe('loading express', function () {
|
||||
/**
|
||||
* /status
|
||||
*/
|
||||
it('responds to /status', function testSlash(done) {
|
||||
api
|
||||
.get('/status')
|
||||
.expect(200)
|
||||
.end(function(err, res){
|
||||
expect(res.body.status).to.equal("running");
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
/**
|
||||
* /foo/bar
|
||||
*/
|
||||
it('404 /foo/bar', function testPath(done) {
|
||||
api
|
||||
.get('/foo/bar')
|
||||
.expect(404, done);
|
||||
});
|
||||
});
|
||||
|
||||
after(function(done) {
|
||||
server.close(done);
|
||||
});
|
||||
|
@ -1,9 +0,0 @@
|
||||
var jwt = require('jsonwebtoken');
|
||||
|
||||
var config = require('../config/config');
|
||||
|
||||
var token = jwt.sign({foo: 'bar'}, config.tokenSecret, {
|
||||
expiresInMinutes: config.userTokenExpirePeriod
|
||||
});
|
||||
|
||||
console.log(token);
|
25
wsServer.js
25
wsServer.js
@ -1,25 +0,0 @@
|
||||
/**
|
||||
* 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;
|
Loading…
x
Reference in New Issue
Block a user