Skip to content

collinglass/mw

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

mw Coverage Status GoDoc

mw is a middleware decorator for go servers.

Getting started

To start using mw, install Go and run go get:

$ go get github.com/collinglass/mw

Using it with existing middleware

import (
	// ...
	"github.com/collinglass/mw"
	"github.com/justinas/nosurf"
)

// decorate router
server := mw.Decorate(
	http.NewServeMux(),
	// add middleware from existing packages
	nosurf.NewPure,
)

Building your own middleware

import (
	// ...
	"github.com/collinglass/mw"
)

// Create middleware from scratch
func JSONMiddleware(h http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		// Set Response Header "Content-Type" to JSON
		w.Header().Set("Content-Type", "application/json")

		h.ServeHTTP(w, r)
	})
}

Using it with gorilla mux

	// new router
	r := mux.NewRouter()

	r.HandleFunc("/api/data", DataHandler).Methods("GET")

	// decorate router
	server := mw.Decorate(
		r,
		nosurf.NewPure,
		JSONMiddleware,
	)

	http.Handle("/api/", server)
	err := http.ListenAndServe(":8080", nil)
	if err != nil {
		panic(err)
	}

Using it with http.ServeMux

	// new router
	r := http.NewServeMux()

	r.HandleFunc("/api/data", DataHandler)

	// decorate router
	server := mw.Decorate(
		r,
		nosurf.NewPure,
		JSONMiddleware,
	)

	http.Handle("/api/", server)
	err := http.ListenAndServe(":8080", nil)
	if err != nil {
		panic(err)
	}

About

Middleware decorator for go servers.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages