forked from loafle/openapi-generator-original
[go-server] Change Routes type from []Route
to map[string]Route
(#15084)
* Change Routes to map[string]Route * Fix linting issues * Regenerate samples
This commit is contained in:
parent
b6d2e0d222
commit
792c49a0ce
@ -48,13 +48,16 @@ func New{{classname}}Controller(s {{classname}}Servicer, opts ...{{classname}}Op
|
||||
|
||||
// Routes returns all the api routes for the {{classname}}Controller
|
||||
func (c *{{classname}}Controller) Routes() Routes {
|
||||
return Routes{ {{#operations}}{{#operation}}
|
||||
{
|
||||
"{{operationId}}",
|
||||
return Routes{
|
||||
{{#operations}}
|
||||
{{#operation}}
|
||||
"{{operationId}}": Route{
|
||||
strings.ToUpper("{{httpMethod}}"),
|
||||
"{{{basePathWithoutHost}}}{{{path}}}",
|
||||
c.{{operationId}},
|
||||
},{{/operation}}{{/operations}}
|
||||
},
|
||||
{{/operation}}
|
||||
{{/operations}}
|
||||
}
|
||||
}{{#operations}}{{#operation}}
|
||||
|
||||
@ -234,5 +237,4 @@ func (c *{{classname}}Controller) {{nickname}}(w http.ResponseWriter, r *http.Re
|
||||
}
|
||||
// If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code,{{#addResponseHeaders}} result.Headers,{{/addResponseHeaders}} w)
|
||||
|
||||
}{{/operation}}{{/operations}}
|
||||
|
@ -1,7 +1,7 @@
|
||||
{{>partial_header}}
|
||||
package {{packageName}}
|
||||
|
||||
// ImplResponse response defines an error code with the associated body
|
||||
// ImplResponse defines an implementation response with error code and the associated body
|
||||
type ImplResponse struct {
|
||||
Code int
|
||||
{{#addResponseHeaders}}
|
||||
|
@ -29,14 +29,13 @@ import (
|
||||
|
||||
// A Route defines the parameters for an api endpoint
|
||||
type Route struct {
|
||||
Name string
|
||||
Method string
|
||||
Pattern string
|
||||
HandlerFunc http.HandlerFunc
|
||||
}
|
||||
|
||||
// Routes are a collection of defined api endpoints
|
||||
type Routes []Route
|
||||
// Routes is a map of defined api endpoints
|
||||
type Routes map[string]Route
|
||||
|
||||
// Router defines the required methods for retrieving api routes
|
||||
type Router interface {
|
||||
@ -60,12 +59,12 @@ func NewRouter(routers ...Router) {{#routers}}{{#mux}}*mux.Router{{/mux}}{{#chi}
|
||||
{{/chi}}
|
||||
{{/routers}}
|
||||
for _, api := range routers {
|
||||
for _, route := range api.Routes() {
|
||||
for name, route := range api.Routes() {
|
||||
var handler http.Handler
|
||||
handler = route.HandlerFunc
|
||||
{{#routers}}
|
||||
{{#mux}}
|
||||
handler = Logger(handler, route.Name)
|
||||
handler = Logger(handler, name)
|
||||
{{#featureCORS}}
|
||||
handler = handlers.CORS()(handler)
|
||||
{{/featureCORS}}
|
||||
@ -73,7 +72,7 @@ func NewRouter(routers ...Router) {{#routers}}{{#mux}}*mux.Router{{/mux}}{{#chi}
|
||||
router.
|
||||
Methods(route.Method).
|
||||
Path(route.Pattern).
|
||||
Name(route.Name).
|
||||
Name(name).
|
||||
Handler(handler)
|
||||
{{/mux}}
|
||||
{{#chi}}
|
||||
@ -90,13 +89,11 @@ func NewRouter(routers ...Router) {{#routers}}{{#mux}}*mux.Router{{/mux}}{{#chi}
|
||||
func EncodeJSONResponse(i interface{}, status *int,{{#addResponseHeaders}} headers map[string][]string,{{/addResponseHeaders}} w http.ResponseWriter) error {
|
||||
{{#addResponseHeaders}}
|
||||
wHeader := w.Header()
|
||||
if headers != nil {
|
||||
for key, values := range headers {
|
||||
for _, value := range values {
|
||||
wHeader.Add(key, value)
|
||||
}
|
||||
}
|
||||
}
|
||||
wHeader.Set("Content-Type", "application/json; charset=UTF-8")
|
||||
{{/addResponseHeaders}}
|
||||
{{^addResponseHeaders}}
|
||||
|
@ -50,50 +50,42 @@ func NewPetApiController(s PetApiServicer, opts ...PetApiOption) Router {
|
||||
// Routes returns all the api routes for the PetApiController
|
||||
func (c *PetApiController) Routes() Routes {
|
||||
return Routes{
|
||||
{
|
||||
"AddPet",
|
||||
"AddPet": Route{
|
||||
strings.ToUpper("Post"),
|
||||
"/v2/pet",
|
||||
c.AddPet,
|
||||
},
|
||||
{
|
||||
"DeletePet",
|
||||
"DeletePet": Route{
|
||||
strings.ToUpper("Delete"),
|
||||
"/v2/pet/{petId}",
|
||||
c.DeletePet,
|
||||
},
|
||||
{
|
||||
"FindPetsByStatus",
|
||||
"FindPetsByStatus": Route{
|
||||
strings.ToUpper("Get"),
|
||||
"/v2/pet/findByStatus",
|
||||
c.FindPetsByStatus,
|
||||
},
|
||||
{
|
||||
"FindPetsByTags",
|
||||
"FindPetsByTags": Route{
|
||||
strings.ToUpper("Get"),
|
||||
"/v2/pet/findByTags",
|
||||
c.FindPetsByTags,
|
||||
},
|
||||
{
|
||||
"GetPetById",
|
||||
"GetPetById": Route{
|
||||
strings.ToUpper("Get"),
|
||||
"/v2/pet/{petId}",
|
||||
c.GetPetById,
|
||||
},
|
||||
{
|
||||
"UpdatePet",
|
||||
"UpdatePet": Route{
|
||||
strings.ToUpper("Put"),
|
||||
"/v2/pet",
|
||||
c.UpdatePet,
|
||||
},
|
||||
{
|
||||
"UpdatePetWithForm",
|
||||
"UpdatePetWithForm": Route{
|
||||
strings.ToUpper("Post"),
|
||||
"/v2/pet/{petId}",
|
||||
c.UpdatePetWithForm,
|
||||
},
|
||||
{
|
||||
"UploadFile",
|
||||
"UploadFile": Route{
|
||||
strings.ToUpper("Post"),
|
||||
"/v2/pet/{petId}/uploadImage",
|
||||
c.UploadFile,
|
||||
@ -122,7 +114,6 @@ func (c *PetApiController) AddPet(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
// If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
|
||||
|
||||
}
|
||||
|
||||
// DeletePet - Deletes a pet
|
||||
@ -143,7 +134,6 @@ func (c *PetApiController) DeletePet(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
// If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
|
||||
|
||||
}
|
||||
|
||||
// FindPetsByStatus - Finds Pets by status
|
||||
@ -158,7 +148,6 @@ func (c *PetApiController) FindPetsByStatus(w http.ResponseWriter, r *http.Reque
|
||||
}
|
||||
// If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
|
||||
|
||||
}
|
||||
|
||||
// FindPetsByTags - Finds Pets by tags
|
||||
@ -174,7 +163,6 @@ func (c *PetApiController) FindPetsByTags(w http.ResponseWriter, r *http.Request
|
||||
}
|
||||
// If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
|
||||
|
||||
}
|
||||
|
||||
// GetPetById - Find pet by ID
|
||||
@ -194,7 +182,6 @@ func (c *PetApiController) GetPetById(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
// If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
|
||||
|
||||
}
|
||||
|
||||
// UpdatePet - Update an existing pet
|
||||
@ -218,7 +205,6 @@ func (c *PetApiController) UpdatePet(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
// If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
|
||||
|
||||
}
|
||||
|
||||
// UpdatePetWithForm - Updates a pet in the store with form data
|
||||
@ -244,7 +230,6 @@ func (c *PetApiController) UpdatePetWithForm(w http.ResponseWriter, r *http.Requ
|
||||
}
|
||||
// If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
|
||||
|
||||
}
|
||||
|
||||
// UploadFile - uploads an image
|
||||
@ -275,5 +260,4 @@ func (c *PetApiController) UploadFile(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
// If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
|
||||
|
||||
}
|
||||
|
@ -50,26 +50,22 @@ func NewStoreApiController(s StoreApiServicer, opts ...StoreApiOption) Router {
|
||||
// Routes returns all the api routes for the StoreApiController
|
||||
func (c *StoreApiController) Routes() Routes {
|
||||
return Routes{
|
||||
{
|
||||
"DeleteOrder",
|
||||
"DeleteOrder": Route{
|
||||
strings.ToUpper("Delete"),
|
||||
"/v2/store/order/{orderId}",
|
||||
c.DeleteOrder,
|
||||
},
|
||||
{
|
||||
"GetInventory",
|
||||
"GetInventory": Route{
|
||||
strings.ToUpper("Get"),
|
||||
"/v2/store/inventory",
|
||||
c.GetInventory,
|
||||
},
|
||||
{
|
||||
"GetOrderById",
|
||||
"GetOrderById": Route{
|
||||
strings.ToUpper("Get"),
|
||||
"/v2/store/order/{orderId}",
|
||||
c.GetOrderById,
|
||||
},
|
||||
{
|
||||
"PlaceOrder",
|
||||
"PlaceOrder": Route{
|
||||
strings.ToUpper("Post"),
|
||||
"/v2/store/order",
|
||||
c.PlaceOrder,
|
||||
@ -90,7 +86,6 @@ func (c *StoreApiController) DeleteOrder(w http.ResponseWriter, r *http.Request)
|
||||
}
|
||||
// If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
|
||||
|
||||
}
|
||||
|
||||
// GetInventory - Returns pet inventories by status
|
||||
@ -103,7 +98,6 @@ func (c *StoreApiController) GetInventory(w http.ResponseWriter, r *http.Request
|
||||
}
|
||||
// If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
|
||||
|
||||
}
|
||||
|
||||
// GetOrderById - Find purchase order by ID
|
||||
@ -123,7 +117,6 @@ func (c *StoreApiController) GetOrderById(w http.ResponseWriter, r *http.Request
|
||||
}
|
||||
// If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
|
||||
|
||||
}
|
||||
|
||||
// PlaceOrder - Place an order for a pet
|
||||
@ -147,5 +140,4 @@ func (c *StoreApiController) PlaceOrder(w http.ResponseWriter, r *http.Request)
|
||||
}
|
||||
// If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
|
||||
|
||||
}
|
||||
|
@ -50,50 +50,42 @@ func NewUserApiController(s UserApiServicer, opts ...UserApiOption) Router {
|
||||
// Routes returns all the api routes for the UserApiController
|
||||
func (c *UserApiController) Routes() Routes {
|
||||
return Routes{
|
||||
{
|
||||
"CreateUser",
|
||||
"CreateUser": Route{
|
||||
strings.ToUpper("Post"),
|
||||
"/v2/user",
|
||||
c.CreateUser,
|
||||
},
|
||||
{
|
||||
"CreateUsersWithArrayInput",
|
||||
"CreateUsersWithArrayInput": Route{
|
||||
strings.ToUpper("Post"),
|
||||
"/v2/user/createWithArray",
|
||||
c.CreateUsersWithArrayInput,
|
||||
},
|
||||
{
|
||||
"CreateUsersWithListInput",
|
||||
"CreateUsersWithListInput": Route{
|
||||
strings.ToUpper("Post"),
|
||||
"/v2/user/createWithList",
|
||||
c.CreateUsersWithListInput,
|
||||
},
|
||||
{
|
||||
"DeleteUser",
|
||||
"DeleteUser": Route{
|
||||
strings.ToUpper("Delete"),
|
||||
"/v2/user/{username}",
|
||||
c.DeleteUser,
|
||||
},
|
||||
{
|
||||
"GetUserByName",
|
||||
"GetUserByName": Route{
|
||||
strings.ToUpper("Get"),
|
||||
"/v2/user/{username}",
|
||||
c.GetUserByName,
|
||||
},
|
||||
{
|
||||
"LoginUser",
|
||||
"LoginUser": Route{
|
||||
strings.ToUpper("Get"),
|
||||
"/v2/user/login",
|
||||
c.LoginUser,
|
||||
},
|
||||
{
|
||||
"LogoutUser",
|
||||
"LogoutUser": Route{
|
||||
strings.ToUpper("Get"),
|
||||
"/v2/user/logout",
|
||||
c.LogoutUser,
|
||||
},
|
||||
{
|
||||
"UpdateUser",
|
||||
"UpdateUser": Route{
|
||||
strings.ToUpper("Put"),
|
||||
"/v2/user/{username}",
|
||||
c.UpdateUser,
|
||||
@ -122,7 +114,6 @@ func (c *UserApiController) CreateUser(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
// If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
|
||||
|
||||
}
|
||||
|
||||
// CreateUsersWithArrayInput - Creates list of users with given input array
|
||||
@ -148,7 +139,6 @@ func (c *UserApiController) CreateUsersWithArrayInput(w http.ResponseWriter, r *
|
||||
}
|
||||
// If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
|
||||
|
||||
}
|
||||
|
||||
// CreateUsersWithListInput - Creates list of users with given input array
|
||||
@ -174,7 +164,6 @@ func (c *UserApiController) CreateUsersWithListInput(w http.ResponseWriter, r *h
|
||||
}
|
||||
// If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
|
||||
|
||||
}
|
||||
|
||||
// DeleteUser - Delete user
|
||||
@ -190,7 +179,6 @@ func (c *UserApiController) DeleteUser(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
// If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
|
||||
|
||||
}
|
||||
|
||||
// GetUserByName - Get user by user name
|
||||
@ -206,7 +194,6 @@ func (c *UserApiController) GetUserByName(w http.ResponseWriter, r *http.Request
|
||||
}
|
||||
// If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
|
||||
|
||||
}
|
||||
|
||||
// LoginUser - Logs user into the system
|
||||
@ -222,7 +209,6 @@ func (c *UserApiController) LoginUser(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
// If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
|
||||
|
||||
}
|
||||
|
||||
// LogoutUser - Logs out current logged in user session
|
||||
@ -235,7 +221,6 @@ func (c *UserApiController) LogoutUser(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
// If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
|
||||
|
||||
}
|
||||
|
||||
// UpdateUser - Updated user
|
||||
@ -262,5 +247,4 @@ func (c *UserApiController) UpdateUser(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
// If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
|
||||
|
||||
}
|
||||
|
@ -9,7 +9,7 @@
|
||||
|
||||
package petstoreserver
|
||||
|
||||
// ImplResponse response defines an error code with the associated body
|
||||
// ImplResponse defines an implementation response with error code and the associated body
|
||||
type ImplResponse struct {
|
||||
Code int
|
||||
Headers map[string][]string
|
||||
|
@ -23,14 +23,13 @@ import (
|
||||
|
||||
// A Route defines the parameters for an api endpoint
|
||||
type Route struct {
|
||||
Name string
|
||||
Method string
|
||||
Pattern string
|
||||
HandlerFunc http.HandlerFunc
|
||||
}
|
||||
|
||||
// Routes are a collection of defined api endpoints
|
||||
type Routes []Route
|
||||
// Routes is a map of defined api endpoints
|
||||
type Routes map[string]Route
|
||||
|
||||
// Router defines the required methods for retrieving api routes
|
||||
type Router interface {
|
||||
@ -43,15 +42,15 @@ const errMsgRequiredMissing = "required parameter is missing"
|
||||
func NewRouter(routers ...Router) *mux.Router {
|
||||
router := mux.NewRouter().StrictSlash(true)
|
||||
for _, api := range routers {
|
||||
for _, route := range api.Routes() {
|
||||
for name, route := range api.Routes() {
|
||||
var handler http.Handler
|
||||
handler = route.HandlerFunc
|
||||
handler = Logger(handler, route.Name)
|
||||
handler = Logger(handler, name)
|
||||
|
||||
router.
|
||||
Methods(route.Method).
|
||||
Path(route.Pattern).
|
||||
Name(route.Name).
|
||||
Name(name).
|
||||
Handler(handler)
|
||||
}
|
||||
}
|
||||
@ -62,13 +61,11 @@ func NewRouter(routers ...Router) *mux.Router {
|
||||
// EncodeJSONResponse uses the json encoder to write an interface to the http response with an optional status code
|
||||
func EncodeJSONResponse(i interface{}, status *int, headers map[string][]string, w http.ResponseWriter) error {
|
||||
wHeader := w.Header()
|
||||
if headers != nil {
|
||||
for key, values := range headers {
|
||||
for _, value := range values {
|
||||
wHeader.Add(key, value)
|
||||
}
|
||||
}
|
||||
}
|
||||
wHeader.Set("Content-Type", "application/json; charset=UTF-8")
|
||||
if status != nil {
|
||||
w.WriteHeader(*status)
|
||||
|
@ -50,50 +50,42 @@ func NewPetApiController(s PetApiServicer, opts ...PetApiOption) Router {
|
||||
// Routes returns all the api routes for the PetApiController
|
||||
func (c *PetApiController) Routes() Routes {
|
||||
return Routes{
|
||||
{
|
||||
"AddPet",
|
||||
"AddPet": Route{
|
||||
strings.ToUpper("Post"),
|
||||
"/v2/pet",
|
||||
c.AddPet,
|
||||
},
|
||||
{
|
||||
"DeletePet",
|
||||
"DeletePet": Route{
|
||||
strings.ToUpper("Delete"),
|
||||
"/v2/pet/{petId}",
|
||||
c.DeletePet,
|
||||
},
|
||||
{
|
||||
"FindPetsByStatus",
|
||||
"FindPetsByStatus": Route{
|
||||
strings.ToUpper("Get"),
|
||||
"/v2/pet/findByStatus",
|
||||
c.FindPetsByStatus,
|
||||
},
|
||||
{
|
||||
"FindPetsByTags",
|
||||
"FindPetsByTags": Route{
|
||||
strings.ToUpper("Get"),
|
||||
"/v2/pet/findByTags",
|
||||
c.FindPetsByTags,
|
||||
},
|
||||
{
|
||||
"GetPetById",
|
||||
"GetPetById": Route{
|
||||
strings.ToUpper("Get"),
|
||||
"/v2/pet/{petId}",
|
||||
c.GetPetById,
|
||||
},
|
||||
{
|
||||
"UpdatePet",
|
||||
"UpdatePet": Route{
|
||||
strings.ToUpper("Put"),
|
||||
"/v2/pet",
|
||||
c.UpdatePet,
|
||||
},
|
||||
{
|
||||
"UpdatePetWithForm",
|
||||
"UpdatePetWithForm": Route{
|
||||
strings.ToUpper("Post"),
|
||||
"/v2/pet/{petId}",
|
||||
c.UpdatePetWithForm,
|
||||
},
|
||||
{
|
||||
"UploadFile",
|
||||
"UploadFile": Route{
|
||||
strings.ToUpper("Post"),
|
||||
"/v2/pet/{petId}/uploadImage",
|
||||
c.UploadFile,
|
||||
@ -122,7 +114,6 @@ func (c *PetApiController) AddPet(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
// If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
|
||||
|
||||
}
|
||||
|
||||
// DeletePet - Deletes a pet
|
||||
@ -142,7 +133,6 @@ func (c *PetApiController) DeletePet(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
// If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
|
||||
|
||||
}
|
||||
|
||||
// FindPetsByStatus - Finds Pets by status
|
||||
@ -157,7 +147,6 @@ func (c *PetApiController) FindPetsByStatus(w http.ResponseWriter, r *http.Reque
|
||||
}
|
||||
// If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
|
||||
|
||||
}
|
||||
|
||||
// FindPetsByTags - Finds Pets by tags
|
||||
@ -173,7 +162,6 @@ func (c *PetApiController) FindPetsByTags(w http.ResponseWriter, r *http.Request
|
||||
}
|
||||
// If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
|
||||
|
||||
}
|
||||
|
||||
// GetPetById - Find pet by ID
|
||||
@ -192,7 +180,6 @@ func (c *PetApiController) GetPetById(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
// If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
|
||||
|
||||
}
|
||||
|
||||
// UpdatePet - Update an existing pet
|
||||
@ -216,7 +203,6 @@ func (c *PetApiController) UpdatePet(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
// If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
|
||||
|
||||
}
|
||||
|
||||
// UpdatePetWithForm - Updates a pet in the store with form data
|
||||
@ -241,7 +227,6 @@ func (c *PetApiController) UpdatePetWithForm(w http.ResponseWriter, r *http.Requ
|
||||
}
|
||||
// If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
|
||||
|
||||
}
|
||||
|
||||
// UploadFile - uploads an image
|
||||
@ -271,5 +256,4 @@ func (c *PetApiController) UploadFile(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
// If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
|
||||
|
||||
}
|
||||
|
@ -50,26 +50,22 @@ func NewStoreApiController(s StoreApiServicer, opts ...StoreApiOption) Router {
|
||||
// Routes returns all the api routes for the StoreApiController
|
||||
func (c *StoreApiController) Routes() Routes {
|
||||
return Routes{
|
||||
{
|
||||
"DeleteOrder",
|
||||
"DeleteOrder": Route{
|
||||
strings.ToUpper("Delete"),
|
||||
"/v2/store/order/{orderId}",
|
||||
c.DeleteOrder,
|
||||
},
|
||||
{
|
||||
"GetInventory",
|
||||
"GetInventory": Route{
|
||||
strings.ToUpper("Get"),
|
||||
"/v2/store/inventory",
|
||||
c.GetInventory,
|
||||
},
|
||||
{
|
||||
"GetOrderById",
|
||||
"GetOrderById": Route{
|
||||
strings.ToUpper("Get"),
|
||||
"/v2/store/order/{orderId}",
|
||||
c.GetOrderById,
|
||||
},
|
||||
{
|
||||
"PlaceOrder",
|
||||
"PlaceOrder": Route{
|
||||
strings.ToUpper("Post"),
|
||||
"/v2/store/order",
|
||||
c.PlaceOrder,
|
||||
@ -89,7 +85,6 @@ func (c *StoreApiController) DeleteOrder(w http.ResponseWriter, r *http.Request)
|
||||
}
|
||||
// If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
|
||||
|
||||
}
|
||||
|
||||
// GetInventory - Returns pet inventories by status
|
||||
@ -102,7 +97,6 @@ func (c *StoreApiController) GetInventory(w http.ResponseWriter, r *http.Request
|
||||
}
|
||||
// If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
|
||||
|
||||
}
|
||||
|
||||
// GetOrderById - Find purchase order by ID
|
||||
@ -121,7 +115,6 @@ func (c *StoreApiController) GetOrderById(w http.ResponseWriter, r *http.Request
|
||||
}
|
||||
// If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
|
||||
|
||||
}
|
||||
|
||||
// PlaceOrder - Place an order for a pet
|
||||
@ -145,5 +138,4 @@ func (c *StoreApiController) PlaceOrder(w http.ResponseWriter, r *http.Request)
|
||||
}
|
||||
// If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
|
||||
|
||||
}
|
||||
|
@ -50,50 +50,42 @@ func NewUserApiController(s UserApiServicer, opts ...UserApiOption) Router {
|
||||
// Routes returns all the api routes for the UserApiController
|
||||
func (c *UserApiController) Routes() Routes {
|
||||
return Routes{
|
||||
{
|
||||
"CreateUser",
|
||||
"CreateUser": Route{
|
||||
strings.ToUpper("Post"),
|
||||
"/v2/user",
|
||||
c.CreateUser,
|
||||
},
|
||||
{
|
||||
"CreateUsersWithArrayInput",
|
||||
"CreateUsersWithArrayInput": Route{
|
||||
strings.ToUpper("Post"),
|
||||
"/v2/user/createWithArray",
|
||||
c.CreateUsersWithArrayInput,
|
||||
},
|
||||
{
|
||||
"CreateUsersWithListInput",
|
||||
"CreateUsersWithListInput": Route{
|
||||
strings.ToUpper("Post"),
|
||||
"/v2/user/createWithList",
|
||||
c.CreateUsersWithListInput,
|
||||
},
|
||||
{
|
||||
"DeleteUser",
|
||||
"DeleteUser": Route{
|
||||
strings.ToUpper("Delete"),
|
||||
"/v2/user/{username}",
|
||||
c.DeleteUser,
|
||||
},
|
||||
{
|
||||
"GetUserByName",
|
||||
"GetUserByName": Route{
|
||||
strings.ToUpper("Get"),
|
||||
"/v2/user/{username}",
|
||||
c.GetUserByName,
|
||||
},
|
||||
{
|
||||
"LoginUser",
|
||||
"LoginUser": Route{
|
||||
strings.ToUpper("Get"),
|
||||
"/v2/user/login",
|
||||
c.LoginUser,
|
||||
},
|
||||
{
|
||||
"LogoutUser",
|
||||
"LogoutUser": Route{
|
||||
strings.ToUpper("Get"),
|
||||
"/v2/user/logout",
|
||||
c.LogoutUser,
|
||||
},
|
||||
{
|
||||
"UpdateUser",
|
||||
"UpdateUser": Route{
|
||||
strings.ToUpper("Put"),
|
||||
"/v2/user/{username}",
|
||||
c.UpdateUser,
|
||||
@ -122,7 +114,6 @@ func (c *UserApiController) CreateUser(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
// If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
|
||||
|
||||
}
|
||||
|
||||
// CreateUsersWithArrayInput - Creates list of users with given input array
|
||||
@ -148,7 +139,6 @@ func (c *UserApiController) CreateUsersWithArrayInput(w http.ResponseWriter, r *
|
||||
}
|
||||
// If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
|
||||
|
||||
}
|
||||
|
||||
// CreateUsersWithListInput - Creates list of users with given input array
|
||||
@ -174,7 +164,6 @@ func (c *UserApiController) CreateUsersWithListInput(w http.ResponseWriter, r *h
|
||||
}
|
||||
// If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
|
||||
|
||||
}
|
||||
|
||||
// DeleteUser - Delete user
|
||||
@ -189,7 +178,6 @@ func (c *UserApiController) DeleteUser(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
// If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
|
||||
|
||||
}
|
||||
|
||||
// GetUserByName - Get user by user name
|
||||
@ -204,7 +192,6 @@ func (c *UserApiController) GetUserByName(w http.ResponseWriter, r *http.Request
|
||||
}
|
||||
// If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
|
||||
|
||||
}
|
||||
|
||||
// LoginUser - Logs user into the system
|
||||
@ -220,7 +207,6 @@ func (c *UserApiController) LoginUser(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
// If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
|
||||
|
||||
}
|
||||
|
||||
// LogoutUser - Logs out current logged in user session
|
||||
@ -233,7 +219,6 @@ func (c *UserApiController) LogoutUser(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
// If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
|
||||
|
||||
}
|
||||
|
||||
// UpdateUser - Updated user
|
||||
@ -259,5 +244,4 @@ func (c *UserApiController) UpdateUser(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
// If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
|
||||
|
||||
}
|
||||
|
@ -9,7 +9,7 @@
|
||||
|
||||
package petstoreserver
|
||||
|
||||
// ImplResponse response defines an error code with the associated body
|
||||
// ImplResponse defines an implementation response with error code and the associated body
|
||||
type ImplResponse struct {
|
||||
Code int
|
||||
Headers map[string][]string
|
||||
|
@ -24,14 +24,13 @@ import (
|
||||
|
||||
// A Route defines the parameters for an api endpoint
|
||||
type Route struct {
|
||||
Name string
|
||||
Method string
|
||||
Pattern string
|
||||
HandlerFunc http.HandlerFunc
|
||||
}
|
||||
|
||||
// Routes are a collection of defined api endpoints
|
||||
type Routes []Route
|
||||
// Routes is a map of defined api endpoints
|
||||
type Routes map[string]Route
|
||||
|
||||
// Router defines the required methods for retrieving api routes
|
||||
type Router interface {
|
||||
@ -45,7 +44,7 @@ func NewRouter(routers ...Router) chi.Router {
|
||||
router := chi.NewRouter()
|
||||
router.Use(middleware.Logger)
|
||||
for _, api := range routers {
|
||||
for _, route := range api.Routes() {
|
||||
for name, route := range api.Routes() {
|
||||
var handler http.Handler
|
||||
handler = route.HandlerFunc
|
||||
router.Method(route.Method, route.Pattern, handler)
|
||||
@ -58,13 +57,11 @@ func NewRouter(routers ...Router) chi.Router {
|
||||
// EncodeJSONResponse uses the json encoder to write an interface to the http response with an optional status code
|
||||
func EncodeJSONResponse(i interface{}, status *int, headers map[string][]string, w http.ResponseWriter) error {
|
||||
wHeader := w.Header()
|
||||
if headers != nil {
|
||||
for key, values := range headers {
|
||||
for _, value := range values {
|
||||
wHeader.Add(key, value)
|
||||
}
|
||||
}
|
||||
}
|
||||
wHeader.Set("Content-Type", "application/json; charset=UTF-8")
|
||||
if status != nil {
|
||||
w.WriteHeader(*status)
|
||||
|
@ -50,50 +50,42 @@ func NewPetApiController(s PetApiServicer, opts ...PetApiOption) Router {
|
||||
// Routes returns all the api routes for the PetApiController
|
||||
func (c *PetApiController) Routes() Routes {
|
||||
return Routes{
|
||||
{
|
||||
"AddPet",
|
||||
"AddPet": Route{
|
||||
strings.ToUpper("Post"),
|
||||
"/v2/pet",
|
||||
c.AddPet,
|
||||
},
|
||||
{
|
||||
"DeletePet",
|
||||
"DeletePet": Route{
|
||||
strings.ToUpper("Delete"),
|
||||
"/v2/pet/{petId}",
|
||||
c.DeletePet,
|
||||
},
|
||||
{
|
||||
"FindPetsByStatus",
|
||||
"FindPetsByStatus": Route{
|
||||
strings.ToUpper("Get"),
|
||||
"/v2/pet/findByStatus",
|
||||
c.FindPetsByStatus,
|
||||
},
|
||||
{
|
||||
"FindPetsByTags",
|
||||
"FindPetsByTags": Route{
|
||||
strings.ToUpper("Get"),
|
||||
"/v2/pet/findByTags",
|
||||
c.FindPetsByTags,
|
||||
},
|
||||
{
|
||||
"GetPetById",
|
||||
"GetPetById": Route{
|
||||
strings.ToUpper("Get"),
|
||||
"/v2/pet/{petId}",
|
||||
c.GetPetById,
|
||||
},
|
||||
{
|
||||
"UpdatePet",
|
||||
"UpdatePet": Route{
|
||||
strings.ToUpper("Put"),
|
||||
"/v2/pet",
|
||||
c.UpdatePet,
|
||||
},
|
||||
{
|
||||
"UpdatePetWithForm",
|
||||
"UpdatePetWithForm": Route{
|
||||
strings.ToUpper("Post"),
|
||||
"/v2/pet/{petId}",
|
||||
c.UpdatePetWithForm,
|
||||
},
|
||||
{
|
||||
"UploadFile",
|
||||
"UploadFile": Route{
|
||||
strings.ToUpper("Post"),
|
||||
"/v2/pet/{petId}/uploadImage",
|
||||
c.UploadFile,
|
||||
@ -122,7 +114,6 @@ func (c *PetApiController) AddPet(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
// If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
|
||||
|
||||
}
|
||||
|
||||
// DeletePet - Deletes a pet
|
||||
@ -142,7 +133,6 @@ func (c *PetApiController) DeletePet(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
// If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
|
||||
|
||||
}
|
||||
|
||||
// FindPetsByStatus - Finds Pets by status
|
||||
@ -157,7 +147,6 @@ func (c *PetApiController) FindPetsByStatus(w http.ResponseWriter, r *http.Reque
|
||||
}
|
||||
// If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
|
||||
|
||||
}
|
||||
|
||||
// FindPetsByTags - Finds Pets by tags
|
||||
@ -173,7 +162,6 @@ func (c *PetApiController) FindPetsByTags(w http.ResponseWriter, r *http.Request
|
||||
}
|
||||
// If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
|
||||
|
||||
}
|
||||
|
||||
// GetPetById - Find pet by ID
|
||||
@ -192,7 +180,6 @@ func (c *PetApiController) GetPetById(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
// If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
|
||||
|
||||
}
|
||||
|
||||
// UpdatePet - Update an existing pet
|
||||
@ -216,7 +203,6 @@ func (c *PetApiController) UpdatePet(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
// If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
|
||||
|
||||
}
|
||||
|
||||
// UpdatePetWithForm - Updates a pet in the store with form data
|
||||
@ -241,7 +227,6 @@ func (c *PetApiController) UpdatePetWithForm(w http.ResponseWriter, r *http.Requ
|
||||
}
|
||||
// If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
|
||||
|
||||
}
|
||||
|
||||
// UploadFile - uploads an image
|
||||
@ -271,5 +256,4 @@ func (c *PetApiController) UploadFile(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
// If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
|
||||
|
||||
}
|
||||
|
@ -50,26 +50,22 @@ func NewStoreApiController(s StoreApiServicer, opts ...StoreApiOption) Router {
|
||||
// Routes returns all the api routes for the StoreApiController
|
||||
func (c *StoreApiController) Routes() Routes {
|
||||
return Routes{
|
||||
{
|
||||
"DeleteOrder",
|
||||
"DeleteOrder": Route{
|
||||
strings.ToUpper("Delete"),
|
||||
"/v2/store/order/{orderId}",
|
||||
c.DeleteOrder,
|
||||
},
|
||||
{
|
||||
"GetInventory",
|
||||
"GetInventory": Route{
|
||||
strings.ToUpper("Get"),
|
||||
"/v2/store/inventory",
|
||||
c.GetInventory,
|
||||
},
|
||||
{
|
||||
"GetOrderById",
|
||||
"GetOrderById": Route{
|
||||
strings.ToUpper("Get"),
|
||||
"/v2/store/order/{orderId}",
|
||||
c.GetOrderById,
|
||||
},
|
||||
{
|
||||
"PlaceOrder",
|
||||
"PlaceOrder": Route{
|
||||
strings.ToUpper("Post"),
|
||||
"/v2/store/order",
|
||||
c.PlaceOrder,
|
||||
@ -89,7 +85,6 @@ func (c *StoreApiController) DeleteOrder(w http.ResponseWriter, r *http.Request)
|
||||
}
|
||||
// If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
|
||||
|
||||
}
|
||||
|
||||
// GetInventory - Returns pet inventories by status
|
||||
@ -102,7 +97,6 @@ func (c *StoreApiController) GetInventory(w http.ResponseWriter, r *http.Request
|
||||
}
|
||||
// If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
|
||||
|
||||
}
|
||||
|
||||
// GetOrderById - Find purchase order by ID
|
||||
@ -121,7 +115,6 @@ func (c *StoreApiController) GetOrderById(w http.ResponseWriter, r *http.Request
|
||||
}
|
||||
// If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
|
||||
|
||||
}
|
||||
|
||||
// PlaceOrder - Place an order for a pet
|
||||
@ -145,5 +138,4 @@ func (c *StoreApiController) PlaceOrder(w http.ResponseWriter, r *http.Request)
|
||||
}
|
||||
// If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
|
||||
|
||||
}
|
||||
|
@ -50,50 +50,42 @@ func NewUserApiController(s UserApiServicer, opts ...UserApiOption) Router {
|
||||
// Routes returns all the api routes for the UserApiController
|
||||
func (c *UserApiController) Routes() Routes {
|
||||
return Routes{
|
||||
{
|
||||
"CreateUser",
|
||||
"CreateUser": Route{
|
||||
strings.ToUpper("Post"),
|
||||
"/v2/user",
|
||||
c.CreateUser,
|
||||
},
|
||||
{
|
||||
"CreateUsersWithArrayInput",
|
||||
"CreateUsersWithArrayInput": Route{
|
||||
strings.ToUpper("Post"),
|
||||
"/v2/user/createWithArray",
|
||||
c.CreateUsersWithArrayInput,
|
||||
},
|
||||
{
|
||||
"CreateUsersWithListInput",
|
||||
"CreateUsersWithListInput": Route{
|
||||
strings.ToUpper("Post"),
|
||||
"/v2/user/createWithList",
|
||||
c.CreateUsersWithListInput,
|
||||
},
|
||||
{
|
||||
"DeleteUser",
|
||||
"DeleteUser": Route{
|
||||
strings.ToUpper("Delete"),
|
||||
"/v2/user/{username}",
|
||||
c.DeleteUser,
|
||||
},
|
||||
{
|
||||
"GetUserByName",
|
||||
"GetUserByName": Route{
|
||||
strings.ToUpper("Get"),
|
||||
"/v2/user/{username}",
|
||||
c.GetUserByName,
|
||||
},
|
||||
{
|
||||
"LoginUser",
|
||||
"LoginUser": Route{
|
||||
strings.ToUpper("Get"),
|
||||
"/v2/user/login",
|
||||
c.LoginUser,
|
||||
},
|
||||
{
|
||||
"LogoutUser",
|
||||
"LogoutUser": Route{
|
||||
strings.ToUpper("Get"),
|
||||
"/v2/user/logout",
|
||||
c.LogoutUser,
|
||||
},
|
||||
{
|
||||
"UpdateUser",
|
||||
"UpdateUser": Route{
|
||||
strings.ToUpper("Put"),
|
||||
"/v2/user/{username}",
|
||||
c.UpdateUser,
|
||||
@ -122,7 +114,6 @@ func (c *UserApiController) CreateUser(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
// If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
|
||||
|
||||
}
|
||||
|
||||
// CreateUsersWithArrayInput - Creates list of users with given input array
|
||||
@ -148,7 +139,6 @@ func (c *UserApiController) CreateUsersWithArrayInput(w http.ResponseWriter, r *
|
||||
}
|
||||
// If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
|
||||
|
||||
}
|
||||
|
||||
// CreateUsersWithListInput - Creates list of users with given input array
|
||||
@ -174,7 +164,6 @@ func (c *UserApiController) CreateUsersWithListInput(w http.ResponseWriter, r *h
|
||||
}
|
||||
// If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
|
||||
|
||||
}
|
||||
|
||||
// DeleteUser - Delete user
|
||||
@ -189,7 +178,6 @@ func (c *UserApiController) DeleteUser(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
// If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
|
||||
|
||||
}
|
||||
|
||||
// GetUserByName - Get user by user name
|
||||
@ -204,7 +192,6 @@ func (c *UserApiController) GetUserByName(w http.ResponseWriter, r *http.Request
|
||||
}
|
||||
// If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
|
||||
|
||||
}
|
||||
|
||||
// LoginUser - Logs user into the system
|
||||
@ -220,7 +207,6 @@ func (c *UserApiController) LoginUser(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
// If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
|
||||
|
||||
}
|
||||
|
||||
// LogoutUser - Logs out current logged in user session
|
||||
@ -233,7 +219,6 @@ func (c *UserApiController) LogoutUser(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
// If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
|
||||
|
||||
}
|
||||
|
||||
// UpdateUser - Updated user
|
||||
@ -259,5 +244,4 @@ func (c *UserApiController) UpdateUser(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
// If no error, encode the body and the result code
|
||||
EncodeJSONResponse(result.Body, &result.Code, result.Headers, w)
|
||||
|
||||
}
|
||||
|
@ -9,7 +9,7 @@
|
||||
|
||||
package petstoreserver
|
||||
|
||||
// ImplResponse response defines an error code with the associated body
|
||||
// ImplResponse defines an implementation response with error code and the associated body
|
||||
type ImplResponse struct {
|
||||
Code int
|
||||
Headers map[string][]string
|
||||
|
@ -24,14 +24,13 @@ import (
|
||||
|
||||
// A Route defines the parameters for an api endpoint
|
||||
type Route struct {
|
||||
Name string
|
||||
Method string
|
||||
Pattern string
|
||||
HandlerFunc http.HandlerFunc
|
||||
}
|
||||
|
||||
// Routes are a collection of defined api endpoints
|
||||
type Routes []Route
|
||||
// Routes is a map of defined api endpoints
|
||||
type Routes map[string]Route
|
||||
|
||||
// Router defines the required methods for retrieving api routes
|
||||
type Router interface {
|
||||
@ -45,7 +44,7 @@ func NewRouter(routers ...Router) chi.Router {
|
||||
router := chi.NewRouter()
|
||||
router.Use(middleware.Logger)
|
||||
for _, api := range routers {
|
||||
for _, route := range api.Routes() {
|
||||
for name, route := range api.Routes() {
|
||||
var handler http.Handler
|
||||
handler = route.HandlerFunc
|
||||
router.Method(route.Method, route.Pattern, handler)
|
||||
@ -58,13 +57,11 @@ func NewRouter(routers ...Router) chi.Router {
|
||||
// EncodeJSONResponse uses the json encoder to write an interface to the http response with an optional status code
|
||||
func EncodeJSONResponse(i interface{}, status *int, headers map[string][]string, w http.ResponseWriter) error {
|
||||
wHeader := w.Header()
|
||||
if headers != nil {
|
||||
for key, values := range headers {
|
||||
for _, value := range values {
|
||||
wHeader.Add(key, value)
|
||||
}
|
||||
}
|
||||
}
|
||||
wHeader.Set("Content-Type", "application/json; charset=UTF-8")
|
||||
if status != nil {
|
||||
w.WriteHeader(*status)
|
||||
|
Loading…
x
Reference in New Issue
Block a user