-
Notifications
You must be signed in to change notification settings - Fork 6k
[NodeJS] Introduce NodeJS codegen for Google Cloud Functions #4406
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| #!/bin/sh | ||
|
|
||
| SCRIPT="$0" | ||
|
|
||
| while [ -h "$SCRIPT" ] ; do | ||
| ls=`ls -ld "$SCRIPT"` | ||
| link=`expr "$ls" : '.*-> \(.*\)$'` | ||
| if expr "$link" : '/.*' > /dev/null; then | ||
| SCRIPT="$link" | ||
| else | ||
| SCRIPT=`dirname "$SCRIPT"`/"$link" | ||
| fi | ||
| done | ||
|
|
||
| if [ ! -d "${APP_DIR}" ]; then | ||
| APP_DIR=`dirname "$SCRIPT"`/.. | ||
| APP_DIR=`cd "${APP_DIR}"; pwd` | ||
| fi | ||
|
|
||
| executable="./modules/swagger-codegen-cli/target/swagger-codegen-cli.jar" | ||
|
|
||
| if [ ! -f "$executable" ] | ||
| then | ||
| mvn clean package | ||
| fi | ||
|
|
||
| # if you've executed sbt assembly previously it will use that instead. | ||
| export JAVA_OPTS="${JAVA_OPTS} -XX:MaxPermSize=256M -Xmx1024M -DloggerPath=conf/log4j.properties" | ||
| ags="$@ generate -i modules/swagger-codegen/src/test/resources/2_0/petstore.yaml -l nodejs-server --additional-properties=googleCloudFunctions=true -o samples/server/petstore/nodejs-google-cloud-functions" | ||
|
|
||
| java $JAVA_OPTS -Dservice -jar $executable $ags |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| 'use strict'; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jmuk we'll move this file to nodejs/google-cloud-functions/index.mustache (and update the code accordingly). so that all nodejs (server) related files are under the folder. Are you OK with the proposed change?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, I don't care the exact file location as long as the code works. But thank you for telling me!
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I found that this file is the same as |
||
|
|
||
| var swaggerTools = require('swagger-tools'); | ||
| var jsyaml = require('js-yaml'); | ||
| var fs = require('fs'); | ||
|
|
||
| // swaggerRouter configuration | ||
| var options = { | ||
| controllers: './controllers', | ||
| useStubs: false | ||
| }; | ||
|
|
||
| // The Swagger document (require it, build it programmatically, fetch it from a URL, ...) | ||
| var spec = fs.readFileSync('./api/swagger.yaml', 'utf8'); | ||
| var swaggerDoc = jsyaml.safeLoad(spec); | ||
|
|
||
| function toPromise(f, req, res) { | ||
| return new Promise(function(resolve, reject) { | ||
| f(req, res, function(err) { | ||
| if (err) { | ||
| reject(err); | ||
| } else { | ||
| resolve(); | ||
| } | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| exports.{{exportedName}} = function(req, res) { | ||
| swaggerTools.initializeMiddleware(swaggerDoc, function(middleware) { | ||
| var metadata = middleware.swaggerMetadata(); | ||
| var validator = middleware.swaggerValidator(); | ||
| var router = middleware.swaggerRouter(options); | ||
| req.url = swaggerDoc.basePath + req.url; | ||
| toPromise(metadata, req, res).then(function() { | ||
| return toPromise(validator, req, res); | ||
| }).then(function() { | ||
| return toPromise(router, req, res); | ||
| }).catch(function(err) { | ||
| console.error(err); | ||
| res.status(res.statusCode || 400).send(err); | ||
| }); | ||
| }); | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| 'use strict'; | ||
|
|
||
| var swaggerTools = require('swagger-tools'); | ||
| var jsyaml = require('js-yaml'); | ||
| var fs = require('fs'); | ||
|
|
||
| // swaggerRouter configuration | ||
| var options = { | ||
| controllers: './controllers', | ||
| useStubs: false | ||
| }; | ||
|
|
||
| // The Swagger document (require it, build it programmatically, fetch it from a URL, ...) | ||
| var spec = fs.readFileSync('./api/swagger.yaml', 'utf8'); | ||
| var swaggerDoc = jsyaml.safeLoad(spec); | ||
|
|
||
| function toPromise(f, req, res) { | ||
| return new Promise(function(resolve, reject) { | ||
| f(req, res, function(err) { | ||
| if (err) { | ||
| reject(err); | ||
| } else { | ||
| resolve(); | ||
| } | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| exports.{{exportedName}} = function(req, res) { | ||
| swaggerTools.initializeMiddleware(swaggerDoc, function(middleware) { | ||
| var metadata = middleware.swaggerMetadata(); | ||
| var validator = middleware.swaggerValidator(); | ||
| var router = middleware.swaggerRouter(options); | ||
| req.url = swaggerDoc.basePath + req.url; | ||
| toPromise(metadata, req, res).then(function() { | ||
| return toPromise(validator, req, res); | ||
| }).then(function() { | ||
| return toPromise(router, req, res); | ||
| }).catch(function(err) { | ||
| console.error(err); | ||
| res.status(res.statusCode || 400).send(err); | ||
| }); | ||
| }); | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| # Swagger generated server | ||
|
|
||
| ## Overview | ||
| This server was generated by the [swagger-codegen](https://github.com/swagger-api/swagger-codegen) project. By using the [OpenAPI-Spec](https://github.com/OAI/OpenAPI-Specification) from a remote server, you can easily generate a server stub. | ||
|
|
||
| ### Deploying the function | ||
| To deploy this module into Google Cloud Functions, you will have to use Google Cloud SDK commandline tool. | ||
|
|
||
| See [Google Cloud Functions quick start guide](https://cloud.google.com/functions/docs/quickstart) and [Deploying Cloud Functions](https://cloud.google.com/functions/docs/deploying/) for the details. | ||
|
|
||
| This project leverages the mega-awesome [swagger-tools](https://github.com/apigee-127/swagger-tools) middleware which does most all the work. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of using
noservice, what about using selective generation as mentioned in https://github.com/swagger-api/swagger-codegen#selective-generation? Would that meet your need?I prefer not to introduce another system property if the same output can already be achieved.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually this comes from the original NodeJSCodegen.java, I just moved the code location. I'm not adding new.
Also, the
noserviceis for a different purpose from the selective generation.The nodejs-server codegen generates two files per API. For the example of petstore, it generates files like
controllers/Pet.jsandcontrollers/PetService.js(see https://github.com/swagger-api/swagger-codegen/tree/master/samples/server/petstore/nodejs/controllers).When this
noserviceis specified, it omits the generation ofPetService.jsfile or any other -Service.js files. OnlyPet.jsor those files.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jmuk thanks for the clarification. I'll review and let you know if I've further question.