[Go][Server] minor enhancement to the template (#4417)

* minor enhancement to the go server template

* update samples
This commit is contained in:
William Cheng 2019-11-09 11:24:12 +08:00 committed by GitHub
parent 777e39007a
commit db729be7df
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 38 additions and 37 deletions

View File

@ -81,5 +81,5 @@ func (c *{{classname}}Controller) {{nickname}}(w http.ResponseWriter, r *http.Re
return return
} }
EncodeJSONResponse(result, nil, w) EncodeJSONResponse(result, nil, w)
}{{/operation}}{{/operations}} }{{/operation}}{{/operations}}

View File

@ -7,9 +7,9 @@ import (
"net/http" "net/http"
"os" "os"
"strconv" "strconv"
{{#featureCORS}} {{#featureCORS}}
"github.com/gorilla/handlers"
"github.com/gorilla/handlers"{{/featureCORS}} {{/featureCORS}}
"github.com/gorilla/mux" "github.com/gorilla/mux"
) )
@ -36,8 +36,10 @@ func NewRouter(routers ...Router) *mux.Router {
for _, route := range api.Routes() { for _, route := range api.Routes() {
var handler http.Handler var handler http.Handler
handler = route.HandlerFunc handler = route.HandlerFunc
handler = Logger(handler, route.Name){{#featureCORS}} handler = Logger(handler, route.Name)
handler = handlers.CORS()(handler){{/featureCORS}} {{#featureCORS}}
handler = handlers.CORS()(handler)
{{/featureCORS}}
router. router.
Methods(route.Method). Methods(route.Method).

View File

@ -95,7 +95,7 @@ func (c *PetApiController) AddPet(w http.ResponseWriter, r *http.Request) {
return return
} }
EncodeJSONResponse(result, nil, w) EncodeJSONResponse(result, nil, w)
} }
// DeletePet - Deletes a pet // DeletePet - Deletes a pet
@ -114,7 +114,7 @@ func (c *PetApiController) DeletePet(w http.ResponseWriter, r *http.Request) {
return return
} }
EncodeJSONResponse(result, nil, w) EncodeJSONResponse(result, nil, w)
} }
// FindPetsByStatus - Finds Pets by status // FindPetsByStatus - Finds Pets by status
@ -127,7 +127,7 @@ func (c *PetApiController) FindPetsByStatus(w http.ResponseWriter, r *http.Reque
return return
} }
EncodeJSONResponse(result, nil, w) EncodeJSONResponse(result, nil, w)
} }
// FindPetsByTags - Finds Pets by tags // FindPetsByTags - Finds Pets by tags
@ -140,7 +140,7 @@ func (c *PetApiController) FindPetsByTags(w http.ResponseWriter, r *http.Request
return return
} }
EncodeJSONResponse(result, nil, w) EncodeJSONResponse(result, nil, w)
} }
// GetPetById - Find pet by ID // GetPetById - Find pet by ID
@ -158,7 +158,7 @@ func (c *PetApiController) GetPetById(w http.ResponseWriter, r *http.Request) {
return return
} }
EncodeJSONResponse(result, nil, w) EncodeJSONResponse(result, nil, w)
} }
// UpdatePet - Update an existing pet // UpdatePet - Update an existing pet
@ -175,7 +175,7 @@ func (c *PetApiController) UpdatePet(w http.ResponseWriter, r *http.Request) {
return return
} }
EncodeJSONResponse(result, nil, w) EncodeJSONResponse(result, nil, w)
} }
// UpdatePetWithForm - Updates a pet in the store with form data // 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 return
} }
EncodeJSONResponse(result, nil, w) EncodeJSONResponse(result, nil, w)
} }
// UploadFile - uploads an image // UploadFile - uploads an image
@ -232,5 +232,5 @@ func (c *PetApiController) UploadFile(w http.ResponseWriter, r *http.Request) {
return return
} }
EncodeJSONResponse(result, nil, w) EncodeJSONResponse(result, nil, w)
} }

View File

