1
0
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:
Vishal Rana 2015-03-25 11:16:17 -07:00
parent 690af22072
commit 1402f61198
3 changed files with 116 additions and 2 deletions

View File

@ -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)

View File

@ -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")
}