diff --git a/README.md b/README.md index 2e2840ed..825aff83 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,6 @@ # [Echo](http://echo.labstack.com) [![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) [![Join the chat at https://gitter.im/labstack/echo](https://img.shields.io/badge/gitter-join%20chat-brightgreen.svg?style=flat-square)](https://gitter.im/labstack/echo) -Echo is a fast HTTP router (zero dynamic memory allocation) and micro web framework in Go. + +Echo, a fast and unfancy micro web framework for Golang. ## Features @@ -75,13 +76,13 @@ BenchmarkZeus_GithubAll 2000 748827 ns/op 30068 $ go get github.com/labstack/echo ``` -##[Examples](https://github.com/labstack/echo/tree/master/examples) +## [Recipes](https://github.com/labstack/echo/tree/master/recipes) -- [Hello, World!](https://github.com/labstack/echo/tree/master/examples/hello) -- [CRUD](https://github.com/labstack/echo/tree/master/examples/crud) -- [Website](https://github.com/labstack/echo/tree/master/examples/website) -- [Middleware](https://github.com/labstack/echo/tree/master/examples/middleware) -- [Stream](https://github.com/labstack/echo/tree/master/examples/stream) +- [File Upload](http://echo.labstack.com/recipes/file-upload) +- [Streaming File Upload](http://echo.labstack.com/recipes/streaming-file-upload) +- [Streaming Response](http://echo.labstack.com/recipes/streaming-response) +- [WebSocket](http://echo.labstack.com/recipes/websocket) +- [Graceful Shutdown](http://echo.labstack.com/recipes/graceful-shutdown) ##[Guide](http://echo.labstack.com/guide) diff --git a/recipes/file-upload/public/index.html b/recipes/file-upload/public/index.html index eb56c800..82f38b49 100644 --- a/recipes/file-upload/public/index.html +++ b/recipes/file-upload/public/index.html @@ -2,11 +2,11 @@ - Multipart File Upload + File Upload

Upload Files

-
+ Name:
Email:
Files:

diff --git a/recipes/streaming-file-upload/public/index.html b/recipes/streaming-file-upload/public/index.html new file mode 100644 index 00000000..b5cfe81e --- /dev/null +++ b/recipes/streaming-file-upload/public/index.html @@ -0,0 +1,16 @@ + + + + + File Upload + + +

Upload Files

+ + Name:
+ Email:
+ Files:

+ +
+ + diff --git a/recipes/streaming-file-upload/server.go b/recipes/streaming-file-upload/server.go index 5e12ffad..9966541b 100644 --- a/recipes/streaming-file-upload/server.go +++ b/recipes/streaming-file-upload/server.go @@ -39,8 +39,6 @@ func upload(c *echo.Context) error { } email := string(b) - println(name, email) - // Read files i := 0 for { @@ -71,7 +69,7 @@ func upload(c *echo.Context) error { func main() { e := echo.New() e.SetDebug(true) - e.Index("../file-upload/public/index.html") + e.Index("public/index.html") e.Post("/upload", upload) e.Run(":1323") } diff --git a/recipes/streaming-response/server.go b/recipes/streaming-response/server.go index 3d88e37f..0dbb1808 100644 --- a/recipes/streaming-response/server.go +++ b/recipes/streaming-response/server.go @@ -29,7 +29,7 @@ var ( func main() { e := echo.New() - e.Get("/stream", func(c *echo.Context) error { + e.Get("/", func(c *echo.Context) error { c.Response().Header().Set(echo.ContentType, echo.ApplicationJSON) c.Response().WriteHeader(http.StatusOK) for _, l := range locations { diff --git a/website/docs/index.md b/website/docs/index.md index 1f2f633a..2615eeab 100644 --- a/website/docs/index.md +++ b/website/docs/index.md @@ -1,13 +1,9 @@ # Echo -Build simple and performant systems! +A fast and light web framework for Golang. --- -## Overview - -Echo is a fast HTTP router (zero dynamic memory allocation) and micro web framework in Go. - ## Features - Fast HTTP router which smartly prioritize routes. @@ -110,7 +106,7 @@ Browse to [http://localhost:1323](http://localhost:1323) and you should see Hello, World! on the page. ### Next? -- Browse [examples](https://github.com/labstack/echo/tree/master/examples) +- Browse [recipes](https://github.com/labstack/echo/tree/master/recipes) - Head over to [Guide](guide.md) ## Contribute diff --git a/website/docs/recipes/file-upload.md b/website/docs/recipes/file-upload.md index 39b146e3..762c310c 100644 --- a/website/docs/recipes/file-upload.md +++ b/website/docs/recipes/file-upload.md @@ -1,4 +1,10 @@ -## Multipart File Upload +## File Upload + +- Multipart/form-data file upload +- Multiple form fields and files + +Use `req.ParseMultipartForm(16 << 20)` for manually parsing multipart form. It gives +us option to specify maximum memory used while parsing the request body. `server.go` @@ -64,11 +70,11 @@ func main() { - Multipart File Upload + File Upload

Upload Files

-
+ Name:
Email:
Files:

