MyGit

iris-contrib/middleware

Fork: 91 Star: 240 (更新于 2024-12-08 17:15:41)

license: 暂无

Language: Go .

Community Middleware List for the Iris Web Framework.

最后发布版本: v12.2.11 ( 2024-04-25 07:45:02)

官方网址 GitHub网址

Iris Community Middleware List

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.

Middleware Description Example
pg PostgreSQL Database pg/_examples
jwt JSON Web Tokens jwt/_example
cors HTTP Access Control. cors/_example
secure Middleware that implements a few quick security wins secure/_example
tollbooth Generic middleware to rate-limit HTTP requests tollboothic/_examples/limit-handler
cloudwatch AWS cloudwatch metrics middleware cloudwatch/_example
newrelic/v3 Official New Relic Go Agent newrelic/_example
prometheus Easily create metrics endpoint for the prometheus instrumentation tool prometheus/_example
casbin An authorization library that supports access control models like ACL, RBAC, ABAC casbin/_examples
sentry-go (ex. raven) Sentry client in Go sentry-go/example/iris
csrf Cross-Site Request Forgery Protection csrf/_example
throttler Rate limiting access to HTTP endpoints throttler/_example
expmetric Expvar for counting requests etc. expmetric/_example
zap Provides log handling using zap package zap/_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

最近版本更新:(数据更新于 2024-10-03 18:44:18)

2024-04-25 07:45:02 v12.2.11

2023-03-15 22:29:55 v12.2.0

2019-12-20 04:46:50 v12.1.2

主题(topics):

community-driven, go, golang, handler, iris, iris-golang, middleware

iris-contrib/middleware同语言 Go最近更新仓库

2024-12-22 07:52:58 navidrome/navidrome

2024-12-21 20:15:12 SagerNet/sing-box

2024-12-21 03:25:54 SpecterOps/BloodHound

2024-12-19 23:11:24 shadow1ng/fscan

2024-12-19 21:50:56 minio/minio

2024-12-19 10:04:39 istio/istio