Register Dependency from Context
// RegisterDependency registers a struct or slice
// or pointer to struct dependency at request-time
// for the next handler in the chain. One value per type.
// Note that it's highly recommended to register
// your dependencies before server ran
// through Party.ConfigureContainer or mvc.Application.Register
// in sake of minimum performance cost.
RegisterDependency(v interface{})
// UnregisterDependency removes a dependency based on its type.
// Reports whether a dependency with that type was
// found and removed successfully.
UnregisterDependency(typ reflect.Type) bool// Role struct value example.
type Role struct {
Name string
}
const roleContextKey = "myapp.role"
// RoleMiddleware example of a custom middleware.
func RoleMiddleware(ctx iris.Context) {
// [do it yourself: extract the role from the request...]
if ctx.URLParam("name") != "kataras" {
ctx.StopWithStatus(iris.StatusUnauthorized)
return
}
//
role := Role{Name: "admin"}
// Share the role value to the next handler(s).
ctx.Values().Set(roleContextKey, role)Last updated
Was this helpful?