@@ -79,4 +85,4 @@ func main() { ``` -## [Source Code] +## [Source Code](https://github.com/labstack/echo/blob/master/recipes/file-upload) diff --git a/website/docs/recipes/graceful-shutdown.md b/website/docs/recipes/graceful-shutdown.md index 9ac5cac2..ed0134b1 100644 --- a/website/docs/recipes/graceful-shutdown.md +++ b/website/docs/recipes/graceful-shutdown.md @@ -50,3 +50,9 @@ func main() { gracehttp.Serve(e.Server(":1323")) } ``` + +## Source Code + +[`graceful`](https://github.com/labstack/echo/blob/master/recipes/graceful-shutdown/graceful) + +[`grace`](https://github.com/labstack/echo/blob/master/recipes/graceful-shutdown/grace) diff --git a/website/docs/recipes/streaming-file-upload.md b/website/docs/recipes/streaming-file-upload.md new file mode 100644 index 00000000..143dc5e8 --- /dev/null +++ b/website/docs/recipes/streaming-file-upload.md @@ -0,0 +1,109 @@ +## Streaming File Upload + +- Streaming multipart/form-data file upload +- Multiple form fields and files + +`server.go` + +```go +package main + +import ( + "io/ioutil" + + "github.com/labstack/echo" + "io" + "os" + "net/http" +) + +func upload(c *echo.Context) error { + mr, err := c.Request().MultipartReader() + if err != nil { + return err + } + + // Read form field `name` + part, err := mr.NextPart() + if err != nil { + return err + } + defer part.Close() + b, err := ioutil.ReadAll(part) + if err != nil { + return err + } + name := string(b) + + // Read form field `email` + part, err = mr.NextPart() + if err != nil { + return err + } + defer part.Close() + b, err = ioutil.ReadAll(part) + if err != nil { + return err + } + email := string(b) + + // Read files + i := 0 + for { + part, err := mr.NextPart() + if err != nil { + if err == io.EOF { + break + } + return err + } + defer part.Close() + + file, err := os.Create(part.FileName()) + if err != nil { + return err + } + defer file.Close() + + if _, err := io.Copy(file, part); err != nil { + return err + } + i++ + } + return c.String(http.StatusOK, "Thank You! %s <%s>, %d files uploaded successfully.", + name, email, i) +} + +func main() { + e := echo.New() + e.SetDebug(true) + e.Index("../file-upload/public/index.html") + e.Post("/upload", upload) + e.Run(":1323") +} +``` + +`index.html` + +```html + + + + + File Upload + + +

Upload Files

+ + Name:
+ Email:
+ Files:

+ +
+ + + +``` + +## [Source Code](https://github.com/labstack/echo/blob/master/recipes/streaming-file-upload) + diff --git a/website/docs/recipes/streaming-response.md b/website/docs/recipes/streaming-response.md index fbc714ab..8ce12d02 100644 --- a/website/docs/recipes/streaming-response.md +++ b/website/docs/recipes/streaming-response.md @@ -1,5 +1,8 @@ ## Streaming Response +- Send data as it is produced +- Streaming JSON response with chunked transfer encoding + `server.go` ```go @@ -34,7 +37,7 @@ var ( func main() { e := echo.New() - e.Get("/stream", func(c *echo.Context) error { + e.Get("/", func(c *echo.Context) error { c.Response().Header().Set(echo.ContentType, echo.ApplicationJSON) c.Response().WriteHeader(http.StatusOK) for _, l := range locations { @@ -50,4 +53,15 @@ func main() { } ``` -## [Source Code]() +`curl localhost:1323` + +```js +{"Altitude":-97,"Latitude":37.819929,"Longitude":-122.478255} +{"Altitude":1899,"Latitude":39.096849,"Longitude":-120.032351} +{"Altitude":2619,"Latitude":37.865101,"Longitude":-119.538329} +{"Altitude":42,"Latitude":33.812092,"Longitude":-117.918974} +{"Altitude":15,"Latitude":37.77493,"Longitude":-122.419416} +``` + +## [Source Code](https://github.com/labstack/echo/blob/master/recipes/streaming-response) + diff --git a/website/docs/recipes/websocket.md b/website/docs/recipes/websocket.md index b826fed4..c3250825 100644 --- a/website/docs/recipes/websocket.md +++ b/website/docs/recipes/websocket.md @@ -23,4 +23,5 @@ func main() { } ``` -## [Source Code]() +## [Source Code](https://github.com/labstack/echo/blob/master/recipes/websocket) + diff --git a/website/echo/base.html b/website/echo/base.html index f005d8f6..75df7a29 100644 --- a/website/echo/base.html +++ b/website/echo/base.html @@ -10,7 +10,7 @@ {% if favicon %} {% else %}{% endif %} - {% if page_title %}{{ page_title }} - {% endif %}{{ site_name }} + {% if page_title %}{{ page_title }} - {% endif %}{{ config.extra.site_title }} diff --git a/website/mkdocs.yml b/website/mkdocs.yml index 05dee5d4..cd627c69 100644 --- a/website/mkdocs.yml +++ b/website/mkdocs.yml @@ -9,6 +9,9 @@ pages: - Guide: guide.md - Recipes: - File Upload: recipes/file-upload.md - - Graceful Shutdown: recipes/graceful-shutdown.md + - Streaming File Upload: recipes/streaming-file-upload.md - Streaming Response: recipes/streaming-response.md - WebSocket: recipes/websocket.md + - Graceful Shutdown: recipes/graceful-shutdown.md +extra: + site_title: Echo, a fast and unfancy micro web framework for Golang.