mirror of
https://github.com/openstf/stf
synced 2025-10-04 18:29:17 +02:00
Add basic api unit. This unit will be responsible for providing all stf restful apis.
This commit is contained in:
parent
7ea7871ec0
commit
41f306a7f0
7 changed files with 172 additions and 0 deletions
41
lib/cli.js
41
lib/cli.js
|
@ -758,6 +758,9 @@ program
|
||||||
.option('-u, --app-url <url>'
|
.option('-u, --app-url <url>'
|
||||||
, 'URL to app'
|
, 'URL to app'
|
||||||
, String)
|
, String)
|
||||||
|
.option('-u, --api-url <url>'
|
||||||
|
, 'URL to api'
|
||||||
|
, String)
|
||||||
.option('-a, --auth-url <url>'
|
.option('-a, --auth-url <url>'
|
||||||
, 'URL to auth client'
|
, 'URL to auth client'
|
||||||
, String)
|
, String)
|
||||||
|
@ -780,6 +783,9 @@ program
|
||||||
if (!options.authUrl) {
|
if (!options.authUrl) {
|
||||||
this.missingArgument('--auth-url')
|
this.missingArgument('--auth-url')
|
||||||
}
|
}
|
||||||
|
if (!options.apiUrl) {
|
||||||
|
this.missingArgument('--api-url')
|
||||||
|
}
|
||||||
if (!options.websocketUrl) {
|
if (!options.websocketUrl) {
|
||||||
this.missingArgument('--websocket-url')
|
this.missingArgument('--websocket-url')
|
||||||
}
|
}
|
||||||
|
@ -796,6 +802,7 @@ program
|
||||||
require('./units/poorxy')({
|
require('./units/poorxy')({
|
||||||
port: options.port
|
port: options.port
|
||||||
, appUrl: options.appUrl
|
, appUrl: options.appUrl
|
||||||
|
, apiUrl: options.apiUrl
|
||||||
, authUrl: options.authUrl
|
, authUrl: options.authUrl
|
||||||
, websocketUrl: options.websocketUrl
|
, websocketUrl: options.websocketUrl
|
||||||
, storageUrl: options.storageUrl
|
, storageUrl: options.storageUrl
|
||||||
|
@ -849,6 +856,28 @@ program
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
program
|
||||||
|
.command('api')
|
||||||
|
.description('start api')
|
||||||
|
.option('-p, --port <port>'
|
||||||
|
, 'port (or $PORT)'
|
||||||
|
, Number
|
||||||
|
, process.env.PORT || 7106)
|
||||||
|
.option('-s, --secret <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
|
program
|
||||||
.command('websocket')
|
.command('websocket')
|
||||||
.description('start websocket')
|
.description('start websocket')
|
||||||
|
@ -1110,6 +1139,10 @@ program
|
||||||
, 'app port'
|
, 'app port'
|
||||||
, Number
|
, Number
|
||||||
, 7105)
|
, 7105)
|
||||||
|
.option('--api-port <port>'
|
||||||
|
, 'api port'
|
||||||
|
, Number
|
||||||
|
, 7106)
|
||||||
.option('--websocket-port <port>'
|
.option('--websocket-port <port>'
|
||||||
, 'websocket port'
|
, 'websocket port'
|
||||||
, Number
|
, Number
|
||||||
|
@ -1285,6 +1318,12 @@ program
|
||||||
return extra
|
return extra
|
||||||
})()))
|
})()))
|
||||||
|
|
||||||
|
// api
|
||||||
|
, procutil.fork(__filename, [
|
||||||
|
'api'
|
||||||
|
, '--port', options.apiPort
|
||||||
|
, '--secret', options.authSecret
|
||||||
|
])
|
||||||
// websocket
|
// websocket
|
||||||
, procutil.fork(__filename, [
|
, procutil.fork(__filename, [
|
||||||
'websocket'
|
'websocket'
|
||||||
|
@ -1326,6 +1365,8 @@ program
|
||||||
, util.format('http://localhost:%d/', options.appPort)
|
, util.format('http://localhost:%d/', options.appPort)
|
||||||
, '--auth-url'
|
, '--auth-url'
|
||||||
, util.format('http://localhost:%d/', options.authPort)
|
, util.format('http://localhost:%d/', options.authPort)
|
||||||
|
, '--api-url'
|
||||||
|
, util.format('http://localhost:%d/', options.apiPort)
|
||||||
, '--websocket-url'
|
, '--websocket-url'
|
||||||
, util.format('http://localhost:%d/', options.websocketPort)
|
, util.format('http://localhost:%d/', options.websocketPort)
|
||||||
, '--storage-url'
|
, '--storage-url'
|
||||||
|
|
38
lib/units/api/config/default.yaml
Normal file
38
lib/units/api/config/default.yaml
Normal file
|
@ -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...
|
10
lib/units/api/controllers/user.js
Normal file
10
lib/units/api/controllers/user.js
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
module.exports = {
|
||||||
|
getCurrentUser: getCurrentUser
|
||||||
|
};
|
||||||
|
|
||||||
|
function getCurrentUser(req, res) {
|
||||||
|
res.json({
|
||||||
|
success: true
|
||||||
|
, user: {"name": "dummy"}
|
||||||
|
})
|
||||||
|
}
|
27
lib/units/api/index.js
Normal file
27
lib/units/api/index.js
Normal file
|
@ -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)
|
||||||
|
}
|
48
lib/units/api/swagger/api_v1.yaml
Normal file
48
lib/units/api/swagger/api_v1.yaml
Normal file
|
@ -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
|
|
@ -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) {
|
app.use(function(req, res) {
|
||||||
proxy.web(req, res, {
|
proxy.web(req, res, {
|
||||||
target: options.appUrl
|
target: options.appUrl
|
||||||
|
|
|
@ -82,6 +82,7 @@
|
||||||
"stf-device-db": "^1.2.0",
|
"stf-device-db": "^1.2.0",
|
||||||
"stf-syrup": "^1.0.0",
|
"stf-syrup": "^1.0.0",
|
||||||
"stf-wiki": "^1.0.0",
|
"stf-wiki": "^1.0.0",
|
||||||
|
"swagger-express-mw": "^0.6.0",
|
||||||
"temp": "^0.8.1",
|
"temp": "^0.8.1",
|
||||||
"transliteration": "^0.1.1",
|
"transliteration": "^0.1.1",
|
||||||
"utf-8-validate": "^1.2.1",
|
"utf-8-validate": "^1.2.1",
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue