From 41f306a7f0906891fdf571d5e8ef9fea1a3a2ae0 Mon Sep 17 00:00:00 2001 From: Vishal Banthia Date: Wed, 2 Dec 2015 18:18:31 +0900 Subject: [PATCH] Add basic api unit. This unit will be responsible for providing all stf restful apis. --- lib/cli.js | 41 ++++++++++++++++++++++++++ lib/units/api/config/default.yaml | 38 ++++++++++++++++++++++++ lib/units/api/controllers/user.js | 10 +++++++ lib/units/api/index.js | 27 +++++++++++++++++ lib/units/api/swagger/api_v1.yaml | 48 +++++++++++++++++++++++++++++++ lib/units/poorxy/index.js | 7 +++++ package.json | 1 + 7 files changed, 172 insertions(+) create mode 100644 lib/units/api/config/default.yaml create mode 100644 lib/units/api/controllers/user.js create mode 100644 lib/units/api/index.js create mode 100644 lib/units/api/swagger/api_v1.yaml diff --git a/lib/cli.js b/lib/cli.js index d6680620..d9532099 100644 --- a/lib/cli.js +++ b/lib/cli.js @@ -758,6 +758,9 @@ program .option('-u, --app-url ' , 'URL to app' , String) + .option('-u, --api-url ' + , 'URL to api' + , String) .option('-a, --auth-url ' , 'URL to auth client' , String) @@ -780,6 +783,9 @@ program if (!options.authUrl) { this.missingArgument('--auth-url') } + if (!options.apiUrl) { + this.missingArgument('--api-url') + } if (!options.websocketUrl) { this.missingArgument('--websocket-url') } @@ -796,6 +802,7 @@ program require('./units/poorxy')({ port: options.port , appUrl: options.appUrl + , apiUrl: options.apiUrl , authUrl: options.authUrl , websocketUrl: options.websocketUrl , storageUrl: options.storageUrl @@ -849,6 +856,28 @@ program }) }) + program + .command('api') + .description('start api') + .option('-p, --port ' + , 'port (or $PORT)' + , Number + , process.env.PORT || 7106) + .option('-s, --secret ' + , 'secret (or $SECRET)' + , String + , process.env.SECRET) + .action(function(options) { + if (!options.secret) { + this.missingArgument('--secret') + } + + require('./units/api')({ + port: options.port + , secret: options.secret + }) + }) + program .command('websocket') .description('start websocket') @@ -1110,6 +1139,10 @@ program , 'app port' , Number , 7105) + .option('--api-port ' + , 'api port' + , Number + , 7106) .option('--websocket-port ' , 'websocket port' , Number @@ -1285,6 +1318,12 @@ program return extra })())) + // api + , procutil.fork(__filename, [ + 'api' + , '--port', options.apiPort + , '--secret', options.authSecret + ]) // websocket , procutil.fork(__filename, [ 'websocket' @@ -1326,6 +1365,8 @@ program , util.format('http://localhost:%d/', options.appPort) , '--auth-url' , util.format('http://localhost:%d/', options.authPort) + , '--api-url' + , util.format('http://localhost:%d/', options.apiPort) , '--websocket-url' , util.format('http://localhost:%d/', options.websocketPort) , '--storage-url' diff --git a/lib/units/api/config/default.yaml b/lib/units/api/config/default.yaml new file mode 100644 index 00000000..0cd0513f --- /dev/null +++ b/lib/units/api/config/default.yaml @@ -0,0 +1,38 @@ +# swagger configuration file + +# values in the swagger hash are system configuration for swagger-node +swagger: + + fittingsDirs: [ fittings ] + defaultPipe: null + swaggerControllerPipe: swagger_controllers # defines the standard processing pipe for controllers + swagger: 'swagger/api_v1.yaml' + + # values defined in the bagpipes key are the bagpipes pipes and fittings definitions + # (see https://github.com/apigee-127/bagpipes) + bagpipes: + + _router: + name: swagger_router + mockMode: false + mockControllersDirs: [ mocks ] + controllersDirs: [ controllers ] + + _swagger_validate: + name: swagger_validator + validateResponse: true + + # pipe for all swagger-node controllers + swagger_controllers: + - onError: json_error_handler + - cors + - swagger_security + - _swagger_validate + - express_compatibility + - _router + + # pipe to serve swagger (endpoint is in swagger.yaml) + swagger_raw: + name: swagger_raw + +# any other values in this file are just loaded into the config for application access... diff --git a/lib/units/api/controllers/user.js b/lib/units/api/controllers/user.js new file mode 100644 index 00000000..7ad97569 --- /dev/null +++ b/lib/units/api/controllers/user.js @@ -0,0 +1,10 @@ +module.exports = { + getCurrentUser: getCurrentUser +}; + +function getCurrentUser(req, res) { + res.json({ + success: true + , user: {"name": "dummy"} + }) +} diff --git a/lib/units/api/index.js b/lib/units/api/index.js new file mode 100644 index 00000000..23fd5397 --- /dev/null +++ b/lib/units/api/index.js @@ -0,0 +1,27 @@ +var http = require('http') +var path = require('path') + +var express = require('express') +var SwaggerExpress = require('swagger-express-mw') + +var logger = require('../../util/logger') + +module.exports = function(options) { + var log = logger.createLogger('api') + , app = express() + , server = http.createServer(app) + + var config = { + appRoot: __dirname + , swaggerFile: path.resolve(__dirname, 'swagger', 'api_v1.yaml') + }; + + SwaggerExpress.create(config, function(err, swaggerExpress) { + if (err) { throw err; } + + swaggerExpress.register(app); + }) + + server.listen(options.port) + log.info('Listening on port %d', options.port) +} diff --git a/lib/units/api/swagger/api_v1.yaml b/lib/units/api/swagger/api_v1.yaml new file mode 100644 index 00000000..fd5cbd8f --- /dev/null +++ b/lib/units/api/swagger/api_v1.yaml @@ -0,0 +1,48 @@ +swagger: "2.0" +info: + version: "1.0.10" + title: Smartphone Test Farm + description: Control and manager real Smartphone devices from browser and apis + license: + name: Apache-2.0 + url: http://www.apache.org/licenses/LICENSE-2.0 + contact: + url: http://openstf.io/ + email: contact@openstf.io +basePath: /api/v1/ +schemes: + - http + - https +consumes: + - application/json +produces: + - application/json +paths: + /me: + x-swagger-router-controller: user + get: + summary: User Profile + description: The User Profile endpoint returns information about current authorized user. + operationId: getCurrentUser + responses: + "200": + description: Current User Profile information + schema: + $ref: "#/definitions/UserResponse" + default: + description: Unexpected Error + schema: + $ref: "#/definitions/ErrorResponse" +definitions: + UserResponse: + required: + - user + properties: + user: + type: object + ErrorResponse: + required: + - message + properties: + message: + type: string diff --git a/lib/units/poorxy/index.js b/lib/units/poorxy/index.js index 1141d1b1..74f44fc5 100644 --- a/lib/units/poorxy/index.js +++ b/lib/units/poorxy/index.js @@ -51,6 +51,13 @@ module.exports = function(options) { }) }) + ;['/api/*'].forEach(function(route) { + app.all(route, function(req, res) { + proxy.web(req, res, { + target: options.apiUrl + }) + }) + }) app.use(function(req, res) { proxy.web(req, res, { target: options.appUrl diff --git a/package.json b/package.json index 98ce8ffe..996134e2 100644 --- a/package.json +++ b/package.json @@ -82,6 +82,7 @@ "stf-device-db": "^1.2.0", "stf-syrup": "^1.0.0", "stf-wiki": "^1.0.0", + "swagger-express-mw": "^0.6.0", "temp": "^0.8.1", "transliteration": "^0.1.1", "utf-8-validate": "^1.2.1",