MCPcopy
hub / github.com/justinas/alice

github.com/justinas/alice @v1.2.0 sqlite

repository ↗ · DeepWiki ↗ · release v1.2.0 ↗
19 symbols 73 edges 2 files 9 documented · 47%
README

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.

Extension points exported contracts — how you extend this code

Constructor (FuncType)
A constructor for a piece of middleware. Some middleware use this constructor out of the box, so in most cases you can j
chain.go

Core symbols most depended-on inside this repo

New
called by 12
chain.go
Then
called by 7
chain.go
Append
called by 3
chain.go
ThenFunc
called by 2
chain.go
Extend
called by 2
chain.go

Shape

Function 13
Method 4
FuncType 1
Struct 1

Languages

Go100%

Modules by API surface

chain_test.go12 symbols
chain.go7 symbols

For agents

$ claude mcp add alice \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact