Skip to content

Commit

Permalink
README updated
Browse files Browse the repository at this point in the history
Signed-off-by: Vishal Rana <[email protected]>
  • Loading branch information
vishr committed Mar 26, 2015
1 parent 1402f61 commit eb57ae1
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 26 deletions.
20 changes: 14 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,16 @@ Bolt is a fast HTTP router (zero memory allocation) + micro web framework in Go.

### Features
- Zippy router.
- Extensible middleware.
- Hook any middleware / handler.
- Extensible middleware / handler, supports:
- Middleware
- `func(bolt.HandlerFunc) bolt.HandlerFunc`
- `http.HandlerFunc`
- `http.Handler`
- `func(http.Handler) http.Handler`
- Handler
- `func(*bolt.Context)`
- `http.HandlerFunc`
- `http.Handler`
- Serve static files, including index.

### Example
Expand All @@ -27,11 +35,11 @@ type user struct {
Name string `json:"name"`
}

var users map[string]*user
var users map[string]user

func init() {
users = map[string]*user{
"1": &user{
users = map[string]user{
"1": user{
ID: "1",
Name: "Wreck-It Ralph",
},
Expand All @@ -41,7 +49,7 @@ func init() {
func createUser(c *bolt.Context) {
u := new(user)
if c.Bind(u) {
users[u.ID] = u
users[u.ID] = *u
c.JSON(http.StatusOK, u)
}
}
Expand Down
32 changes: 16 additions & 16 deletions bolt.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,29 +201,15 @@ func (b *Bolt) Run(addr string) {
log.Fatal(http.ListenAndServe(addr, b))
}

// wraps Handler
func (b *Bolt) wrapH(h Handler) HandlerFunc {
switch h := h.(type) {
case func(*Context):
return HandlerFunc(h)
case http.HandlerFunc:
return func(c *Context) {
h.ServeHTTP(c.Response, c.Request)
}
default:
panic("bolt: unknown handler")
}
}

// wraps Middleware
func wrapM(m Middleware) MiddlewareFunc {
switch m := m.(type) {
case func(HandlerFunc) HandlerFunc:
return MiddlewareFunc(m)
case func(http.ResponseWriter, *http.Request):
case http.HandlerFunc, func(http.ResponseWriter, *http.Request), http.Handler:
return func(h HandlerFunc) HandlerFunc {
return func(c *Context) {
m(c.Response, c.Request)
m.(http.Handler).ServeHTTP(c.Response, c.Request)
h(c)
}
}
Expand All @@ -238,3 +224,17 @@ func wrapM(m Middleware) MiddlewareFunc {
panic("bolt: unknown middleware")
}
}

// wraps Handler
func (b *Bolt) wrapH(h Handler) HandlerFunc {
switch h := h.(type) {
case func(*Context):
return HandlerFunc(h)
case http.HandlerFunc, func(http.ResponseWriter, *http.Request), http.Handler:
return func(c *Context) {
h.(http.Handler).ServeHTTP(c.Response, c.Request)
}
default:
panic("bolt: unknown handler")
}
}
8 changes: 4 additions & 4 deletions example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ type user struct {
Name string `json:"name"`
}

var users map[string]*user
var users map[string]user

func init() {
users = map[string]*user{
"1": &user{
users = map[string]user{
"1": user{
ID: "1",
Name: "Wreck-It Ralph",
},
Expand All @@ -28,7 +28,7 @@ func init() {
func createUser(c *bolt.Context) {
u := new(user)
if c.Bind(u) {
users[u.ID] = u
users[u.ID] = *u
c.JSON(http.StatusOK, u)
}
}
Expand Down

0 comments on commit eb57ae1

Please sign in to comment.