https://github.com/sclevine/infuse
Golang middleware - fully compatible with net/http - provides shared contexts
https://github.com/sclevine/infuse
Last synced: about 1 month ago
JSON representation
Golang middleware - fully compatible with net/http - provides shared contexts
- Host: GitHub
- URL: https://github.com/sclevine/infuse
- Owner: sclevine
- License: mit
- Created: 2015-04-10T06:49:53.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2015-05-12T20:17:34.000Z (almost 10 years ago)
- Last Synced: 2025-03-02T01:24:18.189Z (about 2 months ago)
- Language: Go
- Homepage:
- Size: 256 KB
- Stars: 35
- Watchers: 6
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Infuse
======[](http://travis-ci.org/sclevine/infuse)
[](https://godoc.org/github.com/sclevine/infuse)Infuse provides an immutable, concurrency-safe middleware handler
that conforms to `http.Handler`. An `infuse.Handler` is fully compatible with
the Go standard library, supports flexible chaining, and provides a shared
context between middleware handlers without relying on global state, locks,
or shared closures.BasicAuth example:
```gofunc Example() {
authHandler := infuse.New().HandleFunc(basicAuth)
router := http.NewServeMux()
router.Handle("/hello", authHandler.HandleFunc(userGreeting))
router.Handle("/goodbye", authHandler.HandleFunc(userFarewell))
server := httptest.NewServer(router)
defer server.Close()doRequest(server.URL+"/hello", "bob", "1234")
doRequest(server.URL+"/goodbye", "alice", "5678")
doRequest(server.URL+"/goodbye", "intruder", "guess")// Output:
// Hello bob!
// Goodbye alice!
// Permission Denied
}func userGreeting(response http.ResponseWriter, request *http.Request) {
username := infuse.Get(response).(string)
fmt.Fprintf(response, "Hello %s!", username)
}func userFarewell(response http.ResponseWriter, request *http.Request) {
username := infuse.Get(response).(string)
fmt.Fprintf(response, "Goodbye %s!", username)
}func basicAuth(response http.ResponseWriter, request *http.Request) {
username, password, ok := request.BasicAuth()
if !ok || !userValid(username, password) {
response.WriteHeader(http.StatusUnauthorized)
fmt.Fprint(response, "Permission Denied")
return
}if ok := infuse.Set(response, username); !ok {
response.WriteHeader(http.StatusInternalServerError)
fmt.Fprint(response, "Server Error")
}if ok := infuse.Next(response, request); !ok {
response.WriteHeader(http.StatusInternalServerError)
fmt.Fprint(response, "Server Error")
}
}
```The `mock` package makes it easy to unit test code that depends on an
`infuse.Handler`.