Skip to content

Commit

Permalink
Added swagger tools to use swagger UI
Browse files Browse the repository at this point in the history
but it can not load underscore.min.js, in node modules, I see
lodash.min.js. Maybe it's time to update swagger express mw to 0.6.0
and use Sway instead of swagger tools.

related issue #3
  • Loading branch information
at15 committed Apr 21, 2016
1 parent e0541d7 commit 95eaf8d
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 21 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,9 @@ Swagger API for bform
- `npm install`
- due to https://github.com/at15/bform-api/issues/1, change must be made to installed modules
> edit `node_modules/swagger/config/index.js` change `editorDir: path.resolve(config.nodeModules, 'swagger-editor'),` to `editorDir: path.dirname(require.resolve('swagger-editor'))`
- `npm run edit`
- `npm run edit`

## Generate json

`node util/convert.js` will generate a swagger.json file in `util` folder. You can use it to generate
module and classes in other frameworks
52 changes: 43 additions & 9 deletions api/swagger/swagger.yaml
Original file line number Diff line number Diff line change
@@ -1,22 +1,53 @@
---
swagger: "2.0"

################################################################################
# API Information #
################################################################################
info:
version: "0.0.1"
title: Hello World App
version: 0.0.1
title: Bform API
description:
Bform API Beta

################################################################################
# Host, Base Path, Schemes and Content Types #
################################################################################
# during dev, should point to your local machine
host: localhost:10010
# basePath prefixes all resource paths
basePath: /
#
basePath: /api/v1
schemes:
# tip: remove http to make production-grade
- http
- https
# format of bodies a client can send (Content-Type)
consumes:
- application/json
# format of the responses to the client (Accepts)
produces:
- application/json
consumes:
- application/json

################################################################################
# Tags #
################################################################################
tags:
- name: Users
description: |
User authentication and authorization
################################################################################
# parameters #
################################################################################
parameters:
id:
name: id
in: path
description: ID
type: integer
required: true

################################################################################
# Paths #
################################################################################
paths:
/hello:
# binds a127 app logic to a route
Expand Down Expand Up @@ -44,7 +75,10 @@ paths:
$ref: "#/definitions/ErrorResponse"
/swagger:
x-swagger-pipe: swagger_raw
# complex objects have schema definitions

################################################################################
# Definitions #
################################################################################
definitions:
HelloWorldResponse:
required:
Expand Down
26 changes: 16 additions & 10 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
'use strict';

var SwaggerExpress = require('swagger-express-mw');
var SwaggerUi = require('swagger-tools/middleware/swagger-ui');
var app = require('express')();
module.exports = app; // for testing

var config = {
appRoot: __dirname // required config
appRoot: __dirname // required config
};

SwaggerExpress.create(config, function(err, swaggerExpress) {
if (err) { throw err; }
SwaggerExpress.create(config, function (err, swaggerExpress) {
if (err) {
throw err;
}

// install middleware
swaggerExpress.register(app);
// add swagger-ui
app.use(SwaggerUi(swaggerExpress.runner.swagger));

// install middleware
swaggerExpress.register(app);

var port = process.env.PORT || 10010;
app.listen(port);
var port = process.env.PORT || 10010;
app.listen(port);

if (swaggerExpress.runner.swagger.paths['/hello']) {
console.log('try this:\ncurl http://127.0.0.1:' + port + '/hello?name=Scott');
}
if (swaggerExpress.runner.swagger.paths['/hello']) {
console.log('try this:\ncurl http://127.0.0.1:' + port + '/api/v1/hello?name=Scott');
}
});
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
"homepage": "https://github.com/at15/bform-api#readme",
"dependencies": {
"express": "^4.12.3",
"swagger-express-mw": "^0.1.0"
"swagger-express-mw": "^0.1.0",
"swagger-tools": "^0.9.11"
},
"devDependencies": {
"js-yaml": "^3.6.0",
Expand Down

0 comments on commit 95eaf8d

Please sign in to comment.