apigen/test/goapi/apigen.go

73 lines
1.2 KiB
Go

package goapi
import (
"github.com/gin-gonic/gin"
)
import "crypto"
var perms map[string]string
func init() {
perms = make(map[string]string)
perms["POST_/someapi"] = "ASD"
perms["DELETE_/someapi"] = "ASD"
perms["GET_/someapi"] = "ASD"
perms["PUT_/someapi"] = "ASD"
}
func GetPerm(c *gin.Context) string {
perm, ok := perms[c.Request.Method+"_"+c.Request.URL.Path]
if !ok {
return ""
}
return perm
}
func Build(r *gin.Engine) *gin.Engine {
r.DELETE("/someapi", func(c *gin.Context) {
var req *crypto.Hash
c.BindJSON(req)
res, err := SomeAPI2(c.Request.Context(), req)
if err != nil {
c.Error(err)
}
c.JSON(200, res)
})
r.GET("/someapi", func(c *gin.Context) {
var req string
c.BindJSON(&req)
res, err := SomeGET(c.Request.Context(), req)
if err != nil {
c.Error(err)
}
c.JSON(200, res)
})
r.POST("/someapi", func(c *gin.Context) {
var req string
c.BindJSON(&req)
res, err := SomeAPI(c.Request.Context(), req)
if err != nil {
c.Error(err)
}
c.JSON(200, res)
})
r.PUT("/someapi", func(c *gin.Context) {
var req string
c.BindJSON(&req)
res, err := SomePUT(c.Request.Context(), req)
if err != nil {
c.Error(err)
}
c.JSON(200, res)
})
return r
}