Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[go-server] Add featureCORS option #4400

Merged
merged 1 commit into from
Nov 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/generators/go-server.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ sidebar_label: go-server
|hideGenerationTimestamp|Hides the generation timestamp when files are generated.| |true|
|sourceFolder|source folder for generated code| |go|
|serverPort|The network port the generated server binds to| |8080|
|featureCORS|Enable Cross-Origin Resource Sharing middleware| |false|
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public class GoServerCodegen extends AbstractGoCodegen {
protected int serverPort = 8080;
protected String projectName = "openapi-server";
protected String sourceFolder = "go";
protected Boolean corsFeatureEnabled = false;


public GoServerCodegen() {
Expand All @@ -55,6 +56,11 @@ public GoServerCodegen() {
optServerPort.defaultValue(Integer.toString(serverPort));
cliOptions.add(optServerPort);

CliOption optFeatureCORS = new CliOption("featureCORS", "Enable Cross-Origin Resource Sharing middleware");
optFeatureCORS.setType("bool");
optFeatureCORS.defaultValue(corsFeatureEnabled.toString());
cliOptions.add(optFeatureCORS);

/*
* Models. You can write model files using the modelTemplateFiles map.
* if you want to create one template for file, you can do so here.
Expand Down Expand Up @@ -142,6 +148,12 @@ public void processOpts() {
additionalProperties.put("serverPort", serverPort);
}

if (additionalProperties.containsKey("featureCORS")) {
this.setFeatureCORS(convertPropertyToBooleanAndWriteBack("featureCORS"));
} else {
additionalProperties.put("featureCORS", corsFeatureEnabled);
}

modelPackage = packageName;
apiPackage = packageName;

Expand Down Expand Up @@ -263,4 +275,8 @@ public void setPackageVersion(String packageVersion) {
public void setServerPort(int serverPort) {
this.serverPort = serverPort;
}

public void setFeatureCORS(Boolean featureCORS) {
this.corsFeatureEnabled = featureCORS;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import (
"net/http"
"os"
"strconv"
{{#featureCORS}}

"github.com/gorilla/handlers"{{/featureCORS}}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about removing line 11 and use the following instead?

{{#featureCORS}}
	"github.com/gorilla/handlers"
{{/featureCORS}}

(which will avoid blank lines)

"github.com/gorilla/mux"
)

Expand All @@ -34,7 +36,8 @@ func NewRouter(routers ...Router) *mux.Router {
for _, route := range api.Routes() {
var handler http.Handler
handler = route.HandlerFunc
handler = Logger(handler, route.Name)
handler = Logger(handler, route.Name){{#featureCORS}}
handler = handlers.CORS()(handler){{/featureCORS}}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same suggestion as above:

			{{#featureCORS}}
			handler = handlers.CORS()(handler)
			{{/featureCORS}}


router.
Methods(route.Method).
Expand Down