forked from loafle/openapi-generator-original
Update go-gin-server templates to return a configurable library (#10479)
* Update go-gin-server templates to return a configurable library Before this change, the user would be required to make changes in the generated code. This makes it really hard to update the API and the documentation at the same time. With this change, the generated code can be imported as is and used. The user just needs to set the handler functions for each endpoint. * Use spaces instead of tabs for api files * Use space instead of tab for ApiHandleFunctions in go-gin-server * Update samples for go-gin-server
This commit is contained in:
parent
141b3d4e0b
commit
a55453b5f7
@ -3,15 +3,16 @@ package {{packageName}}
|
|||||||
|
|
||||||
{{#operations}}
|
{{#operations}}
|
||||||
import (
|
import (
|
||||||
"net/http"
|
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
){{#operation}}
|
)
|
||||||
|
|
||||||
// {{nickname}} - {{{summary}}}
|
type {{classname}} struct {
|
||||||
|
{{#operation}}
|
||||||
|
// {{httpMethod}} {{{basePathWithoutHost}}}{{{path}}}
|
||||||
|
// {{{summary}}}
|
||||||
{{#isDeprecated}}
|
{{#isDeprecated}}
|
||||||
// Deprecated
|
// Deprecated
|
||||||
{{/isDeprecated}}
|
{{/isDeprecated}}
|
||||||
func {{nickname}}(c *gin.Context) {
|
{{nickname}} gin.HandlerFunc
|
||||||
c.JSON(http.StatusOK, gin.H{})
|
{{/operation}}
|
||||||
}{{/operation}}{{/operations}}
|
}{{/operations}}
|
||||||
|
@ -11,9 +11,11 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
routes := sw.ApiHandleFunctions{}
|
||||||
|
|
||||||
log.Printf("Server started")
|
log.Printf("Server started")
|
||||||
|
|
||||||
router := sw.NewRouter()
|
router := sw.NewRouter(routes)
|
||||||
|
|
||||||
log.Fatal(router.Run(":{{serverPort}}"))
|
log.Fatal(router.Run(":{{serverPort}}"))
|
||||||
}
|
}
|
||||||
|
@ -19,13 +19,13 @@ type Route struct {
|
|||||||
HandlerFunc gin.HandlerFunc
|
HandlerFunc gin.HandlerFunc
|
||||||
}
|
}
|
||||||
|
|
||||||
// Routes is the list of the generated Route.
|
|
||||||
type Routes []Route
|
|
||||||
|
|
||||||
// NewRouter returns a new router.
|
// NewRouter returns a new router.
|
||||||
func NewRouter() *gin.Engine {
|
func NewRouter(handleFunctions ApiHandleFunctions) *gin.Engine {
|
||||||
router := gin.Default()
|
router := gin.Default()
|
||||||
for _, route := range routes {
|
for _, route := range getRoutes(handleFunctions) {
|
||||||
|
if route.HandlerFunc == nil {
|
||||||
|
route.HandlerFunc = DefaultHandleFunc
|
||||||
|
}
|
||||||
switch route.Method {
|
switch route.Method {
|
||||||
case http.MethodGet:
|
case http.MethodGet:
|
||||||
router.GET(route.Pattern, route.HandlerFunc)
|
router.GET(route.Pattern, route.HandlerFunc)
|
||||||
@ -43,23 +43,25 @@ func NewRouter() *gin.Engine {
|
|||||||
return router
|
return router
|
||||||
}
|
}
|
||||||
|
|
||||||
// Index is the index handler.
|
// Default handler for not yet implemented routes
|
||||||
func Index(c *gin.Context) {
|
func DefaultHandleFunc(c *gin.Context) {
|
||||||
c.String(http.StatusOK, "Hello World!")
|
c.String(http.StatusNotImplemented, "501 not implemented")
|
||||||
}
|
}
|
||||||
|
|
||||||
var routes = Routes{
|
type ApiHandleFunctions struct {
|
||||||
{
|
{{#apiInfo}}{{#apis}}{{#operations}}
|
||||||
"Index",
|
// Routes for the {{classname}} part of the API
|
||||||
http.MethodGet,
|
{{classname}} {{classname}}{{/operations}}{{/apis}}{{/apiInfo}}
|
||||||
"{{{basePathWithoutHost}}}/",
|
}
|
||||||
Index,
|
|
||||||
},{{#apiInfo}}{{#apis}}{{#operations}}{{#operation}}
|
|
||||||
|
|
||||||
|
func getRoutes(handleFunctions ApiHandleFunctions) []Route {
|
||||||
|
return []Route{
|
||||||
|
{{#apiInfo}}{{#apis}}{{#operations}}{{#operation}}
|
||||||
{
|
{
|
||||||
"{{operationId}}",
|
"{{operationId}}",
|
||||||
http.Method{{httpMethod}},
|
http.Method{{httpMethod}},
|
||||||
"{{{basePathWithoutHost}}}{{{path}}}",
|
"{{{basePathWithoutHost}}}{{{path}}}",
|
||||||
{{operationId}},
|
handleFunctions.{{classname}}.{{operationId}},
|
||||||
},{{/operation}}{{/operations}}{{/apis}}{{/apiInfo}}
|
},{{/operation}}{{/operations}}{{/apis}}{{/apiInfo}}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
@ -10,48 +10,33 @@
|
|||||||
package petstoreserver
|
package petstoreserver
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
// AddPet - Add a new pet to the store
|
type PetApi struct {
|
||||||
func AddPet(c *gin.Context) {
|
// Post /v2/pet
|
||||||
c.JSON(http.StatusOK, gin.H{})
|
// Add a new pet to the store
|
||||||
}
|
AddPet gin.HandlerFunc
|
||||||
|
// Delete /v2/pet/:petId
|
||||||
// DeletePet - Deletes a pet
|
// Deletes a pet
|
||||||
func DeletePet(c *gin.Context) {
|
DeletePet gin.HandlerFunc
|
||||||
c.JSON(http.StatusOK, gin.H{})
|
// Get /v2/pet/findByStatus
|
||||||
}
|
// Finds Pets by status
|
||||||
|
FindPetsByStatus gin.HandlerFunc
|
||||||
// FindPetsByStatus - Finds Pets by status
|
// Get /v2/pet/findByTags
|
||||||
func FindPetsByStatus(c *gin.Context) {
|
// Finds Pets by tags
|
||||||
c.JSON(http.StatusOK, gin.H{})
|
|
||||||
}
|
|
||||||
|
|
||||||
// FindPetsByTags - Finds Pets by tags
|
|
||||||
// Deprecated
|
// Deprecated
|
||||||
func FindPetsByTags(c *gin.Context) {
|
FindPetsByTags gin.HandlerFunc
|
||||||
c.JSON(http.StatusOK, gin.H{})
|
// Get /v2/pet/:petId
|
||||||
}
|
// Find pet by ID
|
||||||
|
GetPetById gin.HandlerFunc
|
||||||
// GetPetById - Find pet by ID
|
// Put /v2/pet
|
||||||
func GetPetById(c *gin.Context) {
|
// Update an existing pet
|
||||||
c.JSON(http.StatusOK, gin.H{})
|
UpdatePet gin.HandlerFunc
|
||||||
}
|
// Post /v2/pet/:petId
|
||||||
|
// Updates a pet in the store with form data
|
||||||
// UpdatePet - Update an existing pet
|
UpdatePetWithForm gin.HandlerFunc
|
||||||
func UpdatePet(c *gin.Context) {
|
// Post /v2/pet/:petId/uploadImage
|
||||||
c.JSON(http.StatusOK, gin.H{})
|
// uploads an image
|
||||||
}
|
UploadFile gin.HandlerFunc
|
||||||
|
|
||||||
// UpdatePetWithForm - Updates a pet in the store with form data
|
|
||||||
func UpdatePetWithForm(c *gin.Context) {
|
|
||||||
c.JSON(http.StatusOK, gin.H{})
|
|
||||||
}
|
|
||||||
|
|
||||||
// UploadFile - uploads an image
|
|
||||||
func UploadFile(c *gin.Context) {
|
|
||||||
c.JSON(http.StatusOK, gin.H{})
|
|
||||||
}
|
}
|
||||||
|
@ -10,27 +10,20 @@
|
|||||||
package petstoreserver
|
package petstoreserver
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
// DeleteOrder - Delete purchase order by ID
|
type StoreApi struct {
|
||||||
func DeleteOrder(c *gin.Context) {
|
// Delete /v2/store/order/:orderId
|
||||||
c.JSON(http.StatusOK, gin.H{})
|
// Delete purchase order by ID
|
||||||
}
|
DeleteOrder gin.HandlerFunc
|
||||||
|
// Get /v2/store/inventory
|
||||||
// GetInventory - Returns pet inventories by status
|
// Returns pet inventories by status
|
||||||
func GetInventory(c *gin.Context) {
|
GetInventory gin.HandlerFunc
|
||||||
c.JSON(http.StatusOK, gin.H{})
|
// Get /v2/store/order/:orderId
|
||||||
}
|
// Find purchase order by ID
|
||||||
|
GetOrderById gin.HandlerFunc
|
||||||
// GetOrderById - Find purchase order by ID
|
// Post /v2/store/order
|
||||||
func GetOrderById(c *gin.Context) {
|
// Place an order for a pet
|
||||||
c.JSON(http.StatusOK, gin.H{})
|
PlaceOrder gin.HandlerFunc
|
||||||
}
|
|
||||||
|
|
||||||
// PlaceOrder - Place an order for a pet
|
|
||||||
func PlaceOrder(c *gin.Context) {
|
|
||||||
c.JSON(http.StatusOK, gin.H{})
|
|
||||||
}
|
}
|
||||||
|
@ -10,47 +10,32 @@
|
|||||||
package petstoreserver
|
package petstoreserver
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
// CreateUser - Create user
|
type UserApi struct {
|
||||||
func CreateUser(c *gin.Context) {
|
// Post /v2/user
|
||||||
c.JSON(http.StatusOK, gin.H{})
|
// Create user
|
||||||
}
|
CreateUser gin.HandlerFunc
|
||||||
|
// Post /v2/user/createWithArray
|
||||||
// CreateUsersWithArrayInput - Creates list of users with given input array
|
// Creates list of users with given input array
|
||||||
func CreateUsersWithArrayInput(c *gin.Context) {
|
CreateUsersWithArrayInput gin.HandlerFunc
|
||||||
c.JSON(http.StatusOK, gin.H{})
|
// Post /v2/user/createWithList
|
||||||
}
|
// Creates list of users with given input array
|
||||||
|
CreateUsersWithListInput gin.HandlerFunc
|
||||||
// CreateUsersWithListInput - Creates list of users with given input array
|
// Delete /v2/user/:username
|
||||||
func CreateUsersWithListInput(c *gin.Context) {
|
// Delete user
|
||||||
c.JSON(http.StatusOK, gin.H{})
|
DeleteUser gin.HandlerFunc
|
||||||
}
|
// Get /v2/user/:username
|
||||||
|
// Get user by user name
|
||||||
// DeleteUser - Delete user
|
GetUserByName gin.HandlerFunc
|
||||||
func DeleteUser(c *gin.Context) {
|
// Get /v2/user/login
|
||||||
c.JSON(http.StatusOK, gin.H{})
|
// Logs user into the system
|
||||||
}
|
LoginUser gin.HandlerFunc
|
||||||
|
// Get /v2/user/logout
|
||||||
// GetUserByName - Get user by user name
|
// Logs out current logged in user session
|
||||||
func GetUserByName(c *gin.Context) {
|
LogoutUser gin.HandlerFunc
|
||||||
c.JSON(http.StatusOK, gin.H{})
|
// Put /v2/user/:username
|
||||||
}
|
// Updated user
|
||||||
|
UpdateUser gin.HandlerFunc
|
||||||
// LoginUser - Logs user into the system
|
|
||||||
func LoginUser(c *gin.Context) {
|
|
||||||
c.JSON(http.StatusOK, gin.H{})
|
|
||||||
}
|
|
||||||
|
|
||||||
// LogoutUser - Logs out current logged in user session
|
|
||||||
func LogoutUser(c *gin.Context) {
|
|
||||||
c.JSON(http.StatusOK, gin.H{})
|
|
||||||
}
|
|
||||||
|
|
||||||
// UpdateUser - Updated user
|
|
||||||
func UpdateUser(c *gin.Context) {
|
|
||||||
c.JSON(http.StatusOK, gin.H{})
|
|
||||||
}
|
}
|
||||||
|
@ -27,13 +27,13 @@ type Route struct {
|
|||||||
HandlerFunc gin.HandlerFunc
|
HandlerFunc gin.HandlerFunc
|
||||||
}
|
}
|
||||||
|
|
||||||
// Routes is the list of the generated Route.
|
|
||||||
type Routes []Route
|
|
||||||
|
|
||||||
// NewRouter returns a new router.
|
// NewRouter returns a new router.
|
||||||
func NewRouter() *gin.Engine {
|
func NewRouter(handleFunctions ApiHandleFunctions) *gin.Engine {
|
||||||
router := gin.Default()
|
router := gin.Default()
|
||||||
for _, route := range routes {
|
for _, route := range getRoutes(handleFunctions) {
|
||||||
|
if route.HandlerFunc == nil {
|
||||||
|
route.HandlerFunc = DefaultHandleFunc
|
||||||
|
}
|
||||||
switch route.Method {
|
switch route.Method {
|
||||||
case http.MethodGet:
|
case http.MethodGet:
|
||||||
router.GET(route.Pattern, route.HandlerFunc)
|
router.GET(route.Pattern, route.HandlerFunc)
|
||||||
@ -51,156 +51,143 @@ func NewRouter() *gin.Engine {
|
|||||||
return router
|
return router
|
||||||
}
|
}
|
||||||
|
|
||||||
// Index is the index handler.
|
// Default handler for not yet implemented routes
|
||||||
func Index(c *gin.Context) {
|
func DefaultHandleFunc(c *gin.Context) {
|
||||||
c.String(http.StatusOK, "Hello World!")
|
c.String(http.StatusNotImplemented, "501 not implemented")
|
||||||
}
|
}
|
||||||
|
|
||||||
var routes = Routes{
|
type ApiHandleFunctions struct {
|
||||||
{
|
|
||||||
"Index",
|
// Routes for the PetApi part of the API
|
||||||
http.MethodGet,
|
PetApi PetApi
|
||||||
"/v2/",
|
// Routes for the StoreApi part of the API
|
||||||
Index,
|
StoreApi StoreApi
|
||||||
},
|
// Routes for the UserApi part of the API
|
||||||
|
UserApi UserApi
|
||||||
|
}
|
||||||
|
|
||||||
|
func getRoutes(handleFunctions ApiHandleFunctions) []Route {
|
||||||
|
return []Route{
|
||||||
|
|
||||||
{
|
{
|
||||||
"AddPet",
|
"AddPet",
|
||||||
http.MethodPost,
|
http.MethodPost,
|
||||||
"/v2/pet",
|
"/v2/pet",
|
||||||
AddPet,
|
handleFunctions.PetApi.AddPet,
|
||||||
},
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
"DeletePet",
|
"DeletePet",
|
||||||
http.MethodDelete,
|
http.MethodDelete,
|
||||||
"/v2/pet/:petId",
|
"/v2/pet/:petId",
|
||||||
DeletePet,
|
handleFunctions.PetApi.DeletePet,
|
||||||
},
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
"FindPetsByStatus",
|
"FindPetsByStatus",
|
||||||
http.MethodGet,
|
http.MethodGet,
|
||||||
"/v2/pet/findByStatus",
|
"/v2/pet/findByStatus",
|
||||||
FindPetsByStatus,
|
handleFunctions.PetApi.FindPetsByStatus,
|
||||||
},
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
"FindPetsByTags",
|
"FindPetsByTags",
|
||||||
http.MethodGet,
|
http.MethodGet,
|
||||||
"/v2/pet/findByTags",
|
"/v2/pet/findByTags",
|
||||||
FindPetsByTags,
|
handleFunctions.PetApi.FindPetsByTags,
|
||||||
},
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
"GetPetById",
|
"GetPetById",
|
||||||
http.MethodGet,
|
http.MethodGet,
|
||||||
"/v2/pet/:petId",
|
"/v2/pet/:petId",
|
||||||
GetPetById,
|
handleFunctions.PetApi.GetPetById,
|
||||||
},
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
"UpdatePet",
|
"UpdatePet",
|
||||||
http.MethodPut,
|
http.MethodPut,
|
||||||
"/v2/pet",
|
"/v2/pet",
|
||||||
UpdatePet,
|
handleFunctions.PetApi.UpdatePet,
|
||||||
},
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
"UpdatePetWithForm",
|
"UpdatePetWithForm",
|
||||||
http.MethodPost,
|
http.MethodPost,
|
||||||
"/v2/pet/:petId",
|
"/v2/pet/:petId",
|
||||||
UpdatePetWithForm,
|
handleFunctions.PetApi.UpdatePetWithForm,
|
||||||
},
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
"UploadFile",
|
"UploadFile",
|
||||||
http.MethodPost,
|
http.MethodPost,
|
||||||
"/v2/pet/:petId/uploadImage",
|
"/v2/pet/:petId/uploadImage",
|
||||||
UploadFile,
|
handleFunctions.PetApi.UploadFile,
|
||||||
},
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
"DeleteOrder",
|
"DeleteOrder",
|
||||||
http.MethodDelete,
|
http.MethodDelete,
|
||||||
"/v2/store/order/:orderId",
|
"/v2/store/order/:orderId",
|
||||||
DeleteOrder,
|
handleFunctions.StoreApi.DeleteOrder,
|
||||||
},
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
"GetInventory",
|
"GetInventory",
|
||||||
http.MethodGet,
|
http.MethodGet,
|
||||||
"/v2/store/inventory",
|
"/v2/store/inventory",
|
||||||
GetInventory,
|
handleFunctions.StoreApi.GetInventory,
|
||||||
},
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
"GetOrderById",
|
"GetOrderById",
|
||||||
http.MethodGet,
|
http.MethodGet,
|
||||||
"/v2/store/order/:orderId",
|
"/v2/store/order/:orderId",
|
||||||
GetOrderById,
|
handleFunctions.StoreApi.GetOrderById,
|
||||||
},
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
"PlaceOrder",
|
"PlaceOrder",
|
||||||
http.MethodPost,
|
http.MethodPost,
|
||||||
"/v2/store/order",
|
"/v2/store/order",
|
||||||
PlaceOrder,
|
handleFunctions.StoreApi.PlaceOrder,
|
||||||
},
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
"CreateUser",
|
"CreateUser",
|
||||||
http.MethodPost,
|
http.MethodPost,
|
||||||
"/v2/user",
|
"/v2/user",
|
||||||
CreateUser,
|
handleFunctions.UserApi.CreateUser,
|
||||||
},
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
"CreateUsersWithArrayInput",
|
"CreateUsersWithArrayInput",
|
||||||
http.MethodPost,
|
http.MethodPost,
|
||||||
"/v2/user/createWithArray",
|
"/v2/user/createWithArray",
|
||||||
CreateUsersWithArrayInput,
|
handleFunctions.UserApi.CreateUsersWithArrayInput,
|
||||||
},
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
"CreateUsersWithListInput",
|
"CreateUsersWithListInput",
|
||||||
http.MethodPost,
|
http.MethodPost,
|
||||||
"/v2/user/createWithList",
|
"/v2/user/createWithList",
|
||||||
CreateUsersWithListInput,
|
handleFunctions.UserApi.CreateUsersWithListInput,
|
||||||
},
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
"DeleteUser",
|
"DeleteUser",
|
||||||
http.MethodDelete,
|
http.MethodDelete,
|
||||||
"/v2/user/:username",
|
"/v2/user/:username",
|
||||||
DeleteUser,
|
handleFunctions.UserApi.DeleteUser,
|
||||||
},
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
"GetUserByName",
|
"GetUserByName",
|
||||||
http.MethodGet,
|
http.MethodGet,
|
||||||
"/v2/user/:username",
|
"/v2/user/:username",
|
||||||
GetUserByName,
|
handleFunctions.UserApi.GetUserByName,
|
||||||
},
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
"LoginUser",
|
"LoginUser",
|
||||||
http.MethodGet,
|
http.MethodGet,
|
||||||
"/v2/user/login",
|
"/v2/user/login",
|
||||||
LoginUser,
|
handleFunctions.UserApi.LoginUser,
|
||||||
},
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
"LogoutUser",
|
"LogoutUser",
|
||||||
http.MethodGet,
|
http.MethodGet,
|
||||||
"/v2/user/logout",
|
"/v2/user/logout",
|
||||||
LogoutUser,
|
handleFunctions.UserApi.LogoutUser,
|
||||||
},
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
"UpdateUser",
|
"UpdateUser",
|
||||||
http.MethodPut,
|
http.MethodPut,
|
||||||
"/v2/user/:username",
|
"/v2/user/:username",
|
||||||
UpdateUser,
|
handleFunctions.UserApi.UpdateUser,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
@ -19,9 +19,11 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
routes := sw.ApiHandleFunctions{}
|
||||||
|
|
||||||
log.Printf("Server started")
|
log.Printf("Server started")
|
||||||
|
|
||||||
router := sw.NewRouter()
|
router := sw.NewRouter(routes)
|
||||||
|
|
||||||
log.Fatal(router.Run(":8080"))
|
log.Fatal(router.Run(":8080"))
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user