All Projects → justinas → Alice

justinas / Alice

Licence: mit
Painless middleware chaining for Go

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to Alice

Dotweb
Simple and easy go web micro framework
Stars: ✭ 1,354 (-44.46%)
Mutual labels:  middleware, handler
Middleware
Community Middleware List for the Iris Web Framework.
Stars: ✭ 188 (-92.29%)
Mutual labels:  middleware, handler
AspNetCore.Weixin
An ASP.NET Core middleware for Wechat/Weixin message handling and apis. (微信公众平台/接口调用服务)
Stars: ✭ 24 (-99.02%)
Mutual labels:  middleware, handler
Serve Handler
The foundation of `serve`
Stars: ✭ 349 (-85.68%)
Mutual labels:  middleware, handler
Go Httpwares
Go HTTP Server Middleware and Client Tripperware
Stars: ✭ 60 (-97.54%)
Mutual labels:  middleware, handler
Nex
Aiming to simplify the construction of JSON API service
Stars: ✭ 35 (-98.56%)
Mutual labels:  middleware, handler
Msngr.js
An asynchronous messaging library, written in JavaScript, for node and the web browser
Stars: ✭ 337 (-86.18%)
Mutual labels:  middleware, handler
Handlers
A collection of useful middleware for Go HTTP services & web applications 🛃
Stars: ✭ 1,174 (-51.85%)
Mutual labels:  middleware, handler
Rayo.js
Micro framework for Node.js
Stars: ✭ 170 (-93.03%)
Mutual labels:  middleware, handler
Owin Webapi Service
✅ OWIN / WebAPI windows service example. Includes attribute based routing sample
Stars: ✭ 175 (-92.82%)
Mutual labels:  handler
Vue Gates
🔒 A Vue.js & Nuxt.js plugin that allows you to use roles and permissions in your components or DOM elements, also compatible as middleware and methods.
Stars: ✭ 184 (-92.45%)
Mutual labels:  middleware
Routerify
A lightweight, idiomatic, composable and modular router implementation with middleware support for the Rust HTTP library hyper.rs
Stars: ✭ 173 (-92.9%)
Mutual labels:  middleware
Kemalyst
A rails like framework based on kemal
Stars: ✭ 176 (-92.78%)
Mutual labels:  middleware
Ego
Ego is a full-stack web framework written in Go, lightweight and efficient front-end component solutions, based on gin. The front-end is compiled, does not affect the back-end.
Stars: ✭ 185 (-92.41%)
Mutual labels:  middleware
Laravel Http2 Server Push
A middleware package for Laravel to enable server push for your script, style, and image assets.
Stars: ✭ 174 (-92.86%)
Mutual labels:  middleware
Netflux
JavaScript client and server side transport API based on WebRTC & WebSocket
Stars: ✭ 188 (-92.29%)
Mutual labels:  middleware
Laravel Shield
A HTTP basic auth middleware for Laravel
Stars: ✭ 193 (-92.08%)
Mutual labels:  middleware
Http Cache
High performance Golang HTTP middleware for server-side application layer caching, ideal for REST APIs
Stars: ✭ 184 (-92.45%)
Mutual labels:  middleware
Nevercrash
🌍 全局捕获Crash。信NeverCrash,永不Crash。
Stars: ✭ 170 (-93.03%)
Mutual labels:  handler
Host Validation
Express.js middleware for "Host" and "Referer" header validation to protect against DNS rebinding attacks.
Stars: ✭ 183 (-92.49%)
Mutual labels:  middleware

Alice

GoDoc Build Status Coverage

Alice provides a convenient way to chain your HTTP middleware functions and the app handler.

In short, it transforms

Middleware1(Middleware2(Middleware3(App)))

to

alice.New(Middleware1, Middleware2, Middleware3).Then(App)

Why?

None of the other middleware chaining solutions behaves exactly like Alice. Alice is as minimal as it gets: in essence, it's just a for loop that does the wrapping for you.

Check out this blog post for explanation how Alice is different from other chaining solutions.

Usage

Your middleware constructors should have the form of

func (http.Handler) http.Handler

Some middleware provide this out of the box. For ones that don't, it's trivial to write one yourself.

func myStripPrefix(h http.Handler) http.Handler {
    return http.StripPrefix("/old", h)
}

This complete example shows the full power of Alice.

package main

import (
    "net/http"
    "time"

    "github.com/throttled/throttled"
    "github.com/justinas/alice"
    "github.com/justinas/nosurf"
)

func timeoutHandler(h http.Handler) http.Handler {
    return http.TimeoutHandler(h, 1*time.Second, "timed out")
}

func myApp(w http.ResponseWriter, r *http.Request) {
    w.Write([]byte("Hello world!"))
}

func main() {
    th := throttled.Interval(throttled.PerSec(10), 1, &throttled.VaryBy{Path: true}, 50)
    myHandler := http.HandlerFunc(myApp)

    chain := alice.New(th.Throttle, timeoutHandler, nosurf.NewPure).Then(myHandler)
    http.ListenAndServe(":8000", chain)
}

Here, the request will pass throttled first, then an http.TimeoutHandler we've set up, then nosurf and will finally reach our handler.

Note that Alice makes no guarantees for how one or another piece of middleware will behave. Once it passes the execution to the outer layer of middleware, it has no saying in whether middleware will execute the inner handlers. This is intentional behavior.

Alice works with Go 1.0 and higher.

Contributing

  1. Find an issue that bugs you / open a new one.
  2. Discuss.
  3. Branch off, commit, test.
  4. Make a pull request / attach the commits to the issue.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].