1
0
mirror of https://github.com/mainflux/mainflux.git synced 2025-05-04 22:17:59 +08:00

Merge pull request #33 from drasko/master

Correct Swagger file
This commit is contained in:
Drasko DRASKOVIC 2015-12-04 01:16:44 +01:00
commit 980f6ac525
5 changed files with 178 additions and 296 deletions

View File

@ -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)

View File

@ -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,

View File

@ -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"
} }
} }

View File

@ -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();

View File

@ -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:
# Describe this verb here. Note: you can use markdown
description: | description: |
Gets Mainflux server status. Gets Mainflux server status.
# Expected responses for this operation:
responses: responses:
# Response code '200':
200:
description: Server is running description: Server is running
# A schema describing your response object.
# Use JSON Schema format
schema: schema:
title: Status title: Status
type: string type: string
/devices:
/things:
# This is a HTTP operation
get: get:
# Describe this verb here. Note: you can use markdown
description: | description: |
Gets all of the existing `Thing` objects. Gets all of the existing `Device` objects.
# This is array of GET operation parameters:
parameters: parameters:
# An example parameter that is in query and is required - name: X-Auth-Token
- in: header
name: authUuid
in: query
description: authentification UUID
required: true
type: string
-
name: authToken
in: query
description: authentification token description: authentification token
required: true required: true
type: number type: number
format: double format: double
# Expected responses for this operation:
responses: responses:
# Response code '200':
200:
description: Successful response description: Successful response
# A schema describing your response object.
# Use JSON Schema format
schema: schema:
title: ArrayOfThings title: ArrayOfDevices
type: array type: array
items: items:
title: Thing title: Device
type: object type: object
properties: properties:
uuid: uuid:
type: number type: number
name:
type: string
type:
type: string
manufacturer:
type: string
post: post:
# Describe this verb here. Note: you can use markdown
description: | description: |
Creates `Thing` object. Creates `Device` object.
Returns newly created Thing object. Returns newly created Device object.
# This is array of GET operation parameters:
parameters: parameters:
# An example parameter that is in query and is required - name: X-Auth-Token
- in: header
name: authUuid
in: query
description: authentification UUID
required: true
type: string
-
name: authToken
in: query
description: authentification token description: authentification token
required: true required: true
type: number type: number
format: double format: double
# Expected responses for this operation:
responses: responses:
# Response code '200':
200:
description: Successful response description: Successful response
# A schema describing your response object.
# Use JSON Schema format
schema: schema:
title: Thing title: Device
type: object type: object
properties: properties:
uuid: uuid:
type: number type: number
name: '/devices/{device_id}':
type: string
type:
type: string
manufacturer:
type: string
/things/{thingUuid}:
# This is a HTTP operation
get: get:
# Describe this verb here. Note: you can use markdown
description: | description: |
Gets Thing object from the database by thingUuid. Gets `Device` object from the database by `deviceUuid`.
# This is array of GET operation parameters:
parameters: parameters:
# An example parameter that is in query and is required - name: X-Auth-Token
- in: header
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 description: authentification token
required: true required: true
type: number type: number
format: double format: double
- name: device_id
in: path
# Expected responses for this operation: description: Device UUID
required: true
type: string
responses: responses:
# Response code '200':
200:
description: Successful response description: Successful response
# A schema describing your response object.
# Use JSON Schema format
schema: schema:
title: Thing title: Thing
type: object type: object
properties: properties:
uuid: uuid:
type: number type: number
name:
type: string
type:
type: string
manufacturer:
type: string
put: put:
# Describe this verb here. Note: you can use markdown
description: | description: |
Updates Thing object from the database. Updates Thing object from the database.
# This is array of GET operation parameters:
parameters: parameters:
# An example parameter that is in query and is required - name: X-Auth-Token
- in: header
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 description: authentification token
required: true required: true
type: number type: number
format: double format: double
- name: device_id
in: path
# Expected responses for this operation: description: Device UUID
required: true
type: string
- name: params
in: query
description: Device parameters
required: true
type: string
responses: responses:
# Response code '200':
200:
description: Successful response description: Successful response
# A schema describing your response object.
# Use JSON Schema format
schema: schema:
title: Thing title: Thing
type: object type: object
properties: properties:
uuid: uuid:
type: number type: number
name:
type: string
type:
type: string
manufacturer:
type: string
delete: delete:
# Describe this verb here. Note: you can use markdown
description: | description: |
Deletes Thing object from the database. Deletes Thing object from the database.
# This is array of GET operation parameters:
parameters: parameters:
# An example parameter that is in query and is required - name: X-Auth-Token
- in: header
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 description: authentification token
required: true required: true
type: number type: number
format: double format: double
- name: device_id
in: path
# Expected responses for this operation: description: Device UUID
required: true
type: string
responses: responses:
# Response code '200':
200:
description: Successful response description: Successful response
# A schema describing your response object.
# Use JSON Schema format
schema: schema:
title: Thing title: Thing
type: object type: object
properties: properties:
uuid: uuid:
type: number type: number
name:
type: string
type:
type: string
manufacturer:
type: string