forked from loafle/openapi-generator-original
Use golang's provided method names (gin) (#2983)
* Use golang's provided method names (gin) This commit modifies the gin template for the router to leverage the http constants for method types as defined by RFC 7231 section 4.3. These are documented on: https://golang.org/pkg/net/http/#pkg-constants This removes the need for the `strings` dependency and does not require any new dependencies, as `net/http` is already imported. * Remove strings dependency which is no longer used * Update samples
This commit is contained in:
parent
e3bc228dd5
commit
14118807ba
@ -3,7 +3,6 @@ package {{packageName}}
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
@ -28,13 +27,13 @@ func NewRouter() *gin.Engine {
|
|||||||
router := gin.Default()
|
router := gin.Default()
|
||||||
for _, route := range routes {
|
for _, route := range routes {
|
||||||
switch route.Method {
|
switch route.Method {
|
||||||
case "GET":
|
case http.MethodGet:
|
||||||
router.GET(route.Pattern, route.HandlerFunc)
|
router.GET(route.Pattern, route.HandlerFunc)
|
||||||
case "POST":
|
case http.MethodPost:
|
||||||
router.POST(route.Pattern, route.HandlerFunc)
|
router.POST(route.Pattern, route.HandlerFunc)
|
||||||
case "PUT":
|
case http.MethodPut:
|
||||||
router.PUT(route.Pattern, route.HandlerFunc)
|
router.PUT(route.Pattern, route.HandlerFunc)
|
||||||
case "DELETE":
|
case http.MethodDelete:
|
||||||
router.DELETE(route.Pattern, route.HandlerFunc)
|
router.DELETE(route.Pattern, route.HandlerFunc)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -50,14 +49,14 @@ func Index(c *gin.Context) {
|
|||||||
var routes = Routes{
|
var routes = Routes{
|
||||||
{
|
{
|
||||||
"Index",
|
"Index",
|
||||||
"GET",
|
http.MethodGet,
|
||||||
"{{{basePathWithoutHost}}}/",
|
"{{{basePathWithoutHost}}}/",
|
||||||
Index,
|
Index,
|
||||||
},{{#apiInfo}}{{#apis}}{{#operations}}{{#operation}}
|
},{{#apiInfo}}{{#apis}}{{#operations}}{{#operation}}
|
||||||
|
|
||||||
{
|
{
|
||||||
"{{operationId}}",
|
"{{operationId}}",
|
||||||
strings.ToUpper("{{httpMethod}}"),
|
http.Method{{httpMethod}},
|
||||||
"{{{basePathWithoutHost}}}{{{path}}}",
|
"{{{basePathWithoutHost}}}{{{path}}}",
|
||||||
{{operationId}},
|
{{operationId}},
|
||||||
},{{/operation}}{{/operations}}{{/apis}}{{/apiInfo}}
|
},{{/operation}}{{/operations}}{{/apis}}{{/apiInfo}}
|
||||||
|
@ -11,7 +11,6 @@ package petstoreserver
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
@ -36,13 +35,13 @@ func NewRouter() *gin.Engine {
|
|||||||
router := gin.Default()
|
router := gin.Default()
|
||||||
for _, route := range routes {
|
for _, route := range routes {
|
||||||
switch route.Method {
|
switch route.Method {
|
||||||
case "GET":
|
case http.MethodGet:
|
||||||
router.GET(route.Pattern, route.HandlerFunc)
|
router.GET(route.Pattern, route.HandlerFunc)
|
||||||
case "POST":
|
case http.MethodPost:
|
||||||
router.POST(route.Pattern, route.HandlerFunc)
|
router.POST(route.Pattern, route.HandlerFunc)
|
||||||
case "PUT":
|
case http.MethodPut:
|
||||||
router.PUT(route.Pattern, route.HandlerFunc)
|
router.PUT(route.Pattern, route.HandlerFunc)
|
||||||
case "DELETE":
|
case http.MethodDelete:
|
||||||
router.DELETE(route.Pattern, route.HandlerFunc)
|
router.DELETE(route.Pattern, route.HandlerFunc)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -58,147 +57,147 @@ func Index(c *gin.Context) {
|
|||||||
var routes = Routes{
|
var routes = Routes{
|
||||||
{
|
{
|
||||||
"Index",
|
"Index",
|
||||||
"GET",
|
http.MethodGet,
|
||||||
"/v2/",
|
"/v2/",
|
||||||
Index,
|
Index,
|
||||||
},
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
"AddPet",
|
"AddPet",
|
||||||
strings.ToUpper("Post"),
|
http.MethodPost,
|
||||||
"/v2/pet",
|
"/v2/pet",
|
||||||
AddPet,
|
AddPet,
|
||||||
},
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
"DeletePet",
|
"DeletePet",
|
||||||
strings.ToUpper("Delete"),
|
http.MethodDelete,
|
||||||
"/v2/pet/:petId",
|
"/v2/pet/:petId",
|
||||||
DeletePet,
|
DeletePet,
|
||||||
},
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
"FindPetsByStatus",
|
"FindPetsByStatus",
|
||||||
strings.ToUpper("Get"),
|
http.MethodGet,
|
||||||
"/v2/pet/findByStatus",
|
"/v2/pet/findByStatus",
|
||||||
FindPetsByStatus,
|
FindPetsByStatus,
|
||||||
},
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
"FindPetsByTags",
|
"FindPetsByTags",
|
||||||
strings.ToUpper("Get"),
|
http.MethodGet,
|
||||||
"/v2/pet/findByTags",
|
"/v2/pet/findByTags",
|
||||||
FindPetsByTags,
|
FindPetsByTags,
|
||||||
},
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
"GetPetById",
|
"GetPetById",
|
||||||
strings.ToUpper("Get"),
|
http.MethodGet,
|
||||||
"/v2/pet/:petId",
|
"/v2/pet/:petId",
|
||||||
GetPetById,
|
GetPetById,
|
||||||
},
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
"UpdatePet",
|
"UpdatePet",
|
||||||
strings.ToUpper("Put"),
|
http.MethodPut,
|
||||||
"/v2/pet",
|
"/v2/pet",
|
||||||
UpdatePet,
|
UpdatePet,
|
||||||
},
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
"UpdatePetWithForm",
|
"UpdatePetWithForm",
|
||||||
strings.ToUpper("Post"),
|
http.MethodPost,
|
||||||
"/v2/pet/:petId",
|
"/v2/pet/:petId",
|
||||||
UpdatePetWithForm,
|
UpdatePetWithForm,
|
||||||
},
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
"UploadFile",
|
"UploadFile",
|
||||||
strings.ToUpper("Post"),
|
http.MethodPost,
|
||||||
"/v2/pet/:petId/uploadImage",
|
"/v2/pet/:petId/uploadImage",
|
||||||
UploadFile,
|
UploadFile,
|
||||||
},
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
"DeleteOrder",
|
"DeleteOrder",
|
||||||
strings.ToUpper("Delete"),
|
http.MethodDelete,
|
||||||
"/v2/store/order/:orderId",
|
"/v2/store/order/:orderId",
|
||||||
DeleteOrder,
|
DeleteOrder,
|
||||||
},
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
"GetInventory",
|
"GetInventory",
|
||||||
strings.ToUpper("Get"),
|
http.MethodGet,
|
||||||
"/v2/store/inventory",
|
"/v2/store/inventory",
|
||||||
GetInventory,
|
GetInventory,
|
||||||
},
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
"GetOrderById",
|
"GetOrderById",
|
||||||
strings.ToUpper("Get"),
|
http.MethodGet,
|
||||||
"/v2/store/order/:orderId",
|
"/v2/store/order/:orderId",
|
||||||
GetOrderById,
|
GetOrderById,
|
||||||
},
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
"PlaceOrder",
|
"PlaceOrder",
|
||||||
strings.ToUpper("Post"),
|
http.MethodPost,
|
||||||
"/v2/store/order",
|
"/v2/store/order",
|
||||||
PlaceOrder,
|
PlaceOrder,
|
||||||
},
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
"CreateUser",
|
"CreateUser",
|
||||||
strings.ToUpper("Post"),
|
http.MethodPost,
|
||||||
"/v2/user",
|
"/v2/user",
|
||||||
CreateUser,
|
CreateUser,
|
||||||
},
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
"CreateUsersWithArrayInput",
|
"CreateUsersWithArrayInput",
|
||||||
strings.ToUpper("Post"),
|
http.MethodPost,
|
||||||
"/v2/user/createWithArray",
|
"/v2/user/createWithArray",
|
||||||
CreateUsersWithArrayInput,
|
CreateUsersWithArrayInput,
|
||||||
},
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
"CreateUsersWithListInput",
|
"CreateUsersWithListInput",
|
||||||
strings.ToUpper("Post"),
|
http.MethodPost,
|
||||||
"/v2/user/createWithList",
|
"/v2/user/createWithList",
|
||||||
CreateUsersWithListInput,
|
CreateUsersWithListInput,
|
||||||
},
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
"DeleteUser",
|
"DeleteUser",
|
||||||
strings.ToUpper("Delete"),
|
http.MethodDelete,
|
||||||
"/v2/user/:username",
|
"/v2/user/:username",
|
||||||
DeleteUser,
|
DeleteUser,
|
||||||
},
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
"GetUserByName",
|
"GetUserByName",
|
||||||
strings.ToUpper("Get"),
|
http.MethodGet,
|
||||||
"/v2/user/:username",
|
"/v2/user/:username",
|
||||||
GetUserByName,
|
GetUserByName,
|
||||||
},
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
"LoginUser",
|
"LoginUser",
|
||||||
strings.ToUpper("Get"),
|
http.MethodGet,
|
||||||
"/v2/user/login",
|
"/v2/user/login",
|
||||||
LoginUser,
|
LoginUser,
|
||||||
},
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
"LogoutUser",
|
"LogoutUser",
|
||||||
strings.ToUpper("Get"),
|
http.MethodGet,
|
||||||
"/v2/user/logout",
|
"/v2/user/logout",
|
||||||
LogoutUser,
|
LogoutUser,
|
||||||
},
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
"UpdateUser",
|
"UpdateUser",
|
||||||
strings.ToUpper("Put"),
|
http.MethodPut,
|
||||||
"/v2/user/:username",
|
"/v2/user/:username",
|
||||||
UpdateUser,
|
UpdateUser,
|
||||||
},
|
},
|
||||||
|
Loading…
x
Reference in New Issue
Block a user