@ -67,7 +67,7 @@ func (c *StoreApiController) DeleteOrder(w http.ResponseWriter, r *http.Request)
return return
} }
EncodeJSONResponse(result, nil, w) EncodeJSONResponse(result, nil, w)
} }
// GetInventory - Returns pet inventories by status // GetInventory - Returns pet inventories by status
@ -78,7 +78,7 @@ func (c *StoreApiController) GetInventory(w http.ResponseWriter, r *http.Request
return return
} }
EncodeJSONResponse(result, nil, w) EncodeJSONResponse(result, nil, w)
} }
// GetOrderById - Find purchase order by ID // GetOrderById - Find purchase order by ID
@ -96,7 +96,7 @@ func (c *StoreApiController) GetOrderById(w http.ResponseWriter, r *http.Request
return return
} }
EncodeJSONResponse(result, nil, w) EncodeJSONResponse(result, nil, w)
} }
// PlaceOrder - Place an order for a pet // PlaceOrder - Place an order for a pet
@ -113,5 +113,5 @@ func (c *StoreApiController) PlaceOrder(w http.ResponseWriter, r *http.Request)
return return
} }
EncodeJSONResponse(result, nil, w) EncodeJSONResponse(result, nil, w)
} }

View File

@ -95,7 +95,7 @@ func (c *UserApiController) CreateUser(w http.ResponseWriter, r *http.Request) {
return return
} }
EncodeJSONResponse(result, nil, w) EncodeJSONResponse(result, nil, w)
} }
// CreateUsersWithArrayInput - Creates list of users with given input array // CreateUsersWithArrayInput - Creates list of users with given input array
@ -112,7 +112,7 @@ func (c *UserApiController) CreateUsersWithArrayInput(w http.ResponseWriter, r *
return return
} }
EncodeJSONResponse(result, nil, w) EncodeJSONResponse(result, nil, w)
} }
// CreateUsersWithListInput - Creates list of users with given input array // CreateUsersWithListInput - Creates list of users with given input array
@ -129,7 +129,7 @@ func (c *UserApiController) CreateUsersWithListInput(w http.ResponseWriter, r *h
return return
} }
EncodeJSONResponse(result, nil, w) EncodeJSONResponse(result, nil, w)
} }
// DeleteUser - Delete user // DeleteUser - Delete user
@ -142,7 +142,7 @@ func (c *UserApiController) DeleteUser(w http.ResponseWriter, r *http.Request) {
return return
} }
EncodeJSONResponse(result, nil, w) EncodeJSONResponse(result, nil, w)
} }
// GetUserByName - Get user by user name // GetUserByName - Get user by user name
@ -155,7 +155,7 @@ func (c *UserApiController) GetUserByName(w http.ResponseWriter, r *http.Request
return return
} }
EncodeJSONResponse(result, nil, w) EncodeJSONResponse(result, nil, w)
} }
// LoginUser - Logs user into the system // LoginUser - Logs user into the system
@ -169,7 +169,7 @@ func (c *UserApiController) LoginUser(w http.ResponseWriter, r *http.Request) {
return return
} }
EncodeJSONResponse(result, nil, w) EncodeJSONResponse(result, nil, w)
} }
// LogoutUser - Logs out current logged in user session // LogoutUser - Logs out current logged in user session
@ -180,7 +180,7 @@ func (c *UserApiController) LogoutUser(w http.ResponseWriter, r *http.Request) {
return return
} }
EncodeJSONResponse(result, nil, w) EncodeJSONResponse(result, nil, w)
} }
// UpdateUser - Updated user // UpdateUser - Updated user
@ -199,5 +199,5 @@ func (c *UserApiController) UpdateUser(w http.ResponseWriter, r *http.Request) {
return return
} }
EncodeJSONResponse(result, nil, w) EncodeJSONResponse(result, nil, w)
} }

View File

@ -15,7 +15,6 @@ import (
"net/http" "net/http"
"os" "os"
"strconv" "strconv"
"github.com/gorilla/mux" "github.com/gorilla/mux"
) )