Iris
Getting Started
  • What is Iris
  • 📌Getting started
    • Installation
    • Quick start
  • 🔌Routing
    • Middleware
    • API Versioning
  • 🗜️Compression
    • Index
  • ✈️Redirect
    • Redirect from Context
    • Rewrite Middleware
    • Multi Application Instances
  • 🖼️ View/Templates
    • Documentation
    • Benchmarks
    • ➲ Examples
  • 📁File Server
    • Introduction
    • Listing
    • In-memory Cache
    • HTTP/2 Push + Embedded + Cache and Compression
    • The PrefixDir function
    • Serve files from Context
    • ➲ Examples
  • 🌎Localization
    • Documentation
    • Sitemap
    • ➲ Examples
  • 🛡️Security
    • Basic Authentication
    • CORS
    • Sessions & Cookies
    • CSRF
    • JSON Web Tokens
    • Access Control
    • Anti-bot CAPTCHA
    • ➲ Examples
  • 🚀Responses
    • Text
    • HTML
    • Markdown
    • XML
    • YAML
    • Binary
    • JSON
    • JSONP
    • Problem
    • Protocol Buffers
    • MessagePack
    • Gzip
    • Content Negotiation
    • Stream
    • Server-Sent Events
    • HTTP/2 Push
    • Recorder
    • Outroduction
    • ➲ Examples
  • 📥Requests
    • URL Query
    • Headers
    • URL Path Parameters
    • Form
    • Text
    • XML
    • YAML
    • Binary
    • JSON
    • Validation
    • Protocol Buffers
    • MessagePack
    • Gzip
    • ➲ Examples
  • 💉Dependency Injection
    • Documentation
    • Register Dependency from Context
    • Inputs
    • Outputs
    • ➲ Examples
  • 🦏MVC
    • Quick start
    • Documentation
    • Handle Errors
    • Sessions
    • Websockets
    • gRPC
    • ➲ Examples
  • 🤓Resources
    • Examples
    • Starter Kits
    • Publications
    • Benchmarks
    • Support
  • 📘Contents
    • Host
      • Automatic Public Domain with TLS
    • Configuration
    • Routing
      • Path Parameter Types
      • Reverse Lookups
      • Handle HTTP errors
      • Subdomains
      • Wrap the Router
      • Context Methods
    • HTTP Method Override
    • HTTP Referrer
    • URL Query Parameters
    • Forms
    • Model Validation
    • Cache
    • Cookies
    • Sessions
      • Database
      • Flash Messages
    • Websockets
    • Sitemap
    • Localization
    • Testing
Powered by GitBook
On this page

Was this helpful?

  1. 💉Dependency Injection

Register Dependency from Context

When you want to create a middleware of iris.Handler form but you want to bind any input arguments of a potential (MVC) Controller's method or dependency-injection handlers.

The ctx.RegisterDependency is the method that allows you to build and add request-time dependencies.

// 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

Let's start by creating a custom middleware that we can use in our hypothetical app.

// 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)

When you have access to the middleware itself: Use the RegisterDependency to register struct type values as dependencies at request-time for any potential dependency injection-ed user handler. This way the user of your middleware can get rid of manually register a dependency for that Role type with calls of APIContainer.RegisterDependency (and mvc.Application.Register).

    ctx.RegisterDependency(role)
	ctx.Next()
}
// GetRole returns the role inside the context values,
// the `roleMiddleware` should be executed first.
func GetRole(ctx iris.Context) (Role, bool) {
	v := ctx.Values().Get(roleContextKey)
	if v != nil {
		if role, ok := v.(Role); ok {
			return role, true
		}
	}

	return Role{}, false
}

It's time to use our RoleMiddleware, in a common iris.Handler and a handler which accepts one or more dependencies. Create a main.go file and copy-paste the following code:

package main

import "github.com/kataras/iris/v12"

func main() {
	app := iris.New()
	app.Use(RoleMiddleware)

	app.Get("/", commonHandler)
    c := app.ConfigureContainer()

When you do NOT have access to the middleware code itself then you can register a request dependency which retrieves the value from the Context and returns it, so handler/function's input arguments with that Role type can be binded.

    c.RegisterDependency(func(ctx iris.Context) Role {
        role, ok := GetRole(ctx)
        if !ok {
            // This codeblock will never be executed here
            // but you can stop executing a handler which depends on
            // that dependency with
            // `ctx.StopExecution/ctx.StopWithXXX` methods
            // or by returning a second output argument of `error` type.
            ctx.StopExecution()
            return Role{}
        }

        return role
    })
	c.Get("/dep", handlerWithDependencies)

	// http://localhost:8080?name=kataras
	// http://localhost:8080/dep?name=kataras
	app.Listen(":8080")
}

func commonHandler(ctx iris.Context) {
	role, _ := GetRole(ctx)
	ctx.WriteString(role.Name)
}

func handlerWithDependencies(role Role) string {
	return role.Name
}

That's all.

PreviousDocumentationNextInputs

Last updated 2 years ago

Was this helpful?