diff --git a/docs/generateSwaggerAPI.js b/docs/generateSwaggerAPI.js new file mode 100644 index 0000000..07947fc --- /dev/null +++ b/docs/generateSwaggerAPI.js @@ -0,0 +1,47 @@ +/* +Node-OpenDroneMap Node.js App and REST API to access OpenDroneMap. +Copyright (C) 2016 Node-OpenDroneMap Contributors + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ +"use strict"; +let swaggerJSDoc = require('swagger-jsdoc'); +let fs = require('fs'); + +let options = { + swaggerDefinition: { + info: { + title: 'Node-OpenDroneMap', + version: '0.1.0', + license: { + name: 'GPLv3', + url: 'http://www.gnu.org/licenses/' + }, + contact: { + name: 'Piero Toffanin', + url: 'https://www.masseranolabs.com', + email: 'pt@masseranolabs.com' + } + }, + consumes: ["application/json"], + produces: ["application/json", "application/zip"], + basePath: "/", + schemes: ["http"] + }, + apis: ['../index.js'], // Path to the API docs +}; + +// Initialize swagger-jsdoc -> returns validated swagger spec in json format +let swaggerSpec = swaggerJSDoc(options); +fs.writeFileSync('swagger.json', JSON.stringify(swaggerSpec)); \ No newline at end of file diff --git a/docs/swagger.json b/docs/swagger.json new file mode 100644 index 0000000..d0f3d96 --- /dev/null +++ b/docs/swagger.json @@ -0,0 +1 @@ +{"info":{"title":"Node-OpenDroneMap","version":"0.1.0","license":{"name":"GPLv3","url":"http://www.gnu.org/licenses/"},"contact":{"name":"Piero Toffanin","url":"https://www.masseranolabs.com","email":"pt@masseranolabs.com"}},"consumes":["application/json"],"produces":["application/json"],"basePath":"/","schemes":["http"],"swagger":"2.0","paths":{"/task/cancel":{"post":{"description":"Cancels a task","parameters":[{"name":"uuid","in":"body","description":"UUID of the task to cancel","required":true,"schema":{"type":"string"}}],"responses":{"200":{"description":"Command Received","schema":{"$ref":"#/definitions/Response"}}}}}},"definitions":{"Response":{"type":"object","required":["success"],"properties":{"success":{"type":"boolean"},"error":{"type":"string","description":"Error message if an error occured"}}}},"responses":{},"parameters":{},"securityDefinitions":{},"tags":[]} \ No newline at end of file diff --git a/generateSwaggerAPI.js b/generateSwaggerAPI.js deleted file mode 100644 index 7fc7711..0000000 --- a/generateSwaggerAPI.js +++ /dev/null @@ -1,22 +0,0 @@ -var swaggerJSDoc = require('swagger-jsdoc'); -var jsonfile = require('jsonfile'); - -var options = { - swaggerDefinition: { - info: { - title: 'Node-OpenDroneMap', // Title (required) - version: '0.1.0', // Version (required) - }, - }, - apis: ['./index.js'], // Path to the API docs -}; - -// Initialize swagger-jsdoc -> returns validated swagger spec in json format -var swaggerSpec = swaggerJSDoc(options); - -var file = 'swagger-api.json'; - -jsonfile.writeFile(file, swaggerSpec, function (err) { - console.error(err); - process.exit(1); -}); diff --git a/index.js b/index.js index 2dd87ca..510f5f3 100644 --- a/index.js +++ b/index.js @@ -158,33 +158,89 @@ let uuidCheck = (req, res, next) => { else next(); }; +/** @swagger +* definition: +* Response: +* type: object +* required: +* - success +* properties: +* success: +* type: boolean +* error: +* type: string +* description: Error message if an error occured +*/ + let successHandler = res => { return err => { if (!err) res.json({success: true}); - else res.json({error: err.message}); + else res.json({success: false, error: err.message}); }; }; -/** - * @swagger - * /task/cancel/{uuidCheck}: - * get: - * description: Cancels a task - * parameters: - * - uuidCheck: uuid of the task to cancel - * responses: - * 200: - * description: Task cancelled - */ - +/** @swagger +* /task/cancel: +* post: +* description: Cancels a task (stops its execution, or prevents it from being executed) +* parameters: +* - +* name: uuid +* in: body +* description: UUID of the task to cancel +* required: true +* schema: +* type: string +* responses: +* 200: +* description: Command Received +* schema: +* $ref: "#/definitions/Response" +*/ app.post('/task/cancel', uuidCheck, (req, res) => { - taskManager.cancel(req.body.uuid, successHandler(res)); + taskManager.cancel(req.body.uuid, e(res)); }); +/** @swagger +* /task/remove: +* post: +* description: Removes a task and deletes all of its assets +* parameters: +* - +* name: uuid +* in: body +* description: UUID of the task to cancel +* required: true +* schema: +* type: string +* responses: +* 200: +* description: Command Received +* schema: +* $ref: "#/definitions/Response" +*/ app.post('/task/remove', uuidCheck, (req, res) => { taskManager.remove(req.body.uuid, successHandler(res)); }); +/** @swagger +* /task/restart: +* post: +* description: Restarts a task that was previously canceled or that had failed to process +* parameters: +* - +* name: uuid +* in: body +* description: UUID of the task to cancel +* required: true +* schema: +* type: string +* responses: +* 200: +* description: Command Received +* schema: +* $ref: "#/definitions/Response" +*/ app.post('/task/restart', uuidCheck, (req, res) => { taskManager.restart(req.body.uuid, successHandler(res)); }); diff --git a/package.json b/package.json index 3aa9092..f92f26c 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,6 @@ "async": "^2.0.0-rc.6", "body-parser": "^1.15.2", "express": "^4.14.0", - "jsonfile": "^2.3.1", "minimist": "^1.2.0", "morgan": "^1.7.0", "multer": "^1.1.0", diff --git a/swagger-api.json b/swagger-api.json deleted file mode 100644 index 79e18df..0000000 --- a/swagger-api.json +++ /dev/null @@ -1,29 +0,0 @@ -{ - "info": { - "title": "Node-OpenDroneMap", - "version": "0.1.0" - }, - "swagger": "2.0", - "paths": { - "/task/cancel/{uuidCheck}": { - "get": { - "description": "Cancels a task", - "parameters": [ - { - "uuidCheck": "uuid of the task to cancel" - } - ], - "responses": { - "200": { - "description": "Task cancelled" - } - } - } - } - }, - "definitions": {}, - "responses": {}, - "parameters": {}, - "securityDefinitions": {}, - "tags": [] -}