mirror of
https://github.com/labstack/echo.git
synced 2024-12-24 20:14:31 +02:00
Example now on README
Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
parent
690af22072
commit
1402f61198
84
README.md
84
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)
|
||||
|
4
bolt.go
4
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...),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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")
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user