-
-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from ali-shokoohi/feature/add-gin-handler
Feature/add gin handler
- Loading branch information
Showing
9 changed files
with
358 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,3 +14,6 @@ casbin-server | |
|
||
.idea/ | ||
*.iml | ||
|
||
# Environment Variables | ||
.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package dto | ||
|
||
import ( | ||
pb "github.com/casbin/casbin-server/proto" | ||
) | ||
|
||
type EnforceRequest struct { | ||
pb.EnforceRequest | ||
} | ||
|
||
type EnforceResponse struct { | ||
pb.BoolReply | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package gin | ||
|
||
import ( | ||
"context" | ||
"log" | ||
"net/http" | ||
|
||
"github.com/casbin/casbin-server/dto" | ||
"github.com/casbin/casbin-server/handler" | ||
"github.com/casbin/casbin-server/proto" | ||
server "github.com/casbin/casbin-server/server" | ||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
type GinContext struct { | ||
Ctx *gin.Context | ||
} | ||
|
||
func (g *GinContext) Bind(v interface{}) error { | ||
return g.Ctx.Bind(v) | ||
} | ||
|
||
func (g *GinContext) ShouldBind(v interface{}) error { | ||
return g.Ctx.ShouldBind(v) | ||
} | ||
|
||
func (g *GinContext) JSON(statusCode int, v interface{}) error { | ||
g.Ctx.JSON(statusCode, v) | ||
return nil | ||
} | ||
|
||
func (g *GinContext) Param(key string) string { | ||
return g.Ctx.Param(key) | ||
} | ||
|
||
func (g *GinContext) QueryParam(key string) string { | ||
return g.Ctx.Query(key) | ||
} | ||
|
||
type httpHandler struct { | ||
server *server.Server | ||
} | ||
|
||
func NewHttpHandler( | ||
server *server.Server, | ||
) handler.HttpHandler { | ||
return &httpHandler{ | ||
server: server, | ||
} | ||
} | ||
|
||
func (h *httpHandler) Enforce(c handler.Context) { | ||
var response dto.EnforceResponse | ||
var request dto.EnforceRequest | ||
ctx := context.Background() | ||
err := c.ShouldBind(&request) | ||
if err != nil { | ||
c.JSON(http.StatusBadRequest, response) | ||
return | ||
} | ||
// Create New Enforcer if input handler == -1 | ||
if request.GetEnforcerHandler() == -1 { | ||
e, err := h.server.NewEnforcer(ctx, &proto.NewEnforcerRequest{AdapterHandle: -1}) | ||
if err != nil { | ||
log.Println("Error at calling NewEnforcer in Enforce handler:", err.Error()) | ||
c.JSON(http.StatusInternalServerError, response) | ||
return | ||
} | ||
request.EnforcerHandler = e.GetHandler() | ||
} | ||
allowed, err := h.server.Enforce( | ||
ctx, | ||
&proto.EnforceRequest{ | ||
EnforcerHandler: request.EnforcerHandler, | ||
Params: request.Params, | ||
}, | ||
) | ||
if err != nil { | ||
log.Println("Error at calling Enforce in Enforce handler:", err.Error()) | ||
c.JSON(http.StatusInternalServerError, response) | ||
return | ||
} | ||
response.BoolReply = *allowed | ||
|
||
if response.GetRes() { | ||
c.JSON(http.StatusOK, response) | ||
return | ||
} else { | ||
c.JSON(http.StatusForbidden, response) | ||
return | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package handler | ||
|
||
type HttpHandler interface { | ||
Enforce(c Context) | ||
} | ||
|
||
type Context interface { | ||
Bind(interface{}) error | ||
ShouldBind(interface{}) error | ||
JSON(int, interface{}) error | ||
Param(string) string | ||
QueryParam(string) string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package gin | ||
|
||
import ( | ||
ginHandler "github.com/casbin/casbin-server/handler/gin" | ||
"github.com/casbin/casbin-server/router" | ||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
type GinRouter struct { | ||
engine *gin.Engine | ||
} | ||
|
||
func New() *GinRouter { | ||
return &GinRouter{engine: gin.Default()} | ||
} | ||
|
||
type GinRouterGroup struct { | ||
routerGroup *gin.RouterGroup | ||
} | ||
|
||
func NewGroup(routerGroup *gin.RouterGroup) *GinRouterGroup { | ||
return &GinRouterGroup{routerGroup: routerGroup} | ||
} | ||
|
||
func (r *GinRouter) GET(path string, handler router.HandlerFunc) { | ||
r.engine.GET(path, func(c *gin.Context) { | ||
handler(&ginHandler.GinContext{Ctx: c}) // Convert gin.Context to your custom Context | ||
}) | ||
} | ||
|
||
func (r *GinRouter) POST(path string, handler router.HandlerFunc) { | ||
r.engine.POST(path, func(c *gin.Context) { | ||
handler(&ginHandler.GinContext{Ctx: c}) | ||
}) | ||
} | ||
func (r *GinRouter) PUT(path string, handler router.HandlerFunc) { | ||
r.engine.PUT(path, func(c *gin.Context) { | ||
handler(&ginHandler.GinContext{Ctx: c}) | ||
}) | ||
} | ||
func (r *GinRouter) DELETE(path string, handler router.HandlerFunc) { | ||
r.engine.DELETE(path, func(c *gin.Context) { | ||
handler(&ginHandler.GinContext{Ctx: c}) | ||
}) | ||
} | ||
func (r *GinRouter) OPTIONS(path string, handler router.HandlerFunc) { | ||
r.engine.OPTIONS(path, func(c *gin.Context) { | ||
handler(&ginHandler.GinContext{Ctx: c}) | ||
}) | ||
} | ||
func (r *GinRouter) Serve(addr string) error { | ||
return r.engine.Run(addr) | ||
} | ||
|
||
func (r *GinRouter) Group(relativePath string, handlers ...router.HandlerFunc) router.RouterGroup { | ||
return NewGroup(r.engine.Group(relativePath)) | ||
} | ||
|
||
func (r *GinRouterGroup) GET(path string, handler router.HandlerFunc) { | ||
r.routerGroup.GET(path, func(c *gin.Context) { | ||
handler(&ginHandler.GinContext{Ctx: c}) | ||
}) | ||
} | ||
|
||
func (r *GinRouterGroup) POST(path string, handler router.HandlerFunc) { | ||
r.routerGroup.POST(path, func(c *gin.Context) { | ||
handler(&ginHandler.GinContext{Ctx: c}) | ||
}) | ||
} | ||
func (r *GinRouterGroup) PUT(path string, handler router.HandlerFunc) { | ||
r.routerGroup.PUT(path, func(c *gin.Context) { | ||
handler(&ginHandler.GinContext{Ctx: c}) | ||
}) | ||
} | ||
func (r *GinRouterGroup) DELETE(path string, handler router.HandlerFunc) { | ||
r.routerGroup.DELETE(path, func(c *gin.Context) { | ||
handler(&ginHandler.GinContext{Ctx: c}) | ||
}) | ||
} | ||
func (r *GinRouterGroup) OPTIONS(path string, handler router.HandlerFunc) { | ||
r.routerGroup.OPTIONS(path, func(c *gin.Context) { | ||
handler(&ginHandler.GinContext{Ctx: c}) | ||
}) | ||
} | ||
|
||
func (r *GinRouterGroup) Group(relativePath string, handlers ...router.HandlerFunc) router.RouterGroup { | ||
return NewGroup(r.routerGroup.Group(relativePath)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package router | ||
|
||
import "github.com/casbin/casbin-server/handler" | ||
|
||
type RouterGroup interface { | ||
GET(path string, handler HandlerFunc) | ||
POST(path string, handler HandlerFunc) | ||
PUT(path string, handler HandlerFunc) | ||
DELETE(path string, handler HandlerFunc) | ||
OPTIONS(path string, handler HandlerFunc) | ||
Group(relativePath string, handlers ...HandlerFunc) RouterGroup | ||
} | ||
|
||
type Router interface { | ||
RouterGroup | ||
Serve(addr string) error | ||
} | ||
|
||
type HandlerFunc func(handler.Context) |