1
0
mirror of https://github.com/labstack/echo.git synced 2024-12-24 20:14:31 +02:00

Created a website #29

Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
Vishal Rana 2015-04-18 21:53:38 -07:00
parent f134ea3aea
commit 141b4302ae
5 changed files with 175 additions and 10 deletions

3
.gitignore vendored
View File

@ -22,3 +22,6 @@ _testmain.go
*.exe *.exe
*.test *.test
*.prof *.prof
# Website
site/

View File

@ -6,30 +6,30 @@ import (
"github.com/labstack/echo" "github.com/labstack/echo"
"github.com/labstack/gommon/color" "github.com/labstack/gommon/color"
"github.com/mattn/go-colorable"
) )
func Logger(h echo.HandlerFunc) (echo.HandlerFunc, error) { func Logger(h echo.HandlerFunc) echo.HandlerFunc {
log.SetOutput(colorable.NewColorableStdout())
return func(c *echo.Context) error { return func(c *echo.Context) error {
start := time.Now() start := time.Now()
h(c) if err := h(c); err != nil {
return err
}
end := time.Now() end := time.Now()
col := color.Green col := color.Green
m := c.Request.Method m := c.Request.Method
p := c.Request.URL.Path p := c.Request.URL.Path
s := c.Response.Status() n := c.Response.Status()
switch { switch {
case s >= 500: case n >= 500:
col = color.Red col = color.Red
case s >= 400: case n >= 400:
col = color.Yellow col = color.Yellow
case s >= 300: case n >= 300:
col = color.Cyan col = color.Cyan
} }
log.Printf("%s %s %s %s", m, p, col(s), end.Sub(start)) log.Printf("%s %s %s %s", m, p, col(n), end.Sub(start))
return nil return nil
}, nil }
} }

1
website/docs/guide.md Normal file
View File

@ -0,0 +1 @@
# Coming soon...

154
website/docs/index.md Normal file
View File

@ -0,0 +1,154 @@
# Echo [![GoDoc](http://img.shields.io/badge/go-documentation-blue.svg?style=flat-square)](http://godoc.org/github.com/labstack/echo) [![Build Status](http://img.shields.io/travis/labstack/echo.svg?style=flat-square)](https://travis-ci.org/labstack/echo) [![Coverage Status](http://img.shields.io/coveralls/labstack/echo.svg?style=flat-square)](https://coveralls.io/r/labstack/echo)
Echo is a fast HTTP router (zero memory allocation) and micro web framework in Go.
## Features
- Fast router which smartly resolves conflicting routes.
- Extensible middleware/handler, supports:
- Middleware
- `func(*echo.Context)`
- `func(*echo.Context) error`
- `func(echo.HandlerFunc) echo.HandlerFunc`
- `func(http.Handler) http.Handler`
- `http.Handler`
- `http.HandlerFunc`
- `func(http.ResponseWriter, *http.Request)`
- `func(http.ResponseWriter, *http.Request) error`
- Handler
- `func(*echo.Context)`
- `func(*echo.Context) error`
- `http.Handler`
- `http.HandlerFunc`
- `func(http.ResponseWriter, *http.Request)`
- `func(http.ResponseWriter, *http.Request) error`
- Sub routing with groups.
- Handy encoding/decoding functions.
- Serve static files, including index.
## Installation
```go get github.com/labstack/echo```
## Examples
#### Hello, World!
Create ```server.go``` with the following content:
```go
package main
import (
"net/http"
"github.com/labstack/echo"
mw "github.com/labstack/echo/middleware"
)
// Handler
func hello(c *echo.Context) {
c.String(http.StatusOK, "Hello, World!\n")
}
func main() {
e := echo.New()
// Middleware
e.Use(mw.Logger)
// Routes
e.Get("/", hello)
// Start server
e.Run(":4444")
}
```
```go run server.go``` & browse to ```http://localhost:8080```
#### CRUD - Create, read, update and delete.
- Create user
```curl -X POST -H "Content-Type: application/json" -d '{"name":"Joe"}' http://localhost:4444/users```
- Get user
```curl http://localhost:4444/users/1```
- Update user: Change the user name to Sid
```curl -X PATCH -H "Content-Type: application/json" -d '{"name":"Sid"}' http://localhost:4444/users/1```
- Delete user
```curl -X DELETE http://localhost:4444/users/1```
```go
package main
import (
"net/http"
"strconv"
"github.com/labstack/echo"
mw "github.com/labstack/echo/middleware"
)
type (
user struct {
ID int
Name string
}
)
var (
users = map[int]*user{}
seq = 1
)
//----------
// Handlers
//----------
func createUser(c *echo.Context) error {
u := &user{
ID: seq,
}
if err := c.Bind(u); err != nil {
return err
}
users[u.ID] = u
seq++
return c.JSON(http.StatusCreated, u)
}
func getUser(c *echo.Context) error {
id, _ := strconv.Atoi(c.Param("id"))
return c.JSON(http.StatusOK, users[id])
}
func updateUser(c *echo.Context) error {
u := new(user)
if err := c.Bind(u); err != nil {
return err
}
id, _ := strconv.Atoi(c.Param("id"))
users[id].Name = u.Name
return c.JSON(http.StatusOK, users[id])
}
func deleteUser(c *echo.Context) error {
id, _ := strconv.Atoi(c.Param("id"))
delete(users, id)
return c.NoContent(http.StatusNoContent)
}
func main() {
e := echo.New()
// Middleware
e.Use(mw.Logger)
// Routes
e.Post("/users", createUser)
e.Get("/users/:id", getUser)
e.Patch("/users/:id", updateUser)
e.Delete("/users/:id", deleteUser)
// Start server
e.Run(":4444")
}
```

7
website/mkdocs.yml Normal file
View File

@ -0,0 +1,7 @@
site_name: Echo
theme: flatly
repo_url: https://github.com/labstack/echo
google_analytics: ['UA-51208124-3', 'labstack.github.io/echo']