mirror of
https://github.com/gofiber/fiber.git
synced 2025-02-23 07:43:51 +00:00
Update README.md
This commit is contained in:
parent
1aca495c3a
commit
07fdec2bb0
90
.github/README.md
vendored
90
.github/README.md
vendored
@ -204,6 +204,65 @@ func main() {
|
||||
<details>
|
||||
<summary>📚 Show more code examples</summary>
|
||||
|
||||
### 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
|
||||
|
Loading…
x
Reference in New Issue
Block a user