forked from loafle/openapi-generator-original
[Go][Server] minor enhancement to the template (#4417)
* minor enhancement to the go server template * update samples
This commit is contained in:
parent
777e39007a
commit
db729be7df
@ -47,10 +47,10 @@ public class GoServerCodegen extends AbstractGoCodegen {
|
||||
|
||||
// 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));
|
||||
@ -117,8 +117,8 @@ public class GoServerCodegen extends AbstractGoCodegen {
|
||||
@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
|
||||
@ -129,19 +129,19 @@ public class GoServerCodegen extends AbstractGoCodegen {
|
||||
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 {
|
||||
@ -263,7 +263,7 @@ public class GoServerCodegen extends AbstractGoCodegen {
|
||||
public String modelFileFolder() {
|
||||
return outputFolder + File.separator + apiPackage().replace('.', File.separatorChar);
|
||||
}
|
||||
|
||||
|
||||
public void setSourceFolder(String sourceFolder) {
|
||||
this.sourceFolder = sourceFolder;
|
||||
}
|
||||
@ -274,9 +274,9 @@ public class GoServerCodegen extends AbstractGoCodegen {
|
||||
|
||||
public void setServerPort(int serverPort) {
|
||||
this.serverPort = serverPort;
|
||||
}
|
||||
}
|
||||
|
||||
public void setFeatureCORS(Boolean featureCORS) {
|
||||
this.corsFeatureEnabled = featureCORS;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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}}
|
||||
|
@ -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"
|
||||
)
|
||||
|
||||
@ -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).
|
||||
|
@ -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
|
||||
@ -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
|
||||
@ -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
|
||||
@ -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
|
||||
@ -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
|
||||
@ -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
|
||||
@ -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
|
||||
@ -232,5 +232,5 @@ func (c *PetApiController) UploadFile(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
EncodeJSONResponse(result, nil, w)
|
||||
EncodeJSONResponse(result, nil, w)
|
||||
}
|
||||
|
@ -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
|
||||
@ -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
|
||||
@ -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
|
||||
@ -113,5 +113,5 @@ func (c *StoreApiController) PlaceOrder(w http.ResponseWriter, r *http.Request)
|
||||
return
|
||||
}
|
||||
|
||||
EncodeJSONResponse(result, nil, w)
|
||||
EncodeJSONResponse(result, nil, w)
|
||||
}
|
||||
|
@ -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
|
||||
@ -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
|
||||
@ -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
|
||||
@ -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
|
||||
@ -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
|
||||
@ -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
|
||||
@ -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
|
||||
@ -199,5 +199,5 @@ func (c *UserApiController) UpdateUser(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
EncodeJSONResponse(result, nil, w)
|
||||
EncodeJSONResponse(result, nil, w)
|
||||
}
|
||||
|
@ -15,7 +15,6 @@ import (
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
)
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user