https://github.com/mkch/burrow
Toolkit for go net/http.
https://github.com/mkch/burrow
Last synced: about 1 year ago
JSON representation
Toolkit for go net/http.
- Host: GitHub
- URL: https://github.com/mkch/burrow
- Owner: mkch
- License: mit
- Created: 2013-10-06T11:33:52.000Z (over 12 years ago)
- Default Branch: master
- Last Pushed: 2021-12-14T12:14:44.000Z (over 4 years ago)
- Last Synced: 2025-06-04T01:56:51.972Z (about 1 year ago)
- Language: Go
- Homepage:
- Size: 85 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Burrow for gophers.
======
Toolkit for go net/http.
## Install
go get github.com/mkch/burrow
go install github.com/mkch/burrow
## Examples
### * Compress
package compress_test
import (
"github.com/mkch/burrow/compress"
"net/http"
)
func main() {
http.ListenAndServe(":8080", compress.NewHandler(http.DefaultServeMux, nil))
}
### * Session
package session_test
import (
"github.com/mkch/burrow/session"
"net/http"
)
var sessionManager = session.NewSessionManager()
func main() {
http.Handle("/foo", session.HTTPHandlerFunc(fooHandler))
http.ListenAndServe(":8080", sessionManager.Handler(http.DefaultServeMux))
}
func fooHandler(w http.ResponseWriter, r *http.Request, s session.Session) {
// Access session value with s.
}
### * Status Hook
package statushook_test
import (
"github.com/mkch/burrow/statushook"
"net/http"
)
func main() {
http.HandleFunc("/foo",
func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("This is foo."))
})
refined404Hook := func(code int, w http.ResponseWriter, r *http.Request) {
if code == http.StatusNotFound {
w.Write([]byte("404 Gohper is not here: "+r.URL.String()))
}
}
handler := statushook.Handler(http.DefaultServeMux, statushook.HookFunc(refined404Hook))
http.ListenAndServe("localhost:8181", handler)
// Please access http://localhost:8181/anything-except-foo in your browser
// to get the refined 404 page:
//
// 404 Gohper is not here: /anything-except-foo
}
### * Google SPDY™
package spdy_test
import (
"crypto/tls"
"github.com/mkch/burrow/spdy"
"net/http"
)
func main() {
server := &http.Server{
Addr: ":8080",
TLSConfig: &tls.Config{
NextProtos: []string{"spdy/2"},
},
TLSNextProto: map[string]func(*http.Server, *tls.Conn, http.Handler){
"spdy/2": spdy.TLSNextProtoFuncV2,
},
}
http.HandleFunc("/hello", func(w http.ResponseWriter, req *http.Request) {
w.Write([]byte("Hello, " + req.URL.String()))
if spdy.Spdy(req) {
w.Write([]byte(" from SPDY!"))
}
})
server.ListenAndServeTLS("/path/to/host.crt", "/path/to/host.key")
}