Last active
June 18, 2020 10:03
-
-
Save tango238/3e550416e8b9b8445d15e64ee321b9c1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
java -jar ../../swagger-codegen-cli.jar generate -i ../swagger.yml -l nodejs-server |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"scripts": { | |
... | |
"swagger:combine": "multi-file-swagger -o yaml swagger/index.yml > ./swagger.yml", | |
"swagger:validate": "./node_modules/swagger-cli/bin/swagger-cli.js validate ./swagger.yml", | |
"swagger:generate": "java -Dmodels -Dapis -DapiTests=false -DapiDocs=false -DmodelTests=false -DmodelDocs=false -jar ../swagger-codegen-cli.jar generate -i ./swagger.yml -l javascript -o ./ --additional-properties usePromises=true,useES6=true", | |
"swagger:generate-ts": "./openapitools/openapi-generator-cli generate -i ./swagger.yaml -g typescript-fetch -o ./ -Dmodels -Dapis --additional-properties=withSeparateModelsAndApi=true -DsupportingFiles=index.ts && rm src/index.ts .openapi-generator-ignore", | |
"swagger:mock": "cd mock; yarn install; ./generate_mock.sh", | |
"update-api": "npm-run-all swagger:combine swagger:validate swagger:generate", | |
... | |
}, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var fs = require('fs'), | |
path = require('path'), | |
http = require('http'); | |
var app = require('connect')(); | |
var cors = require('cors'); | |
var swaggerTools = require('swagger-tools'); | |
var jsyaml = require('js-yaml'); | |
var serverPort = 8080; | |
// swaggerRouter configuration | |
var options = { | |
swaggerUi: path.join(__dirname, '/swagger.json'), | |
controllers: path.join(__dirname, './controllers'), | |
ignoreMissingHandlers: true, | |
useStubs: process.env.NODE_ENV === 'development' // Conditionally turn on stubs (mock mode) | |
}; | |
// The Swagger document (require it, build it programmatically, fetch it from a URL, ...) | |
var spec = fs.readFileSync(path.join(__dirname,'api/swagger.yaml'), 'utf8'); | |
var swaggerDoc = jsyaml.safeLoad(spec); | |
// Initialize the Swagger middleware | |
swaggerTools.initializeMiddleware(swaggerDoc, function (middleware) { | |
app.use(cors()); | |
app.use((function(){ | |
let postData = ""; | |
return function(req, res, next) { | |
req.on("data", function(chunk) { | |
postData += chunk; | |
}); | |
req.on("end", function() { | |
console.log(req.method + " " + req.url + " " + JSON.stringify(postData)); | |
}); | |
return next(); | |
} | |
})()); | |
// Interpret Swagger resources and attach metadata to request - must be first in swagger-tools middleware chain | |
app.use(middleware.swaggerMetadata()); | |
// Validate Swagger requests | |
app.use(middleware.swaggerValidator()); | |
// Route validated requests to appropriate controller | |
app.use(middleware.swaggerRouter(options)); | |
// Serve the Swagger documents and Swagger UI | |
app.use(middleware.swaggerUi()); | |
// Start the server | |
http.createServer(app).listen(serverPort, function () { | |
console.log('options.ignoreMissingHandlers: %s', options.ignoreMissingHandlers); | |
console.log('Your environment is %s', process.env.NODE_ENV); | |
console.log('Your server is listening on port %d (http://localhost:%d)', serverPort, serverPort); | |
console.log('Swagger-ui is available on http://localhost:%d/docs', serverPort); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment