Chi-authz is an authorization middleware for Chi, it's based on https://github.com/casbin/casbin.
go get github.com/casbin/chi-authz
package main
import (
"net/http"
"github.com/casbin/chi-authz"
"github.com/casbin/casbin"
"github.com/go-chi/chi"
)
func main() {
router := chi.NewRouter()
// load the casbin model and policy from files, database is also supported.
e := casbin.NewEnforcer("authz_model.conf", "authz_policy.csv")
router.Use(authz.Authorizer(e))
// define your handler, this is just an example to return HTTP 200 for any requests.
// the access that is denied by authz will return HTTP 403 error.
router.HandleFunc("/*", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
})
}
This project is under MIT License. See the LICENSE file for the full license text.