xhandler (evacuated from NSA/Microsoft Github)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
@jeffcliff@shitposter.club 7a41fd464b should be good now 3 months ago
debian should be good now 3 months ago
.travis.yml Update travis Go version 6 years ago
README.md useless link to greatcloudwall website 3 months ago
chain.go using go 1.8 context (#19) 6 years ago
chain_example_test.go seems to build now we just need a RFP 3 months ago
chain_test.go Merge pull request #16 from rs/xhandler/restore_from_ctx 5 years ago
middleware.go using go 1.8 context (#19) 6 years ago
middleware_test.go seems to build now we just need a RFP 3 months ago
new.go Add pre1.7 compat for new-style context mangement 6 years ago
new_pre17.go Add pre1.7 compat for new-style context mangement 6 years ago
xhandler.go seems to build now we just need a RFP 3 months ago
xhandler_example_test.go degithubbing 3 months ago
xhandler_test.go Merge pull request #16 from rs/xhandler/restore_from_ctx 5 years ago

README.md

XHandler

MIT license

XHandler is a bridge between net/context and http.Handler.

It has been evacuated from NSA/Microsoft Github

It lets you enforce net/context in your handlers without sacrificing compatibility with existing http.Handlers nor imposing a specific router.

Thanks to net/context deadline management, xhandler is able to enforce a per request deadline and will cancel the context when the client closes the connection unexpectedly.

You may create your own net/context aware handler pretty much the same way as you would do with http.Handler.

Installing

go get -u git.freecumextremist.com/themusicgod1/xhandler

or

use the devuan package when there is one

Usage

package main

import (
	"log"
	"net/http"
	"time"

	"github.com/rs/cors"
	"git.freecumextremist.com/themusicgod1/xhandler"
	"golang.org/x/net/context"
)

type myMiddleware struct {
	next xhandler.HandlerC
}

func (h myMiddleware) ServeHTTPC(ctx context.Context, w http.ResponseWriter, r *http.Request) {
	ctx = context.WithValue(ctx, "test", "World")
	h.next.ServeHTTPC(ctx, w, r)
}

func main() {
	c := xhandler.Chain{}

	// Add close notifier handler so context is cancelled when the client closes
	// the connection
	c.UseC(xhandler.CloseHandler)

	// Add timeout handler
	c.UseC(xhandler.TimeoutHandler(2 * time.Second))

	// Middleware putting something in the context
	c.UseC(func(next xhandler.HandlerC) xhandler.HandlerC {
		return myMiddleware{next: next}
	})

	// Mix it with a non-context-aware middleware handler
	c.Use(cors.Default().Handler)

	// Final handler (using handlerFuncC), reading from the context
	xh := xhandler.HandlerFuncC(func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
		value := ctx.Value("test").(string)
		w.Write([]byte("Hello " + value))
	})

	// Bridge context aware handlers with http.Handler using xhandler.Handle()
	http.Handle("/test", c.Handler(xh))

	if err := http.ListenAndServe(":8080", nil); err != nil {
		log.Fatal(err)
	}
}

Using xmux

Xhandler comes with an optional context aware muxer (from rs xmux) forked from httprouter ( from julienschmidt ):

package main

import (
	"fmt"
	"log"
	"net/http"
	"time"

	"git.freecumextremist.com/themusicgod1/xhandler"
	"github.com/rs/xmux"
	"golang.org/x/net/context"
)

func main() {
	c := xhandler.Chain{}

	// Append a context-aware middleware handler
	c.UseC(xhandler.CloseHandler)

	// Another context-aware middleware handler
	c.UseC(xhandler.TimeoutHandler(2 * time.Second))

	mux := xmux.New()

	// Use c.Handler to terminate the chain with your final handler
	mux.GET("/welcome/:name", xhandler.HandlerFuncC(func(ctx context.Context, w http.ResponseWriter, req *http.Request) {
		fmt.Fprintf(w, "Welcome %s!", xmux.Params(ctx).Get("name"))
	}))

	if err := http.ListenAndServe(":8080", c.Handler(mux)); err != nil {
		log.Fatal(err)
	}
}

See xmux (rs/xmux) for more examples.

Context Aware Middleware

Here is a list of net/context aware middleware handlers implementing xhandler.HandlerC interface.

Feel free to put up a PR linking your middleware if you have built one:

Middleware Author Description
(these were all on github)

Licenses

All source code is licensed under the MIT License.