Code Monkey home page Code Monkey logo

middleware's Introduction

This repository provides a way to share any minor handlers for iris web framework. You can view the built'n supported handlers by pressing here.

Installation

Install a middleware, take for example the cors one.

$ go get github.com/iris-contrib/middleware/cors

import as

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

// [...]

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
jwt Middleware checks for a JWT on the Authorization header on incoming requests and decodes it. 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. tollbooth/_examples/limit-handler
cloudwatch AWS cloudwatch metrics middleware. cloudwatch/_example
new relic 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
raven Sentry client in Go raven/_example
csrf Cross-Site Request Forgery Protection csrf/_example

How can I register middleware?

To a single route

app := iris.New()
app.Get("/mypath", myMiddleware1, myMiddleware2, func(ctx iris.Context){}, func(ctx iris.Context){}, myMiddleware5,myMainHandlerLast)

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 routes, parties and subdomains

app.UseGlobal(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.Run(iris.Addr(":8080"))
}

Contributing

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

People

List of all contributors

middleware's People

Contributors

kataras avatar saeid-ir avatar zaniadeveloper avatar hiveminded avatar thomasbarton avatar xyproto avatar heralight avatar alexisvisco avatar fredcarle avatar bliuchak avatar jonnylangefeld avatar douglarek avatar emj365 avatar godofdream avatar memwey avatar yazgoo avatar syyongx avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.