Skip to content

Commit

Permalink
[NodeJS] make serverPort configurable via CLI option (#7899)
Browse files Browse the repository at this point in the history
* Add "serverPort" option

* Use port number passed via CLI option if specified

* Replace hand-written param name with the constant

* Rename serverPort -> defaultServerPort

* Fix failed test

https://travis-ci.org/swagger-api/swagger-codegen/builds/357674590
  • Loading branch information
ackintosh authored and wing328 committed Mar 25, 2018
1 parent fc7e083 commit 6d88d07
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ public class NodeJSServerCodegen extends DefaultCodegen implements CodegenConfig
protected String implFolder = "service";
public static final String GOOGLE_CLOUD_FUNCTIONS = "googleCloudFunctions";
public static final String EXPORTED_NAME = "exportedName";
public static final String SERVER_PORT = "serverPort";

protected String apiVersion = "1.0.0";
protected int serverPort = 8080;
protected String projectName = "swagger-server";
protected String defaultServerPort = "8080";

protected boolean googleCloudFunctions;
protected String exportedName;
Expand Down Expand Up @@ -82,7 +83,6 @@ public NodeJSServerCodegen() {
* are available in models, apis, and supporting files
*/
additionalProperties.put("apiVersion", apiVersion);
additionalProperties.put("serverPort", serverPort);
additionalProperties.put("implFolder", implFolder);

supportingFiles.add(new SupportingFile("writer.mustache", ("utils").replace(".", File.separator), "writer.js"));
Expand All @@ -96,6 +96,8 @@ public NodeJSServerCodegen() {
"When the generated code will be deployed to Google Cloud Functions, this option can be "
+ "used to update the name of the exported function. By default, it refers to the "
+ "basePath. This does not affect normal standalone nodejs server code."));
cliOptions.add(new CliOption(SERVER_PORT,
"TCP port to listen on."));
}

@Override
Expand Down Expand Up @@ -318,7 +320,7 @@ public void processOpts() {
@Override
public void preprocessSwagger(Swagger swagger) {
String host = swagger.getHost();
String port = "8080";
String port = defaultServerPort;

if (!StringUtils.isEmpty(host)) {
String[] parts = host.split(":");
Expand All @@ -331,7 +333,10 @@ public void preprocessSwagger(Swagger swagger) {
LOGGER.warn("'host' in the specification is empty or undefined. Default to http://localhost.");
}

this.additionalProperties.put("serverPort", port);
if (additionalProperties.containsKey(SERVER_PORT)) {
port = additionalProperties.get(SERVER_PORT).toString();
}
this.additionalProperties.put(SERVER_PORT, port);

if (swagger.getInfo() != null) {
Info info = swagger.getInfo();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class NodeJSServerOptionsProvider implements OptionsProvider {
public static final String GOOGLE_CLOUD_FUNCTIONS = "false";
public static final String EXPORTED_NAME = "exported";
public static final String ALLOW_UNICODE_IDENTIFIERS_VALUE = "false";
public static final String SERVER_PORT = "8080";


@Override
Expand All @@ -26,6 +27,7 @@ public Map<String, String> createOptions() {
.put(CodegenConstants.ENSURE_UNIQUE_PARAMS, ENSURE_UNIQUE_PARAMS_VALUE)
.put(NodeJSServerCodegen.GOOGLE_CLOUD_FUNCTIONS, GOOGLE_CLOUD_FUNCTIONS)
.put(NodeJSServerCodegen.EXPORTED_NAME, EXPORTED_NAME)
.put(NodeJSServerCodegen.SERVER_PORT, SERVER_PORT)
.put(CodegenConstants.ALLOW_UNICODE_IDENTIFIERS, ALLOW_UNICODE_IDENTIFIERS_VALUE)
.build();
}
Expand Down

0 comments on commit 6d88d07

Please # to comment.