diff --git a/.github/README.md b/.github/README.md index a17379f6..864062b4 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 @@ -272,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 diff --git a/.github/README_ru.md b/.github/README_ru.md index b62fb1bf..fb70effd 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 @@ -272,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_) ## 👍 Помощь проекту