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 1/4] 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 From aaf5a232d9e264a6444ad808b001fd495c39a152 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vic=20Sh=C3=B3stak?= Date: Sat, 22 Feb 2020 11:38:04 +0300 Subject: [PATCH 2/4] Update README_ru.md --- .github/README_ru.md | 61 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/.github/README_ru.md b/.github/README_ru.md index b62fb1bf..ead36eff 100644 --- a/.github/README_ru.md +++ b/.github/README_ru.md @@ -204,6 +204,67 @@ func main() {
📚 Показать больше примеров кода +### Работа с шаблонами + +Поддерживаемые движки шаблонов: + +- [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() { + // Вы можете настроить нужный движок для шаблонов + // перед инициализацией приложения: + app := fiber.New(&fiber.Settings{ + ViewEngine: "mustache", + ViewFolder: "./views", + ViewExtension: ".tmpl", + }) + + // ИЛИ уже после инициализации приложения, + // в любом удобном месте: + app.Settings.ViewEngine = "mustache" + app.Settings.ViewFolder = "./views" + app.Settings.ViewExtension = ".tmpl" + + // Теперь, вы сможете вызывать шаблон `./views/home.tmpl` вот так: + app.Get("/", func(c *fiber.Ctx) { + c.Render("home", fiber.Map{ + "title": "Homepage", + "year": 1999, + }) + }) + + // ... +} +``` + +### Группировка роутов в цепочки + +```go +func main() { + app := fiber.New() + + // Корневой API роут + api := app.Group("/api", cors()) // /api + + // Роуты для API v1 + v1 := api.Group("/v1", mysql()) // /api/v1 + v1.Get("/list", handler) // /api/v1/list + v1.Get("/user", handler) // /api/v1/user + + // Роуты для API v2 + v2 := api.Group("/v2", mongodb()) // /api/v2 + v2.Get("/list", handler) // /api/v2/list + v2.Get("/user", handler) // /api/v2/user + + // ... +} +``` + ### Обработка 404 ошибки ```go From bc18f101111ab58224edd1e7569a6e4bea77a5f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vic=20Sh=C3=B3stak?= Date: Sat, 22 Feb 2020 11:42:33 +0300 Subject: [PATCH 3/4] Update README.md --- .github/README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/README.md b/.github/README.md index 49df6419..864062b4 100644 --- a/.github/README.md +++ b/.github/README.md @@ -362,7 +362,8 @@ func main() { ## 💬 Media -- [Welcome to Fiber — an Express.js styled web framework written in Go with ❤️](https://dev.to/koddr/welcome-to-fiber-an-express-js-styled-fastest-web-framework-written-with-on-golang-497) _by [Vic Shóstak](https://github.com/koddr), 03 Feb 2020_ +- [Welcome to Fiber — an Express.js styled web framework written in Go with ❤️](https://dev.to/koddr/welcome-to-fiber-an-express-js-styled-fastest-web-framework-written-with-on-golang-497) (_by [Vic Shóstak](https://github.com/koddr), 03 Feb 2020_) +- [Fiber official release is out now! 🎉 What's new and is he still fast, flexible and friendly?](https://dev.to/koddr/fiber-v2-is-out-now-what-s-new-and-is-he-still-fast-flexible-and-friendly-3ipf) (_by [Vic Shóstak](https://github.com/koddr), 21 Feb 2020_) ## 👍 Contribute From a1269b0566c3aa7d5d8aa5548649f2d20efd1cde Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vic=20Sh=C3=B3stak?= Date: Sat, 22 Feb 2020 11:43:03 +0300 Subject: [PATCH 4/4] Update README_ru.md --- .github/README_ru.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/README_ru.md b/.github/README_ru.md index ead36eff..fb70effd 100644 --- a/.github/README_ru.md +++ b/.github/README_ru.md @@ -333,7 +333,8 @@ func main() { ## 💬 Медиа -- [Welcome to Fiber — an Express.js styled web framework written in Go with ❤️](https://dev.to/koddr/welcome-to-fiber-an-express-js-styled-fastest-web-framework-written-with-on-golang-497) *[Vic Shóstak](https://github.com/koddr), 3 февраля 2020 г.* +- [Welcome to Fiber — an Express.js styled web framework written in Go with ❤️](https://dev.to/koddr/welcome-to-fiber-an-express-js-styled-fastest-web-framework-written-with-on-golang-497) (_by [Vic Shóstak](https://github.com/koddr), 03 Feb 2020_) +- [Fiber official release is out now! 🎉 What's new and is he still fast, flexible and friendly?](https://dev.to/koddr/fiber-v2-is-out-now-what-s-new-and-is-he-still-fast-flexible-and-friendly-3ipf) (_by [Vic Shóstak](https://github.com/koddr), 21 Feb 2020_) ## 👍 Помощь проекту