Iris Community Middleware List

August 20, 2023 ยท View on GitHub

This repository provides a way to share community-based middlewares for Iris Web Framework. Among with those, you can also navigate through the builtin Iris handlers.

Installation

Install a middleware, take for example the jwt one.

$ go env -w GOPROXY=goproxy.cn,gocenter.io,goproxy.io,direct
$ go mod init myapp
$ go get github.com/kataras/iris/v12@main
$ go get github.com/iris-contrib/middleware/jwt@master

import as

import "github.com/iris-contrib/middleware/jwt"

// [...Code]

build

$ go build

Middleware is just a chain handlers which can be executed before or after the main handler, can transfer data between handlers and communicate with third-party libraries, they are just functions.

MiddlewareDescriptionExample
pgPostgreSQL Databasepg/_examples
jwtJSON Web Tokensjwt/_example
corsHTTP Access Control.cors/_example
secureMiddleware that implements a few quick security winssecure/_example
tollboothGeneric middleware to rate-limit HTTP requeststollboothic/_examples/limit-handler
cloudwatchAWS cloudwatch metrics middlewarecloudwatch/_example
newrelic/v3Official New Relic Go Agentnewrelic/_example
prometheusEasily create metrics endpoint for the prometheus instrumentation toolprometheus/_example
casbinAn authorization library that supports access control models like ACL, RBAC, ABACcasbin/_examples
sentry-go (ex. raven)Sentry client in Gosentry-go/example/iris
csrfCross-Site Request Forgery Protectioncsrf/_example
throttlerRate limiting access to HTTP endpointsthrottler/_example
expmetricExpvar for counting requests etc.expmetric/_example
zapProvides log handling using zap packagezap/_examples

Register a middleware

To a single route

app := iris.New()
app.Get("/mypath",
  onBegin,
  mySecondMiddleware,
  mainHandler,
)

func onBegin(ctx iris.Context) { /* ... */ ctx.Next() }
func mySecondMiddleware(ctx iris.Context) { /* ... */ ctx.Next() }
func mainHandler(ctx iris.Context) { /* ... */ }

To a party of routes or subdomain


p := app.Party("/sellers", authMiddleware, logMiddleware)

OR

p := app.Party("/customers")
p.Use(logMiddleware)

To all routes

app.Use(func(ctx iris.Context) { }, myMiddleware2)

To global, all registered routes (including the http errors)

app.UseGlobal(func(ctx iris.Context) { }, myMiddleware2)

To Party and its children, even on unmatched routes and errors

app.UseRouter(func(ctx iris.Context) { }, myMiddleware2))

Can I use standard net/http handler with iris?

Yes you can, just pass the Handler inside the iris.FromStd in order to be converted into iris.Handler and register it as you saw before.

Convert handler which has the form of http.Handler/HandlerFunc

package main

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

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

    sillyHTTPHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request){
            println(r.RequestURI)
    })

    sillyConvertedToIon := iris.FromStd(sillyHTTPHandler)
    // FromStd can take (http.ResponseWriter, *http.Request, next http.Handler) too!
    app.Use(sillyConvertedToIon)

    app.Listen(":8080")
}

Contributing

If you are interested in contributing to this project, please push a PR.

People

List of all contributors