Skip to content
Permalink
Browse files

Add Go Server featureCORS option (#4400)

Add a Go server configuration option of "featureCORS" that defaults to false. When set to true the Go server routers will be generated with Cross-Origin Resource Sharing middleware through gorrilla mux.

https://www.gorillatoolkit.org/pkg/handlers#CORS
  • Loading branch information...
Jesse0Michael authored and wing328 committed Nov 8, 2019
1 parent 79d7e61 commit 5b4441892fc8c5088a22419b9bf5bdbae8b17a2c
@@ -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|
@@ -39,6 +39,7 @@
protected int serverPort = 8080;
protected String projectName = "openapi-server";
protected String sourceFolder = "go";
protected Boolean corsFeatureEnabled = false;


public GoServerCodegen() {
@@ -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.
@@ -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;

@@ -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;
}
}
@@ -7,7 +7,9 @@ import (
"net/http"
"os"
"strconv"
{{#featureCORS}}

"github.com/gorilla/handlers"{{/featureCORS}}
"github.com/gorilla/mux"
)

@@ -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}}

router.
Methods(route.Method).

0 comments on commit 5b44418

Please sign in to comment.
You can’t perform that action at this time.