diff --git a/README.md b/README.md index 9b1133af..f34e10e0 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,90 @@ Bolt is a fast HTTP router (zero memory allocation) + micro web framework in Go. ### Example https://github.com/labstack/bolt/tree/master/example +```go +package main + +import ( + "net/http" + + "github.com/labstack/bolt" + mw "github.com/labstack/bolt/middleware" + "github.com/rs/cors" + "github.com/thoas/stats" +) + +type user struct { + ID string `json:"id"` + Name string `json:"name"` +} + +var users map[string]*user + +func init() { + users = map[string]*user{ + "1": &user{ + ID: "1", + Name: "Wreck-It Ralph", + }, + } +} + +func createUser(c *bolt.Context) { + u := new(user) + if c.Bind(u) { + users[u.ID] = u + c.JSON(http.StatusOK, u) + } +} + +func getUsers(c *bolt.Context) { + c.JSON(http.StatusOK, users) +} + +func getUser(c *bolt.Context) { + c.JSON(http.StatusOK, users[c.P(0)]) +} + +func main() { + b := bolt.New() + + //*************************// + // Built-in middleware // + //*************************// + b.Use(mw.Logger) + + //****************************// + // Third-party middleware // + //****************************// + // https://github.com/rs/cors + b.Use(cors.Default().Handler) + + // https://github.com/thoas/stats + s := stats.New() + b.Use(s.Handler) + // Route + b.Get("/stats", func(c *bolt.Context) { + c.JSON(200, s.Data()) + }) + + // Serve index file + b.Index("public/index.html") + + // Serve static files + b.Static("/js", "public/js") + + //************// + // Routes // + //************// + b.Post("/users", createUser) + b.Get("/users", getUsers) + b.Get("/users/:id", getUser) + + // Start server + b.Run(":8080") +} +``` + ### Benchmark Based on [julienschmidt/go-http-routing-benchmark] (https://github.com/vishr/go-http-routing-benchmark) ##### [GitHub API](http://developer.github.com/v3) diff --git a/bolt.go b/bolt.go index 0c88f592..848101b3 100644 --- a/bolt.go +++ b/bolt.go @@ -67,8 +67,8 @@ func (h HandlerFunc) ServeHTTP(r http.ResponseWriter, w *http.Request) { func (b *Bolt) Sub(prefix string, m ...MiddlewareFunc) *Bolt { return &Bolt{ - // prefix: b.prefix + prefix, - // middleware: append(b.handlers, handlers...), + // prefix: b.prefix + prefix, + // middleware: append(b.handlers, handlers...), } } diff --git a/example/main.go b/example/main.go index aaddad0c..0c0d0511 100644 --- a/example/main.go +++ b/example/main.go @@ -5,6 +5,8 @@ import ( "github.com/labstack/bolt" mw "github.com/labstack/bolt/middleware" + "github.com/rs/cors" + "github.com/thoas/stats" ) type user struct { @@ -41,11 +43,39 @@ func getUser(c *bolt.Context) { func main() { b := bolt.New() + + //*************************// + // Built-in middleware // + //*************************// b.Use(mw.Logger) + + //****************************// + // Third-party middleware // + //****************************// + // https://github.com/rs/cors + b.Use(cors.Default().Handler) + + // https://github.com/thoas/stats + s := stats.New() + b.Use(s.Handler) + // Route + b.Get("/stats", func(c *bolt.Context) { + c.JSON(200, s.Data()) + }) + + // Serve index file b.Index("public/index.html") + + // Serve static files b.Static("/js", "public/js") + + //************// + // Routes // + //************// b.Post("/users", createUser) b.Get("/users", getUsers) b.Get("/users/:id", getUser) + + // Start server b.Run(":8080") }