Skip to content

Commit

Permalink
Merge pull request #202 from Fenny/master
Browse files Browse the repository at this point in the history
v1.8.2
  • Loading branch information
Fenny committed Mar 4, 2020
2 parents c4b1270 + 7e2ee40 commit 51407bb
Show file tree
Hide file tree
Showing 20 changed files with 2,037 additions and 857 deletions.
77 changes: 49 additions & 28 deletions .github/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,12 @@ func main() {
```

### Serve static files

https://fiber.wiki/application#static
```go
func main() {
app := fiber.New()

app.Static("/public")
app.Static("/", "/public")
// => http://localhost:3000/js/script.js
// => http://localhost:3000/css/style.css

Expand All @@ -174,7 +174,8 @@ func main() {
```

### Middleware & Next

https://fiber.wiki/routing#middleware
https://fiber.wiki/context#next
```go
func main() {
app := fiber.New()
Expand Down Expand Up @@ -205,9 +206,10 @@ func main() {
<summary>📚 Show more code examples</summary>

### Template engines
https://fiber.wiki/application#settings
https://fiber.wiki/context#render

Already supports:

Supported engines:
- [html](https://golang.org/pkg/html/template/)
- [amber](https://github.com/eknkc/amber)
- [handlebars](https://github.com/aymerick/raymond)
Expand Down Expand Up @@ -241,7 +243,7 @@ func main() {
```

### Grouping routes into chains

https://fiber.wiki/application#group
```go
func main() {
app := fiber.New()
Expand All @@ -263,8 +265,8 @@ func main() {
}
```

### Built-in logger

### Middleware logger
https://fiber.wiki/middleware#logger
```go
import (
"github.com/gofiber/fiber"
Expand All @@ -288,6 +290,7 @@ func main() {
```

### Cross-Origin Resource Sharing (CORS)
https://fiber.wiki/middleware#cors

[CORS](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS) is a mechanism that uses additional HTTP headers to tell browsers to give a web application running at one origin, access to selected resources from a different origin. A web application executes a cross-origin HTTP request when it requests a resource that has a different origin (domain, protocol, or port) from its own.

Expand Down Expand Up @@ -343,35 +346,40 @@ func main() {
```

### JSON Response

https://fiber.wiki/context#json
```go
type User struct {
Name string `json:"name"`
Age int `json:"age"`
}

func main() {
app := fiber.New()

type User struct {
Name string `json:"name"`
Age int `json:"age"`
}
app.Get("/user", func(c *fiber.Ctx) {
c.JSON(&User{"John", 20})
// {"name":"John", "age":20}
})

// Serialize JSON
app.Get("/json", func(c *fiber.Ctx) {
c.JSON(&User{"John", 20})
// => {"name":"John", "age":20}
c.JSON(&fiber.Map{
"success": true,
"message": "Hi John!",
})
// {"success":true, "message":"Hi John!"}
})

app.Listen(3000)
}
```

### WebSocket support

https://fiber.wiki/application#websocket
```go
func main() {
app := fiber.New()

app.WebSocket("/ws/:name", func(c *fiber.Conn) {
log.Println(c.Params("name"))

app.WebSocket("/ws", func(c *fiber.Conn) {
for {
mt, msg, err := c.ReadMessage()
if err != nil {
Expand All @@ -389,26 +397,33 @@ func main() {
}
})

// Listen on ws://localhost:3000/ws/john
// Listen on ws://localhost:3000/ws
app.Listen(3000)
}
```

### Recover from `panic`

### Recover middleware
https://fiber.wiki/middleware#recover
```go
package main

import (
"github.com/gofiber/fiber"
"github.com/gofiber/fiber/middleware"
)

func main() {
app := fiber.New()

app.Use(middleware.Recover(func(c *fiber.Ctx, err error) {
log.Println(err) // "Something went wrong!"
c.SendStatus(500) // Internal Server Error
})))

app.Get("/", func(c *fiber.Ctx) {
panic("Something went wrong!")
})

app.Recover(func(c *fiber.Ctx) {
c.Status(500).Send(c.Error())
// => 500 "Something went wrong!"
})

app.Listen(3000)
}
```
Expand Down Expand Up @@ -437,6 +452,12 @@ If you want to say **thank you** and/or support the active development of `Fiber

<table>
<tr>
<td align="center">
<a href="https://github.com/gofiber/fiber">
<img src="https://i.stack.imgur.com/frlIf.png" width="100px"></br>
<sub><b>JustDave</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/bihe">
<img src="https://avatars1.githubusercontent.com/u/635852?s=460&v=4" width="100px"></br>
Expand Down

0 comments on commit 51407bb

Please sign in to comment.