2019-08-16 15:57:03 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-10-25 00:27:02 +02:00
|
|
|
"github.com/kataras/iris/v12"
|
2019-08-16 15:57:03 +02:00
|
|
|
|
|
|
|
"github.com/casbin/casbin/v2"
|
|
|
|
cm "github.com/iris-contrib/middleware/casbin"
|
|
|
|
)
|
|
|
|
|
|
|
|
// $ go get github.com/casbin/casbin/v2
|
|
|
|
// $ go run main.go
|
|
|
|
|
|
|
|
// Enforcer maps the model and the policy for the casbin service, we use this variable on the main_test too.
|
|
|
|
var Enforcer, _ = casbin.NewEnforcer("casbinmodel.conf", "casbinpolicy.csv")
|
|
|
|
|
|
|
|
func newApp() *iris.Application {
|
|
|
|
casbinMiddleware := cm.New(Enforcer)
|
|
|
|
|
|
|
|
app := iris.New()
|
|
|
|
app.Use(casbinMiddleware.ServeHTTP)
|
|
|
|
|
|
|
|
app.Get("/", hi)
|
|
|
|
|
|
|
|
app.Get("/dataset1/{p:path}", hi) // p, alice, /dataset1/*, GET
|
|
|
|
|
|
|
|
app.Post("/dataset1/resource1", hi)
|
|
|
|
|
|
|
|
app.Get("/dataset2/resource2", hi)
|
|
|
|
app.Post("/dataset2/folder1/{p:path}", hi)
|
|
|
|
|
|
|
|
app.Any("/dataset2/resource1", hi)
|
|
|
|
|
|
|
|
return app
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
app := newApp()
|
2020-03-05 21:41:27 +01:00
|
|
|
app.Listen(":8080")
|
2019-08-16 15:57:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func hi(ctx iris.Context) {
|
|
|
|
ctx.Writef("Hello %s", cm.Username(ctx.Request()))
|
|
|
|
}
|