mirror of
https://github.com/mainflux/mainflux.git
synced 2025-05-02 22:17:10 +08:00
commit
980f6ac525
@ -1,6 +1,9 @@
|
|||||||
var mongojs = require('mongojs');
|
var mongojs = require('mongojs');
|
||||||
var devicesDb = require('../database').collection('devices');
|
var devicesDb = require('../database').collection('devices');
|
||||||
|
|
||||||
|
var jwt = require('jsonwebtoken');
|
||||||
|
var config = require('../../config/config');
|
||||||
|
|
||||||
/** createDevice() */
|
/** createDevice() */
|
||||||
exports.createDevice = function(req, res, next) {
|
exports.createDevice = function(req, res, next) {
|
||||||
|
|
||||||
@ -9,9 +12,17 @@ exports.createDevice = function(req, res, next) {
|
|||||||
/** Save the device and check for errors */
|
/** Save the device and check for errors */
|
||||||
devicesDb.insert(req.body, function(err, device) {
|
devicesDb.insert(req.body, function(err, device) {
|
||||||
if (err)
|
if (err)
|
||||||
res.send(err);
|
return next(err);
|
||||||
|
|
||||||
res.json(device);
|
var token = jwt.sign(device, config.tokenSecret, {
|
||||||
|
expiresInMinutes: config.userTokenExpirePeriod
|
||||||
|
});
|
||||||
|
|
||||||
|
res.json({
|
||||||
|
status: 200,
|
||||||
|
message: 'Device created',
|
||||||
|
token: token
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
return next();
|
return next();
|
||||||
@ -24,7 +35,7 @@ exports.getAllDevices = function(req, res, next) {
|
|||||||
|
|
||||||
devicesDb.find(req.body, function(err, devices) {
|
devicesDb.find(req.body, function(err, devices) {
|
||||||
if (err)
|
if (err)
|
||||||
res.send(err);
|
return next(err);
|
||||||
|
|
||||||
res.json(devices);
|
res.json(devices);
|
||||||
return next();
|
return next();
|
||||||
@ -63,7 +74,7 @@ exports.updateDevice = function(req, res, next) {
|
|||||||
|
|
||||||
/** deleteDevice() */
|
/** deleteDevice() */
|
||||||
exports.deleteDevice = function(req, res, next) {
|
exports.deleteDevice = function(req, res, next) {
|
||||||
deviceDb.remove({
|
devicesDb.remove({
|
||||||
_id: mongojs.ObjectId(req.params.device_id)
|
_id: mongojs.ObjectId(req.params.device_id)
|
||||||
}, function(err, device) {
|
}, function(err, device) {
|
||||||
if (err)
|
if (err)
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
"name" : "test"
|
"name" : "test"
|
||||||
},
|
},
|
||||||
"port" : "8080",
|
"port" : "8080",
|
||||||
"secretToken": "VelikaSrbija",
|
"tokenSecret": "Pariz-Beograd",
|
||||||
"userTokenExpirePeriod": "10080",
|
"userTokenExpirePeriod": "10080",
|
||||||
"limiter" : {
|
"limiter" : {
|
||||||
"defaultBurstRate": 50,
|
"defaultBurstRate": 50,
|
||||||
|
@ -27,6 +27,7 @@
|
|||||||
"gulp-nodemon": "^2.0.3",
|
"gulp-nodemon": "^2.0.3",
|
||||||
"jshint-stylish": "^2.0.1",
|
"jshint-stylish": "^2.0.1",
|
||||||
"mocha": "^2.3.3",
|
"mocha": "^2.3.3",
|
||||||
|
"restify-jwt": "^0.4.0",
|
||||||
"supertest": "^1.1.0"
|
"supertest": "^1.1.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
21
server.js
21
server.js
@ -6,6 +6,7 @@
|
|||||||
* See the included LICENSE file for more details.
|
* See the included LICENSE file for more details.
|
||||||
*/
|
*/
|
||||||
var restify = require('restify');
|
var restify = require('restify');
|
||||||
|
var jwt = require('restify-jwt');
|
||||||
var domain = require('domain');
|
var domain = require('domain');
|
||||||
var config = require('./config/config');
|
var config = require('./config/config');
|
||||||
|
|
||||||
@ -29,7 +30,25 @@ console.log('Enabling CORS');
|
|||||||
server.use(restify.CORS());
|
server.use(restify.CORS());
|
||||||
server.use(restify.fullResponse());
|
server.use(restify.fullResponse());
|
||||||
|
|
||||||
//Global error handler
|
/** JWT */
|
||||||
|
server.use(jwt({
|
||||||
|
secret: config.tokenSecret,
|
||||||
|
requestProperty: 'token',
|
||||||
|
getToken: function fromHeaderOrQuerystring(req) {
|
||||||
|
var token = (req.body && req.body.access_token) ||
|
||||||
|
(req.query && req.query.access_token) ||
|
||||||
|
req.headers['x-auth-token'];
|
||||||
|
|
||||||
|
return token;
|
||||||
|
}
|
||||||
|
}).unless({
|
||||||
|
path: [
|
||||||
|
'/status',
|
||||||
|
{url: '/devices', methods: ['POST']}
|
||||||
|
]
|
||||||
|
}));
|
||||||
|
|
||||||
|
/** Global error handler */
|
||||||
server.use(function(req, res, next) {
|
server.use(function(req, res, next) {
|
||||||
var domainHandler = domain.create();
|
var domainHandler = domain.create();
|
||||||
|
|
||||||
|
431
swagger.yaml
431
swagger.yaml
@ -1,293 +1,144 @@
|
|||||||
swagger: '2.0'
|
swagger: '2.0'
|
||||||
|
|
||||||
# Document metadata
|
|
||||||
info:
|
info:
|
||||||
version: "0.0.1"
|
version: 0.0.1
|
||||||
title: Mainflux
|
title: Mainflux
|
||||||
termsOfService: http://swagger.io/terms/
|
termsOfService: 'http://mainflux.com/tos'
|
||||||
contact:
|
contact:
|
||||||
name: API Support
|
name: Mainflux
|
||||||
url: http://wwww.mainflux.com/support
|
url: 'http://wwww.mainflux.com'
|
||||||
email: support@mainflux.com
|
email: info@mainflux.com
|
||||||
license:
|
license:
|
||||||
name: MIT
|
name: Apache-2.0
|
||||||
url: http://opensource.org/licenses/MIT
|
url: 'http://opensource.org/licenses/Apache-2.0'
|
||||||
|
|
||||||
paths:
|
paths:
|
||||||
/status:
|
/status:
|
||||||
# This is a HTTP operation
|
get:
|
||||||
get:
|
description: |
|
||||||
# Describe this verb here. Note: you can use markdown
|
Gets Mainflux server status.
|
||||||
description: |
|
responses:
|
||||||
Gets Mainflux server status.
|
'200':
|
||||||
|
description: Server is running
|
||||||
# Expected responses for this operation:
|
schema:
|
||||||
responses:
|
title: Status
|
||||||
# Response code
|
type: string
|
||||||
200:
|
/devices:
|
||||||
description: Server is running
|
get:
|
||||||
|
description: |
|
||||||
# A schema describing your response object.
|
Gets all of the existing `Device` objects.
|
||||||
# Use JSON Schema format
|
parameters:
|
||||||
schema:
|
- name: X-Auth-Token
|
||||||
title: Status
|
in: header
|
||||||
type: string
|
description: authentification token
|
||||||
|
required: true
|
||||||
/things:
|
type: number
|
||||||
# This is a HTTP operation
|
format: double
|
||||||
get:
|
responses:
|
||||||
# Describe this verb here. Note: you can use markdown
|
'200':
|
||||||
description: |
|
description: Successful response
|
||||||
Gets all of the existing `Thing` objects.
|
schema:
|
||||||
|
title: ArrayOfDevices
|
||||||
# This is array of GET operation parameters:
|
type: array
|
||||||
parameters:
|
items:
|
||||||
# An example parameter that is in query and is required
|
title: Device
|
||||||
-
|
type: object
|
||||||
name: authUuid
|
properties:
|
||||||
in: query
|
uuid:
|
||||||
description: authentification UUID
|
type: number
|
||||||
required: true
|
post:
|
||||||
type: string
|
description: |
|
||||||
|
Creates `Device` object.
|
||||||
-
|
Returns newly created Device object.
|
||||||
name: authToken
|
parameters:
|
||||||
in: query
|
- name: X-Auth-Token
|
||||||
description: authentification token
|
in: header
|
||||||
required: true
|
description: authentification token
|
||||||
type: number
|
required: true
|
||||||
format: double
|
type: number
|
||||||
|
format: double
|
||||||
|
responses:
|
||||||
# Expected responses for this operation:
|
'200':
|
||||||
responses:
|
description: Successful response
|
||||||
# Response code
|
schema:
|
||||||
200:
|
title: Device
|
||||||
description: Successful response
|
type: object
|
||||||
|
properties:
|
||||||
# A schema describing your response object.
|
uuid:
|
||||||
# Use JSON Schema format
|
type: number
|
||||||
schema:
|
'/devices/{device_id}':
|
||||||
title: ArrayOfThings
|
get:
|
||||||
type: array
|
description: |
|
||||||
items:
|
Gets `Device` object from the database by `deviceUuid`.
|
||||||
title: Thing
|
parameters:
|
||||||
type: object
|
- name: X-Auth-Token
|
||||||
properties:
|
in: header
|
||||||
uuid:
|
description: authentification token
|
||||||
type: number
|
required: true
|
||||||
name:
|
type: number
|
||||||
type: string
|
format: double
|
||||||
type:
|
- name: device_id
|
||||||
type: string
|
in: path
|
||||||
manufacturer:
|
description: Device UUID
|
||||||
type: string
|
required: true
|
||||||
post:
|
type: string
|
||||||
# Describe this verb here. Note: you can use markdown
|
responses:
|
||||||
description: |
|
'200':
|
||||||
Creates `Thing` object.
|
description: Successful response
|
||||||
Returns newly created Thing object.
|
schema:
|
||||||
|
title: Thing
|
||||||
# This is array of GET operation parameters:
|
type: object
|
||||||
parameters:
|
properties:
|
||||||
# An example parameter that is in query and is required
|
uuid:
|
||||||
-
|
type: number
|
||||||
name: authUuid
|
put:
|
||||||
in: query
|
description: |
|
||||||
description: authentification UUID
|
Updates Thing object from the database.
|
||||||
required: true
|
parameters:
|
||||||
type: string
|
- name: X-Auth-Token
|
||||||
|
in: header
|
||||||
-
|
description: authentification token
|
||||||
name: authToken
|
required: true
|
||||||
in: query
|
type: number
|
||||||
description: authentification token
|
format: double
|
||||||
required: true
|
- name: device_id
|
||||||
type: number
|
in: path
|
||||||
format: double
|
description: Device UUID
|
||||||
|
required: true
|
||||||
|
type: string
|
||||||
# Expected responses for this operation:
|
- name: params
|
||||||
responses:
|
in: query
|
||||||
# Response code
|
description: Device parameters
|
||||||
200:
|
required: true
|
||||||
description: Successful response
|
type: string
|
||||||
|
responses:
|
||||||
# A schema describing your response object.
|
'200':
|
||||||
# Use JSON Schema format
|
description: Successful response
|
||||||
schema:
|
schema:
|
||||||
title: Thing
|
title: Thing
|
||||||
type: object
|
type: object
|
||||||
properties:
|
properties:
|
||||||
uuid:
|
uuid:
|
||||||
type: number
|
type: number
|
||||||
name:
|
delete:
|
||||||
type: string
|
description: |
|
||||||
type:
|
Deletes Thing object from the database.
|
||||||
type: string
|
parameters:
|
||||||
manufacturer:
|
- name: X-Auth-Token
|
||||||
type: string
|
in: header
|
||||||
/things/{thingUuid}:
|
description: authentification token
|
||||||
# This is a HTTP operation
|
required: true
|
||||||
get:
|
type: number
|
||||||
# Describe this verb here. Note: you can use markdown
|
format: double
|
||||||
description: |
|
- name: device_id
|
||||||
Gets Thing object from the database by thingUuid.
|
in: path
|
||||||
|
description: Device UUID
|
||||||
# This is array of GET operation parameters:
|
required: true
|
||||||
parameters:
|
type: string
|
||||||
# An example parameter that is in query and is required
|
responses:
|
||||||
-
|
'200':
|
||||||
name: thingUuid
|
description: Successful response
|
||||||
in: path
|
schema:
|
||||||
description: thing UUID
|
title: Thing
|
||||||
required: true
|
type: object
|
||||||
type: string
|
properties:
|
||||||
-
|
uuid:
|
||||||
name: authUuid
|
type: number
|
||||||
in: query
|
|
||||||
description: authentification UUID
|
|
||||||
required: true
|
|
||||||
type: string
|
|
||||||
|
|
||||||
-
|
|
||||||
name: authToken
|
|
||||||
in: query
|
|
||||||
description: authentification token
|
|
||||||
required: true
|
|
||||||
type: number
|
|
||||||
format: double
|
|
||||||
|
|
||||||
|
|
||||||
# Expected responses for this operation:
|
|
||||||
responses:
|
|
||||||
# Response code
|
|
||||||
200:
|
|
||||||
description: Successful response
|
|
||||||
|
|
||||||
# A schema describing your response object.
|
|
||||||
# Use JSON Schema format
|
|
||||||
schema:
|
|
||||||
title: Thing
|
|
||||||
type: object
|
|
||||||
properties:
|
|
||||||
uuid:
|
|
||||||
type: number
|
|
||||||
name:
|
|
||||||
type: string
|
|
||||||
type:
|
|
||||||
type: string
|
|
||||||
manufacturer:
|
|
||||||
type: string
|
|
||||||
|
|
||||||
put:
|
|
||||||
# Describe this verb here. Note: you can use markdown
|
|
||||||
description: |
|
|
||||||
Updates Thing object from the database.
|
|
||||||
|
|
||||||
# This is array of GET operation parameters:
|
|
||||||
parameters:
|
|
||||||
# An example parameter that is in query and is required
|
|
||||||
-
|
|
||||||
name: thingUuid
|
|
||||||
in: path
|
|
||||||
description: thing UUID
|
|
||||||
required: true
|
|
||||||
type: string
|
|
||||||
-
|
|
||||||
name: params
|
|
||||||
in: query
|
|
||||||
description: thing parameters
|
|
||||||
required: true
|
|
||||||
type: string
|
|
||||||
-
|
|
||||||
name: authUuid
|
|
||||||
in: query
|
|
||||||
description: authentification UUID
|
|
||||||
required: true
|
|
||||||
type: string
|
|
||||||
|
|
||||||
-
|
|
||||||
name: authToken
|
|
||||||
in: query
|
|
||||||
description: authentification token
|
|
||||||
required: true
|
|
||||||
type: number
|
|
||||||
format: double
|
|
||||||
|
|
||||||
|
|
||||||
# Expected responses for this operation:
|
|
||||||
responses:
|
|
||||||
# Response code
|
|
||||||
200:
|
|
||||||
description: Successful response
|
|
||||||
|
|
||||||
# A schema describing your response object.
|
|
||||||
# Use JSON Schema format
|
|
||||||
schema:
|
|
||||||
title: Thing
|
|
||||||
type: object
|
|
||||||
properties:
|
|
||||||
uuid:
|
|
||||||
type: number
|
|
||||||
name:
|
|
||||||
type: string
|
|
||||||
type:
|
|
||||||
type: string
|
|
||||||
manufacturer:
|
|
||||||
type: string
|
|
||||||
|
|
||||||
delete:
|
|
||||||
# Describe this verb here. Note: you can use markdown
|
|
||||||
description: |
|
|
||||||
Deletes Thing object from the database.
|
|
||||||
|
|
||||||
# This is array of GET operation parameters:
|
|
||||||
parameters:
|
|
||||||
# An example parameter that is in query and is required
|
|
||||||
-
|
|
||||||
name: thingUuid
|
|
||||||
in: path
|
|
||||||
description: thing UUID
|
|
||||||
required: true
|
|
||||||
type: string
|
|
||||||
-
|
|
||||||
name: authUuid
|
|
||||||
in: query
|
|
||||||
description: authentification UUID
|
|
||||||
required: true
|
|
||||||
type: string
|
|
||||||
|
|
||||||
-
|
|
||||||
name: authToken
|
|
||||||
in: query
|
|
||||||
description: authentification token
|
|
||||||
required: true
|
|
||||||
type: number
|
|
||||||
format: double
|
|
||||||
|
|
||||||
|
|
||||||
# Expected responses for this operation:
|
|
||||||
responses:
|
|
||||||
# Response code
|
|
||||||
200:
|
|
||||||
description: Successful response
|
|
||||||
|
|
||||||
# A schema describing your response object.
|
|
||||||
# Use JSON Schema format
|
|
||||||
schema:
|
|
||||||
title: Thing
|
|
||||||
type: object
|
|
||||||
properties:
|
|
||||||
uuid:
|
|
||||||
type: number
|
|
||||||
name:
|
|
||||||
type: string
|
|
||||||
type:
|
|
||||||
type: string
|
|
||||||
manufacturer:
|
|
||||||
type: string
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user