-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
40 lines (31 loc) · 911 Bytes
/
main.go
File metadata and controls
40 lines (31 loc) · 911 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package main
import (
"log"
"github.com/casbin/casbin/v2"
"github.com/casbin/gin-casbin-example/handler"
"github.com/gin-contrib/authz"
"github.com/gin-gonic/gin"
)
func init() {
gin.SetMode(gin.ReleaseMode)
}
func setupRouter() (router *gin.Engine) {
// load the casbin model and policy from files, database is also supported.
e, err := casbin.NewEnforcer("authz_model.conf", "authz_policy.csv")
if err != nil {
return
}
// define your router, and use the Casbin authz middleware.
// the access that is denied by authz will return HTTP 403 error.
router = gin.New()
router.Use(authz.NewAuthorizer(e))
dataset1 := router.Group("/dataset1")
dataset1.Any("/:resource", handler.Dataset1)
dataset1.POST("/resource1", handler.CreateResource1)
router.Any("/dataset2/resource1", handler.Dataset2Resource1)
return
}
func main() {
router := setupRouter()
log.Fatal(router.Run(":8080"))
}