From 07fdec2bb0de638c5ed4c969e3724daed19f1ab4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vic=20Sh=C3=B3stak?= Date: Sat, 22 Feb 2020 11:37:59 +0300 Subject: [PATCH] Update README.md --- .github/README.md | 90 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) diff --git a/.github/README.md b/.github/README.md index a17379f6..49df6419 100644 --- a/.github/README.md +++ b/.github/README.md @@ -204,6 +204,65 @@ func main() {
📚 Show more code examples +### Template engines + +Already supports: + +- [html](https://golang.org/pkg/html/template/) +- [amber](https://github.com/eknkc/amber) +- [handlebars](https://github.com/aymerick/raymond) +- [mustache](https://github.com/cbroglie/mustache) +- [pug](https://github.com/Joker/jade) + +```go +func main() { + // You can setup template engine before initiation app: + app := fiber.New(&fiber.Settings{ + ViewEngine: "mustache", + ViewFolder: "./views", + ViewExtension: ".tmpl", + }) + + // OR after initiation app at any convenient location: + app.Settings.ViewEngine = "mustache" + app.Settings.ViewFolder = "./views" + app.Settings.ViewExtension = ".tmpl" + + // And now, you can call template `./views/home.tmpl` like this: + app.Get("/", func(c *fiber.Ctx) { + c.Render("home", fiber.Map{ + "title": "Homepage", + "year": 1999, + }) + }) + + // ... +} +``` + +### Grouping routes into chains + +```go +func main() { + app := fiber.New() + + // Root API route + api := app.Group("/api", cors()) // /api + + // API v1 routes + v1 := api.Group("/v1", mysql()) // /api/v1 + v1.Get("/list", handler) // /api/v1/list + v1.Get("/user", handler) // /api/v1/user + + // API v2 routes + v2 := api.Group("/v2", mongodb()) // /api/v2 + v2.Get("/list", handler) // /api/v2/list + v2.Get("/user", handler) // /api/v2/user + + // ... +} +``` + ### Custom 404 response ```go @@ -250,6 +309,37 @@ func main() { } ``` +### WebSocket support + +```go +func main() { + app := fiber.New() + + app.WebSocket("/ws/:name", func(c *fiber.Conn) { + log.Println(c.Params("name")) + + for { + mt, msg, err := c.ReadMessage() + if err != nil { + log.Println("read:", err) + break + } + + log.Printf("recovery: %s", msg) + + err = c.WriteMessage(mt, msg) + if err != nil { + log.Println("write:", err) + break + } + } + }) + + // Listen on ws://localhost:3000/ws/john + app.Listen(3000) +} +``` + ### Recover from `panic` ```go