2021-01-30 09:53:34 +00:00
|
|
|
package goapi
|
|
|
|
|
|
|
|
import (
|
2021-04-02 14:40:28 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
2021-01-30 09:53:34 +00:00
|
|
|
)
|
|
|
|
|
2021-04-02 14:40:28 +00:00
|
|
|
import "crypto"
|
2021-01-30 09:53:34 +00:00
|
|
|
|
2021-04-02 14:40:28 +00:00
|
|
|
var perms map[string]string
|
2021-01-30 09:53:34 +00:00
|
|
|
|
2021-04-02 14:40:28 +00:00
|
|
|
func init() {
|
|
|
|
perms = make(map[string]string)
|
2021-01-30 09:53:34 +00:00
|
|
|
|
2021-04-02 14:40:28 +00:00
|
|
|
perms["POST_/someapi"] = "ASD"
|
2021-01-30 09:53:34 +00:00
|
|
|
|
2021-04-02 14:40:28 +00:00
|
|
|
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
|
2021-01-30 09:53:34 +00:00
|
|
|
}
|
|
|
|
|
2021-04-02 14:40:28 +00:00
|
|
|
func Build(r *gin.Engine) *gin.Engine {
|
2021-01-30 09:53:34 +00:00
|
|
|
|
2021-04-02 14:40:28 +00:00
|
|
|
r.DELETE("/someapi", func(c *gin.Context) {
|
|
|
|
var req *crypto.Hash
|
|
|
|
c.BindJSON(req)
|
|
|
|
res, err := SomeAPI2(c.Request.Context(), req)
|
2021-01-30 09:53:34 +00:00
|
|
|
if err != nil {
|
2021-04-02 14:40:28 +00:00
|
|
|
c.Error(err)
|
2021-01-30 09:53:34 +00:00
|
|
|
}
|
2021-04-02 14:40:28 +00:00
|
|
|
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)
|
|
|
|
})
|
2021-01-30 09:53:34 +00:00
|
|
|
|
2021-04-02 14:40:28 +00:00
|
|
|
return r
|
2021-01-30 09:53:34 +00:00
|
|
|
}
|