Skip to content

Commit

Permalink
[Go][Server] minor enhancement to the template (OpenAPITools#4417)
Browse files Browse the repository at this point in the history
* minor enhancement to the go server template

* update samples
  • Loading branch information
wing328 authored Nov 9, 2019
1 parent 777e390 commit db729be
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ public GoServerCodegen() {

// set the output folder here
outputFolder = "generated-code/go";

cliOptions.add(new CliOption(CodegenConstants.SOURCE_FOLDER, CodegenConstants.SOURCE_FOLDER_DESC)
.defaultValue(sourceFolder));

CliOption optServerPort = new CliOption("serverPort", "The network port the generated server binds to");
optServerPort.setType("int");
optServerPort.defaultValue(Integer.toString(serverPort));
Expand Down Expand Up @@ -117,8 +117,8 @@ public GoServerCodegen() {
@Override
public void processOpts() {
super.processOpts();


/*
* Additional Properties. These values can be passed to the templates and
* are available in models, apis, and supporting files
Expand All @@ -129,19 +129,19 @@ public void processOpts() {
setPackageName("openapi");
additionalProperties.put(CodegenConstants.PACKAGE_NAME, packageName);
}

if (additionalProperties.containsKey(CodegenConstants.PACKAGE_VERSION)) {
this.setPackageVersion((String) additionalProperties.get(CodegenConstants.PACKAGE_VERSION));
} else {
additionalProperties.put(CodegenConstants.PACKAGE_VERSION, packageVersion);
}

if (additionalProperties.containsKey(CodegenConstants.SOURCE_FOLDER)) {
this.setSourceFolder((String) additionalProperties.get(CodegenConstants.SOURCE_FOLDER));
} else {
additionalProperties.put(CodegenConstants.SOURCE_FOLDER, sourceFolder);
}

if (additionalProperties.containsKey("serverPort") && additionalProperties.get("serverPort") instanceof Integer) {
this.setServerPort((int) additionalProperties.get("serverPort"));
} else {
Expand Down Expand Up @@ -263,7 +263,7 @@ public String apiFileFolder() {
public String modelFileFolder() {
return outputFolder + File.separator + apiPackage().replace('.', File.separatorChar);
}

public void setSourceFolder(String sourceFolder) {
this.sourceFolder = sourceFolder;
}
Expand All @@ -274,9 +274,9 @@ 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 @@ -81,5 +81,5 @@ func (c *{{classname}}Controller) {{nickname}}(w http.ResponseWriter, r *http.Re
return
}

EncodeJSONResponse(result, nil, w)
EncodeJSONResponse(result, nil, w)
}{{/operation}}{{/operations}}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (
"net/http"
"os"
"strconv"
{{#featureCORS}}

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

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

router.
Methods(route.Method).
Expand Down
16 changes: 8 additions & 8 deletions samples/server/petstore/go-api-server/go/api_pet.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func (c *PetApiController) AddPet(w http.ResponseWriter, r *http.Request) {
return
}

EncodeJSONResponse(result, nil, w)
EncodeJSONResponse(result, nil, w)
}

// DeletePet - Deletes a pet
Expand All @@ -114,7 +114,7 @@ func (c *PetApiController) DeletePet(w http.ResponseWriter, r *http.Request) {
return
}

EncodeJSONResponse(result, nil, w)
EncodeJSONResponse(result, nil, w)
}

// FindPetsByStatus - Finds Pets by status
Expand All @@ -127,7 +127,7 @@ func (c *PetApiController) FindPetsByStatus(w http.ResponseWriter, r *http.Reque
return
}

EncodeJSONResponse(result, nil, w)
EncodeJSONResponse(result, nil, w)
}

// FindPetsByTags - Finds Pets by tags
Expand All @@ -140,7 +140,7 @@ func (c *PetApiController) FindPetsByTags(w http.ResponseWriter, r *http.Request
return
}

EncodeJSONResponse(result, nil, w)
EncodeJSONResponse(result, nil, w)
}

// GetPetById - Find pet by ID
Expand All @@ -158,7 +158,7 @@ func (c *PetApiController) GetPetById(w http.ResponseWriter, r *http.Request) {
return
}

EncodeJSONResponse(result, nil, w)
EncodeJSONResponse(result, nil, w)
}

// UpdatePet - Update an existing pet
Expand All @@ -175,7 +175,7 @@ func (c *PetApiController) UpdatePet(w http.ResponseWriter, r *http.Request) {
return
}

EncodeJSONResponse(result, nil, w)
EncodeJSONResponse(result, nil, w)
}

// UpdatePetWithForm - Updates a pet in the store with form data
Expand All @@ -201,7 +201,7 @@ func (c *PetApiController) UpdatePetWithForm(w http.ResponseWriter, r *http.Requ
return
}

EncodeJSONResponse(result, nil, w)
EncodeJSONResponse(result, nil, w)
}

// UploadFile - uploads an image
Expand Down Expand Up @@ -232,5 +232,5 @@ func (c *PetApiController) UploadFile(w http.ResponseWriter, r *http.Request) {
return
}

EncodeJSONResponse(result, nil, w)
EncodeJSONResponse(result, nil, w)
}
8 changes: 4 additions & 4 deletions samples/server/petstore/go-api-server/go/api_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (c *StoreApiController) DeleteOrder(w http.ResponseWriter, r *http.Request)
return
}

EncodeJSONResponse(result, nil, w)
EncodeJSONResponse(result, nil, w)
}

// GetInventory - Returns pet inventories by status
Expand All @@ -78,7 +78,7 @@ func (c *StoreApiController) GetInventory(w http.ResponseWriter, r *http.Request
return
}

EncodeJSONResponse(result, nil, w)
EncodeJSONResponse(result, nil, w)
}

// GetOrderById - Find purchase order by ID
Expand All @@ -96,7 +96,7 @@ func (c *StoreApiController) GetOrderById(w http.ResponseWriter, r *http.Request
return
}

EncodeJSONResponse(result, nil, w)
EncodeJSONResponse(result, nil, w)
}

// PlaceOrder - Place an order for a pet
Expand All @@ -113,5 +113,5 @@ func (c *StoreApiController) PlaceOrder(w http.ResponseWriter, r *http.Request)
return
}

EncodeJSONResponse(result, nil, w)
EncodeJSONResponse(result, nil, w)
}
16 changes: 8 additions & 8 deletions samples/server/petstore/go-api-server/go/api_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func (c *UserApiController) CreateUser(w http.ResponseWriter, r *http.Request) {
return
}

EncodeJSONResponse(result, nil, w)
EncodeJSONResponse(result, nil, w)
}

// CreateUsersWithArrayInput - Creates list of users with given input array
Expand All @@ -112,7 +112,7 @@ func (c *UserApiController) CreateUsersWithArrayInput(w http.ResponseWriter, r *
return
}

EncodeJSONResponse(result, nil, w)
EncodeJSONResponse(result, nil, w)
}

// CreateUsersWithListInput - Creates list of users with given input array
Expand All @@ -129,7 +129,7 @@ func (c *UserApiController) CreateUsersWithListInput(w http.ResponseWriter, r *h
return
}

EncodeJSONResponse(result, nil, w)
EncodeJSONResponse(result, nil, w)
}

// DeleteUser - Delete user
Expand All @@ -142,7 +142,7 @@ func (c *UserApiController) DeleteUser(w http.ResponseWriter, r *http.Request) {
return
}

EncodeJSONResponse(result, nil, w)
EncodeJSONResponse(result, nil, w)
}

// GetUserByName - Get user by user name
Expand All @@ -155,7 +155,7 @@ func (c *UserApiController) GetUserByName(w http.ResponseWriter, r *http.Request
return
}

EncodeJSONResponse(result, nil, w)
EncodeJSONResponse(result, nil, w)
}

// LoginUser - Logs user into the system
Expand All @@ -169,7 +169,7 @@ func (c *UserApiController) LoginUser(w http.ResponseWriter, r *http.Request) {
return
}

EncodeJSONResponse(result, nil, w)
EncodeJSONResponse(result, nil, w)
}

// LogoutUser - Logs out current logged in user session
Expand All @@ -180,7 +180,7 @@ func (c *UserApiController) LogoutUser(w http.ResponseWriter, r *http.Request) {
return
}

EncodeJSONResponse(result, nil, w)
EncodeJSONResponse(result, nil, w)
}

// UpdateUser - Updated user
Expand All @@ -199,5 +199,5 @@ func (c *UserApiController) UpdateUser(w http.ResponseWriter, r *http.Request) {
return
}

EncodeJSONResponse(result, nil, w)
EncodeJSONResponse(result, nil, w)
}
1 change: 0 additions & 1 deletion samples/server/petstore/go-api-server/go/routers.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"net/http"
"os"
"strconv"

"github.com/gorilla/mux"
)

Expand Down

0 comments on commit db729be

Please sign in to comment.