1
0
mirror of https://github.com/gofiber/fiber.git synced 2025-02-22 07:12:55 +00:00

Merge pull request #147 from Fenny/master

Deprecate old functions
This commit is contained in:
Fenny 2020-02-16 12:18:32 -05:00 committed by GitHub
commit b12bf08ac2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 1008 additions and 1078 deletions

227
.github/README.md vendored
View File

@ -113,22 +113,22 @@ Fiber is **inspired** by Express, the most popular web framework on the Internet
Listed below are some of the common examples. If you want to see more code examples, please visit our [Recipes repository](https://github.com/gofiber/recipes) or visit our [API documentation](https://fiber.wiki).
### Static files
### Serve static files
```go
func main() {
app := fiber.New()
app.Static("./public")
app.Static("/public")
// => http://localhost:3000/js/script.js
// => http://localhost:3000/css/style.css
app.Static("/prefix", "./public")
app.Static("/prefix", "/public")
// => http://localhost:3000/prefix/js/script.js
// => http://localhost:3000/prefix/css/style.css
app.Static("*", "./public/index.html")
// => http://localhost:3000/anything/returns/the/index/file
app.Static("*", "/public/index.html")
// => http://localhost:3000/any/path/shows/index/html
app.Listen(3000)
}
@ -136,139 +136,126 @@ func main() {
### Routing
<details>
<summary>📜 Show code snippet</summary>
```go
func main() {
app := fiber.New()
```go
func main() {
app := fiber.New()
// GET /john
app.Get("/:name", func(c *fiber.Ctx) {
fmt.Printf("Hello %s!", c.Params("name"))
// => Hello john!
})
// GET /john
app.Get("/:name", func(c *fiber.Ctx) {
fmt.Printf("Hello %s!", c.Params("name"))
// => Hello john!
})
// GET /john
app.Get("/:name/:age?", func(c *fiber.Ctx) {
fmt.Printf("Name: %s, Age: %s", c.Params("name"), c.Params("age"))
// => Name: john, Age:
})
// GET /john
app.Get("/:name/:age?", func(c *fiber.Ctx) {
fmt.Printf("Name: %s, Age: %s", c.Params("name"), c.Params("age"))
// => Name: john, Age:
})
// GET /api/register
app.Get("/api*", func(c *fiber.Ctx) {
fmt.Printf("/api%s", c.Params("*"))
// => /api/register
})
// GET /api/register
app.Get("/api*", func(c *fiber.Ctx) {
fmt.Printf("/api%s", c.Params("*"))
// => /api/register
})
app.Listen(3000)
}
```
app.Listen(3000)
}
```
</details>
### Middleware & Next
### Middleware
```go
func main() {
app := fiber.New()
// Match any route
app.Use(func(c *fiber.Ctx) {
fmt.Println("First middleware")
c.Next()
})
// Match all routes starting with /api
app.Use("/api", func(c *fiber.Ctx) {
fmt.Println("Second middleware")
c.Next()
})
// POST /api/register
app.Post("/api/register", func(c *fiber.Ctx) {
fmt.Println("Last middleware")
c.Send("Hello, World!")
})
app.Listen(3000)
}
```
<details>
<summary>📜 Show code snippet</summary>
<summary>📜 Show more code examples</summary>
```go
func main() {
app := fiber.New()
### Custom 404 response
// Match any post route
app.Use(func(c *fiber.Ctx) {
if c.IP() == "1.2.3.4" {
c.SendStatus(403)
return
}
c.Next()
})
```go
func main() {
app := fiber.New()
// Match all routes starting with /api
app.Use("/api", func(c *fiber.Ctx) {
c.Set("Access-Control-Allow-Origin", "*")
c.Set("Access-Control-Allow-Headers", "X-Requested-With")
c.Next()
})
app.Static("/public")
app.Get("/demo", func(c *fiber.Ctx) {
c.Send("This is a demo!")
})
app.Post("/register", func(c *fiber.Ctx) {
c.Send("Welcome!")
})
// POST /api/register
app.Post("/api/register", func(c *fiber.Ctx) {
username := c.Body("username")
password := c.Body("password")
// ..
})
// Last middleware to match anything
app.Use(func(c *fiber.Ctx) {
c.SendStatus(404) // => 404 "Not Found"
})
app.Listen(3000)
}
```
</details>
### Custom 404
<details>
<summary>📜 Show code snippet</summary>
```go
func main() {
app := fiber.New()
// Serve static files from "public" directory
app.Static("./public")
// Last middleware
app.Use(func(c *fiber.Ctx) {
c.SendStatus(404) // => 404 "Not Found"
})
app.Listen(3000)
}
```
</details>
app.Listen(3000)
}
```
### JSON Response
<details>
<summary>📜 Show code snippet</summary>
```go
func main() {
app := fiber.New()
```go
func main() {
app := fiber.New()
type User struct {
Name string `json:"name"`
Age int `json:"age"`
}
// Serialize JSON
app.Get("/json", func(c *fiber.Ctx) {
c.JSON(&User{"John", 20})
})
app.Listen(3000)
type User struct {
Name string `json:"name"`
Age int `json:"age"`
}
```
</details>
### Recover
// Serialize JSON
app.Get("/json", func(c *fiber.Ctx) {
c.JSON(&User{"John", 20})
// => {"name":"John", "age":20}
})
<details>
<summary>📜 Show code snippet</summary>
app.Listen(3000)
}
```
```go
func main() {
app := fiber.New()
app.Get("/json", func(c *fiber.Ctx) {
panic("Something went wrong!")
})
### Recover from panic
app.Recover(func(c *fiber.Ctx) {
c.Status(500).Send(c.Error())
})
```go
func main() {
app := fiber.New()
app.Listen(3000)
}
```
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)
}
```
</details>
## 💬 Media
@ -287,30 +274,32 @@ If you want to say **thank you** and/or support the active development of `Fiber
## ☕ Supporters
<a href="https://www.buymeacoffee.com/fenny" target="_blank"><img src="https://github.com/gofiber/docs/blob/master/static/buy-morning-coffee-3x.gif" alt="Buy Me A Coffee" height="100" ></a>
<a href="https://www.buymeacoffee.com/fenny" target="_blank">
<img src="https://github.com/gofiber/docs/blob/master/static/buy-morning-coffee-3x.gif" alt="Buy Me A Coffee" height="100" >
</a>
<table>
<tr>
<td align="center">
<a href="https://github.com/bihe">
<img src="https://avatars1.githubusercontent.com/u/635852?s=460&v=4" width="75"></br>
<img src="https://avatars1.githubusercontent.com/u/635852?s=460&v=4" width="100px"></br>
<sub><b>HenrikBinggl</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/koddr">
<img src="https://avatars0.githubusercontent.com/u/11155743?s=460&v=4" width="75"></br>
<img src="https://avatars0.githubusercontent.com/u/11155743?s=460&v=4" width="100px"></br>
<sub><b>koddr</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/MarvinJWendt">
<img src="https://avatars1.githubusercontent.com/u/31022056?s=460&v=4" width="75"></br>
<img src="https://avatars1.githubusercontent.com/u/31022056?s=460&v=4" width="100px"></br>
<sub><b>MarvinJWendt</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/toishy">
<img src="https://avatars1.githubusercontent.com/u/31921460?s=460&v=4" width="75"></br>
<img src="https://avatars1.githubusercontent.com/u/31921460?s=460&v=4" width="100px"></br>
<sub><b>ToishY</b></sub>
</a>
</td>

239
.github/README_de.md vendored
View File

@ -113,162 +113,151 @@ Fiber ist **inspiriert** von Expressjs, dem beliebtesten Web-Framework im Intern
Nachfolgend sind einige der gängigen Beispiele aufgeführt. Wenn du weitere Codebeispiele sehen möchten, besuche bitte unser ["Recipes Repository"](https://github.com/gofiber/recipes) oder besuche unsere [API Dokumentation](https://fiber.wiki).
### Statische Dateien
### Serve static files
```go
func main() {
app := fiber.New()
app.Static("./public")
app.Static("/public")
// => http://localhost:3000/js/script.js
// => http://localhost:3000/css/style.css
app.Static("/prefix", "./public")
app.Static("/prefix", "/public")
// => http://localhost:3000/prefix/js/script.js
// => http://localhost:3000/prefix/css/style.css
app.Static("*", "/public/index.html")
// => http://localhost:3000/any/path/shows/index/html
app.Listen(3000)
}
```
### Routing
<details>
<summary>📜 Show code snippet</summary>
```go
func main() {
app := fiber.New()
```go
func main() {
app := fiber.New()
// GET /john
app.Get("/:name", func(c *fiber.Ctx) {
fmt.Printf("Hello %s!", c.Params("name"))
// => Hello john!
})
// GET /john
app.Get("/:name", func(c *fiber.Ctx) {
fmt.Printf("Hello %s!", c.Params("name"))
// => Hello john!
})
// GET /john
app.Get("/:name/:age?", func(c *fiber.Ctx) {
fmt.Printf("Name: %s, Age: %s", c.Params("name"), c.Params("age"))
// => Name: john, Age:
})
// GET /john
app.Get("/:name/:age?", func(c *fiber.Ctx) {
fmt.Printf("Name: %s, Age: %s", c.Params("name"), c.Params("age"))
// => Name: john, Age:
})
// GET /api/register
app.Get("/api*", func(c *fiber.Ctx) {
fmt.Printf("/api%s", c.Params("*"))
// => /api/register
})
// GET /api/register
app.Get("/api*", func(c *fiber.Ctx) {
fmt.Printf("/api%s", c.Params("*"))
// => /api/register
})
app.Listen(3000)
}
```
app.Listen(3000)
}
```
</details>
### Middleware & Next
### Middleware
```go
func main() {
app := fiber.New()
// Match any route
app.Use(func(c *fiber.Ctx) {
fmt.Println("First middleware")
c.Next()
})
// Match all routes starting with /api
app.Use("/api", func(c *fiber.Ctx) {
fmt.Println("Second middleware")
c.Next()
})
// POST /api/register
app.Post("/api/register", func(c *fiber.Ctx) {
fmt.Println("Last middleware")
c.Send("Hello, World!")
})
app.Listen(3000)
}
```
<details>
<summary>📜 Show code snippet</summary>
<summary>📜 Show more code examples</summary>
```go
func main() {
app := fiber.New()
### Custom 404 response
// Match any post route
app.Use(func(c *fiber.Ctx) {
if c.IP() == "1.2.3.4" {
c.SendStatus(403)
return
}
c.Next()
})
```go
func main() {
app := fiber.New()
// Match all routes starting with /api
app.Use("/api", func(c *fiber.Ctx) {
c.Set("Access-Control-Allow-Origin", "*")
c.Set("Access-Control-Allow-Headers", "X-Requested-With")
c.Next()
})
app.Static("/public")
app.Get("/demo", func(c *fiber.Ctx) {
c.Send("This is a demo!")
})
app.Post("/register", func(c *fiber.Ctx) {
c.Send("Welcome!")
})
// POST /api/register
app.Post("/api/register", func(c *fiber.Ctx) {
username := c.Body("username")
password := c.Body("password")
// ..
})
// Last middleware to match anything
app.Use(func(c *fiber.Ctx) {
c.SendStatus(404) // => 404 "Not Found"
})
app.Listen(3000)
}
```
</details>
### 404 Handling
<details>
<summary>📜 Show code snippet</summary>
```go
func main() {
app := fiber.New()
// Serve static files from "public" directory
app.Static("./public")
// Last middleware
app.Use(func(c *fiber.Ctx) {
c.SendStatus(404) // => 404 "Not Found"
})
app.Listen(3000)
}
```
</details>
app.Listen(3000)
}
```
### JSON Response
<details>
<summary>📜 Show code snippet</summary>
```go
func main() {
app := fiber.New()
```go
func main() {
app := fiber.New()
type User struct {
Name string `json:"name"`
Age int `json:"age"`
}
// Serialize JSON
app.Get("/json", func(c *fiber.Ctx) {
c.JSON(&User{"John", 20})
})
app.Listen(3000)
type User struct {
Name string `json:"name"`
Age int `json:"age"`
}
```
// Serialize JSON
app.Get("/json", func(c *fiber.Ctx) {
c.JSON(&User{"John", 20})
// => {"name":"John", "age":20}
})
app.Listen(3000)
}
```
### Recover from panic
```go
func main() {
app := fiber.New()
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)
}
```
</details>
### Recover
<details>
<summary>📜 Show code snippet</summary>
```go
func main() {
app := fiber.New()
app.Get("/json", func(c *fiber.Ctx) {
panic("Something went wrong!")
})
app.Recover(func(c *fiber.Ctx) {
c.Status(500).Send(c.Error())
})
app.Listen(3000)
}
```
</details>
## 💬 Medien
- [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) _von [Vic Shóstak](https://github.com/koddr), 03 Feb 2020_
@ -284,30 +273,32 @@ Falls du **danke** sagen möchtest und/oder aktiv die Entwicklung von `fiber` f
## ☕ Supporters
<a href="https://www.buymeacoffee.com/fenny" target="_blank"><img src="https://github.com/gofiber/docs/blob/master/static/buy-morning-coffee-3x.gif" alt="Buy Me A Coffee" height="100" ></a>
<a href="https://www.buymeacoffee.com/fenny" target="_blank">
<img src="https://github.com/gofiber/docs/blob/master/static/buy-morning-coffee-3x.gif" alt="Buy Me A Coffee" height="100" >
</a>
<table>
<tr>
<td align="center">
<a href="https://github.com/bihe">
<img src="https://avatars1.githubusercontent.com/u/635852?s=460&v=4" width="75"></br>
<img src="https://avatars1.githubusercontent.com/u/635852?s=460&v=4" width="100px"></br>
<sub><b>HenrikBinggl</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/koddr">
<img src="https://avatars0.githubusercontent.com/u/11155743?s=460&v=4" width="75"></br>
<img src="https://avatars0.githubusercontent.com/u/11155743?s=460&v=4" width="100px"></br>
<sub><b>koddr</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/MarvinJWendt">
<img src="https://avatars1.githubusercontent.com/u/31022056?s=460&v=4" width="75"></br>
<img src="https://avatars1.githubusercontent.com/u/31022056?s=460&v=4" width="100px"></br>
<sub><b>MarvinJWendt</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/toishy">
<img src="https://avatars1.githubusercontent.com/u/31921460?s=460&v=4" width="75"></br>
<img src="https://avatars1.githubusercontent.com/u/31921460?s=460&v=4" width="100px"></br>
<sub><b>ToishY</b></sub>
</a>
</td>

263
.github/README_es.md vendored
View File

@ -113,162 +113,151 @@ Fiber está **inspirado** en Expressjs, el framework web más popular en Interne
A continuación se enumeran algunos de los ejemplos comunes. Si desea ver más ejemplos de código, visite nuestro [repositorio de Recetas](https://github.com/gofiber/recipes) o nuestra [documentación de API](https://fiber.wiki) .
### Archivos estáticos
### Serve static files
```go
func main() {
app := fiber.New()
app.Static("./public")
app.Static("/public")
// => http://localhost:3000/js/script.js
// => http://localhost:3000/css/style.css
app.Static("/prefix", "./public")
app.Static("/prefix", "/public")
// => http://localhost:3000/prefix/js/script.js
// => http://localhost:3000/prefix/css/style.css
app.Static("*", "/public/index.html")
// => http://localhost:3000/any/path/shows/index/html
app.Listen(3000)
}
```
### Enrutamiento
### Routing
```go
func main() {
app := fiber.New()
// GET /john
app.Get("/:name", func(c *fiber.Ctx) {
fmt.Printf("Hello %s!", c.Params("name"))
// => Hello john!
})
// GET /john
app.Get("/:name/:age?", func(c *fiber.Ctx) {
fmt.Printf("Name: %s, Age: %s", c.Params("name"), c.Params("age"))
// => Name: john, Age:
})
// GET /api/register
app.Get("/api*", func(c *fiber.Ctx) {
fmt.Printf("/api%s", c.Params("*"))
// => /api/register
})
app.Listen(3000)
}
```
### Middleware & Next
```go
func main() {
app := fiber.New()
// Match any route
app.Use(func(c *fiber.Ctx) {
fmt.Println("First middleware")
c.Next()
})
// Match all routes starting with /api
app.Use("/api", func(c *fiber.Ctx) {
fmt.Println("Second middleware")
c.Next()
})
// POST /api/register
app.Post("/api/register", func(c *fiber.Ctx) {
fmt.Println("Last middleware")
c.Send("Hello, World!")
})
app.Listen(3000)
}
```
<details>
<summary>📜 Show code snippet</summary>
<summary>📜 Show more code examples</summary>
```go
func main() {
app := fiber.New()
### Custom 404 response
// GET /john
app.Get("/:name", func(c *fiber.Ctx) {
fmt.Printf("Hello %s!", c.Params("name"))
// => Hello john!
})
```go
func main() {
app := fiber.New()
// GET /john
app.Get("/:name/:age?", func(c *fiber.Ctx) {
fmt.Printf("Name: %s, Age: %s", c.Params("name"), c.Params("age"))
// => Name: john, Age:
})
app.Static("/public")
app.Get("/demo", func(c *fiber.Ctx) {
c.Send("This is a demo!")
})
app.Post("/register", func(c *fiber.Ctx) {
c.Send("Welcome!")
})
// GET /api/register
app.Get("/api*", func(c *fiber.Ctx) {
fmt.Printf("/api%s", c.Params("*"))
// => /api/register
})
// Last middleware to match anything
app.Use(func(c *fiber.Ctx) {
c.SendStatus(404) // => 404 "Not Found"
})
app.Listen(3000)
app.Listen(3000)
}
```
### JSON Response
```go
func main() {
app := fiber.New()
type User struct {
Name string `json:"name"`
Age int `json:"age"`
}
```
// Serialize JSON
app.Get("/json", func(c *fiber.Ctx) {
c.JSON(&User{"John", 20})
// => {"name":"John", "age":20}
})
app.Listen(3000)
}
```
### Recover from panic
```go
func main() {
app := fiber.New()
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)
}
```
</details>
### Middleware
<details>
<summary>📜 Show code snippet</summary>
```go
func main() {
app := fiber.New()
// Match any post route
app.Use(func(c *fiber.Ctx) {
if c.IP() == "1.2.3.4" {
c.SendStatus(403)
return
}
c.Next()
})
// Match all routes starting with /api
app.Use("/api", func(c *fiber.Ctx) {
c.Set("Access-Control-Allow-Origin", "*")
c.Set("Access-Control-Allow-Headers", "X-Requested-With")
c.Next()
})
// POST /api/register
app.Post("/api/register", func(c *fiber.Ctx) {
username := c.Body("username")
password := c.Body("password")
// ..
})
app.Listen(3000)
}
```
</details>
### Manejo 404
<details>
<summary>📜 Show code snippet</summary>
```go
func main() {
app := fiber.New()
// Serve static files from "public" directory
app.Static("./public")
// Last middleware
app.Use(func(c *fiber.Ctx) {
c.SendStatus(404) // => 404 "Not Found"
})
app.Listen(3000)
}
```
</details>
### Respuesta JSON
<details>
<summary>📜 Show code snippet</summary>
```go
func main() {
app := fiber.New()
type User struct {
Name string `json:"name"`
Age int `json:"age"`
}
// Serialize JSON
app.Get("/json", func(c *fiber.Ctx) {
c.JSON(&User{"John", 20})
})
app.Listen(3000)
}
```
</details>
### Recover
<details>
<summary>📜 Show code snippet</summary>
```go
func main() {
app := fiber.New()
app.Get("/json", func(c *fiber.Ctx) {
panic("Something went wrong!")
})
app.Recover(func(c *fiber.Ctx) {
c.Status(500).Send(c.Error())
})
app.Listen(3000)
}
```
</details>
## 💬 Medios
- [Bienvenido a Fiber: un marco web con estilo Express.js escrito en Go with ❤️](https://dev.to/koddr/welcome-to-fiber-an-express-js-styled-fastest-web-framework-written-with-on-golang-497) *por [Vic Shóstak](https://github.com/koddr), 03 feb 2020*
@ -284,30 +273,32 @@ Si quiere **agradecer** y/o apoyar el desarrollo activo de la `Fiber`:
## ☕ Supporters
<a href="https://www.buymeacoffee.com/fenny" target="_blank"><img src="https://github.com/gofiber/docs/blob/master/static/buy-morning-coffee-3x.gif" alt="Buy Me A Coffee" height="100" ></a>
<a href="https://www.buymeacoffee.com/fenny" target="_blank">
<img src="https://github.com/gofiber/docs/blob/master/static/buy-morning-coffee-3x.gif" alt="Buy Me A Coffee" height="100" >
</a>
<table>
<tr>
<td align="center">
<a href="https://github.com/bihe">
<img src="https://avatars1.githubusercontent.com/u/635852?s=460&v=4" width="75"></br>
<img src="https://avatars1.githubusercontent.com/u/635852?s=460&v=4" width="100px"></br>
<sub><b>HenrikBinggl</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/koddr">
<img src="https://avatars0.githubusercontent.com/u/11155743?s=460&v=4" width="75"></br>
<img src="https://avatars0.githubusercontent.com/u/11155743?s=460&v=4" width="100px"></br>
<sub><b>koddr</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/MarvinJWendt">
<img src="https://avatars1.githubusercontent.com/u/31022056?s=460&v=4" width="75"></br>
<img src="https://avatars1.githubusercontent.com/u/31022056?s=460&v=4" width="100px"></br>
<sub><b>MarvinJWendt</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/toishy">
<img src="https://avatars1.githubusercontent.com/u/31921460?s=460&v=4" width="75"></br>
<img src="https://avatars1.githubusercontent.com/u/31921460?s=460&v=4" width="100px"></br>
<sub><b>ToishY</b></sub>
</a>
</td>

264
.github/README_ja.md vendored
View File

@ -117,162 +117,150 @@ Fiberは人気の高いWebフレームワークであるExpressjsに**インス
以下に一般的な例をいくつか示します。他のコード例をご覧になりたい場合は、 [Recipesリポジトリ](https://github.com/gofiber/recipes)または[APIドキュメント](https://fiber.wiki)にアクセスしてください。
### 静的ファイル
### Serve static files
```go
func main() {
app := fiber.New()
app.Static("./public")
app.Static("/public")
// => http://localhost:3000/js/script.js
// => http://localhost:3000/css/style.css
app.Static("/prefix", "./public")
app.Static("/prefix", "/public")
// => http://localhost:3000/prefix/js/script.js
// => http://localhost:3000/prefix/css/style.css
app.Static("*", "/public/index.html")
// => http://localhost:3000/any/path/shows/index/html
app.Listen(3000)
}
```
### ルーティング
### Routing
```go
func main() {
app := fiber.New()
// GET /john
app.Get("/:name", func(c *fiber.Ctx) {
fmt.Printf("Hello %s!", c.Params("name"))
// => Hello john!
})
// GET /john
app.Get("/:name/:age?", func(c *fiber.Ctx) {
fmt.Printf("Name: %s, Age: %s", c.Params("name"), c.Params("age"))
// => Name: john, Age:
})
// GET /api/register
app.Get("/api*", func(c *fiber.Ctx) {
fmt.Printf("/api%s", c.Params("*"))
// => /api/register
})
app.Listen(3000)
}
```
### Middleware & Next
```go
func main() {
app := fiber.New()
// Match any route
app.Use(func(c *fiber.Ctx) {
fmt.Println("First middleware")
c.Next()
})
// Match all routes starting with /api
app.Use("/api", func(c *fiber.Ctx) {
fmt.Println("Second middleware")
c.Next()
})
// POST /api/register
app.Post("/api/register", func(c *fiber.Ctx) {
fmt.Println("Last middleware")
c.Send("Hello, World!")
})
app.Listen(3000)
}
```
<details>
<summary>📜 Show code snippet</summary>
<summary>📜 Show more code examples</summary>
```go
func main() {
app := fiber.New()
### Custom 404 response
// GET /john
app.Get("/:name", func(c *fiber.Ctx) {
fmt.Printf("Hello %s!", c.Params("name"))
// => Hello john!
})
```go
func main() {
app := fiber.New()
// GET /john
app.Get("/:name/:age?", func(c *fiber.Ctx) {
fmt.Printf("Name: %s, Age: %s", c.Params("name"), c.Params("age"))
// => Name: john, Age:
})
app.Static("/public")
app.Get("/demo", func(c *fiber.Ctx) {
c.Send("This is a demo!")
})
app.Post("/register", func(c *fiber.Ctx) {
c.Send("Welcome!")
})
// GET /api/register
app.Get("/api*", func(c *fiber.Ctx) {
fmt.Printf("/api%s", c.Params("*"))
// => /api/register
})
// Last middleware to match anything
app.Use(func(c *fiber.Ctx) {
c.SendStatus(404) // => 404 "Not Found"
})
app.Listen(3000)
app.Listen(3000)
}
```
### JSON Response
```go
func main() {
app := fiber.New()
type User struct {
Name string `json:"name"`
Age int `json:"age"`
}
```
// Serialize JSON
app.Get("/json", func(c *fiber.Ctx) {
c.JSON(&User{"John", 20})
// => {"name":"John", "age":20}
})
app.Listen(3000)
}
```
### Recover from panic
```go
func main() {
app := fiber.New()
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)
}
```
</details>
### ミドルウェア
<details>
<summary>📜 Show code snippet</summary>
```go
func main() {
app := fiber.New()
// Match any post route
app.Use(func(c *fiber.Ctx) {
if c.IP() == "1.2.3.4" {
c.SendStatus(403)
return
}
c.Next()
})
// Match all routes starting with /api
app.Use("/api", func(c *fiber.Ctx) {
c.Set("Access-Control-Allow-Origin", "*")
c.Set("Access-Control-Allow-Headers", "X-Requested-With")
c.Next()
})
// POST /api/register
app.Post("/api/register", func(c *fiber.Ctx) {
username := c.Body("username")
password := c.Body("password")
// ..
})
app.Listen(3000)
}
```
</details>
### 404ハンドリング
<details>
<summary>📜 Show code snippet</summary>
```go
func main() {
app := fiber.New()
// Serve static files from "public" directory
app.Static("./public")
// Last middleware
app.Use(func(c *fiber.Ctx) {
c.SendStatus(404) // => 404 "Not Found"
})
app.Listen(3000)
}
```
</details>
### JSONレスポンス
<details>
<summary>📜 Show code snippet</summary>
```go
func main() {
app := fiber.New()
type User struct {
Name string `json:"name"`
Age int `json:"age"`
}
// Serialize JSON
app.Get("/json", func(c *fiber.Ctx) {
c.JSON(&User{"John", 20})
})
app.Listen(3000)
}
```
</details>
### Recover
<details>
<summary>📜 Show code snippet</summary>
```go
func main() {
app := fiber.New()
app.Get("/json", func(c *fiber.Ctx) {
panic("Something went wrong!")
})
app.Recover(func(c *fiber.Ctx) {
c.Status(500).Send(c.Error())
})
app.Listen(3000)
}
```
</details>
## 💬 メディア
- [ファイバーへようこそ— Go with❤で記述されたExpress.jsスタイルのWebフレームワーク](https://dev.to/koddr/welcome-to-fiber-an-express-js-styled-fastest-web-framework-written-with-on-golang-497) *[ヴィック・ショースタク](https://github.com/koddr) 、2020年2月3日*
@ -290,30 +278,32 @@ func main() {
## ☕ Supporters
<a href="https://www.buymeacoffee.com/fenny" target="_blank"><img src="https://github.com/gofiber/docs/blob/master/static/buy-morning-coffee-3x.gif" alt="Buy Me A Coffee" height="100" ></a>
<a href="https://www.buymeacoffee.com/fenny" target="_blank">
<img src="https://github.com/gofiber/docs/blob/master/static/buy-morning-coffee-3x.gif" alt="Buy Me A Coffee" height="100" >
</a>
<table>
<tr>
<td align="center">
<a href="https://github.com/bihe">
<img src="https://avatars1.githubusercontent.com/u/635852?s=460&v=4" width="75"></br>
<img src="https://avatars1.githubusercontent.com/u/635852?s=460&v=4" width="100px"></br>
<sub><b>HenrikBinggl</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/koddr">
<img src="https://avatars0.githubusercontent.com/u/11155743?s=460&v=4" width="75"></br>
<img src="https://avatars0.githubusercontent.com/u/11155743?s=460&v=4" width="100px"></br>
<sub><b>koddr</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/MarvinJWendt">
<img src="https://avatars1.githubusercontent.com/u/31022056?s=460&v=4" width="75"></br>
<img src="https://avatars1.githubusercontent.com/u/31022056?s=460&v=4" width="100px"></br>
<sub><b>MarvinJWendt</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/toishy">
<img src="https://avatars1.githubusercontent.com/u/31921460?s=460&v=4" width="75"></br>
<img src="https://avatars1.githubusercontent.com/u/31921460?s=460&v=4" width="100px"></br>
<sub><b>ToishY</b></sub>
</a>
</td>

260
.github/README_ko.md vendored
View File

@ -113,17 +113,17 @@ Fiber는 인터넷에서 가장 인기있는 웹 프레임워크인 Express에
다음은 일반적인 예제들 입니다. 더 많은 코드 예제를 보고 싶다면, [Recipes 저장소](https://github.com/gofiber/recipes) 또는 [API 문서](https://fiber.wiki)를 방문하세요.
### 정적 파일
### Serve static files
```go
func main() {
app := fiber.New()
app.Static("./public")
app.Static("/public")
// => http://localhost:3000/js/script.js
// => http://localhost:3000/css/style.css
app.Static("/prefix", "./public")
app.Static("/prefix", "/public")
// => http://localhost:3000/prefix/js/script.js
// => http://localhost:3000/prefix/css/style.css
@ -134,144 +134,130 @@ func main() {
}
```
### 라우팅
### Routing
```go
func main() {
app := fiber.New()
// GET /john
app.Get("/:name", func(c *fiber.Ctx) {
fmt.Printf("Hello %s!", c.Params("name"))
// => Hello john!
})
// GET /john
app.Get("/:name/:age?", func(c *fiber.Ctx) {
fmt.Printf("Name: %s, Age: %s", c.Params("name"), c.Params("age"))
// => Name: john, Age:
})
// GET /api/register
app.Get("/api*", func(c *fiber.Ctx) {
fmt.Printf("/api%s", c.Params("*"))
// => /api/register
})
app.Listen(3000)
}
```
### Middleware & Next
```go
func main() {
app := fiber.New()
// Match any route
app.Use(func(c *fiber.Ctx) {
fmt.Println("First middleware")
c.Next()
})
// Match all routes starting with /api
app.Use("/api", func(c *fiber.Ctx) {
fmt.Println("Second middleware")
c.Next()
})
// POST /api/register
app.Post("/api/register", func(c *fiber.Ctx) {
fmt.Println("Last middleware")
c.Send("Hello, World!")
})
app.Listen(3000)
}
```
<details>
<summary>📜 Show code snippet</summary>
<summary>📜 Show more code examples</summary>
```go
func main() {
app := fiber.New()
### Custom 404 response
// GET /john
app.Get("/:name", func(c *fiber.Ctx) {
fmt.Printf("Hello %s!", c.Params("name"))
// => Hello john!
})
```go
func main() {
app := fiber.New()
// GET /john
app.Get("/:name/:age?", func(c *fiber.Ctx) {
fmt.Printf("Name: %s, Age: %s", c.Params("name"), c.Params("age"))
// => Name: john, Age:
})
app.Static("/public")
app.Get("/demo", func(c *fiber.Ctx) {
c.Send("This is a demo!")
})
app.Post("/register", func(c *fiber.Ctx) {
c.Send("Welcome!")
})
// GET /api/register
app.Get("/api*", func(c *fiber.Ctx) {
fmt.Printf("/api%s", c.Params("*"))
// => /api/register
})
// Last middleware to match anything
app.Use(func(c *fiber.Ctx) {
c.SendStatus(404) // => 404 "Not Found"
})
app.Listen(3000)
app.Listen(3000)
}
```
### JSON Response
```go
func main() {
app := fiber.New()
type User struct {
Name string `json:"name"`
Age int `json:"age"`
}
```
// Serialize JSON
app.Get("/json", func(c *fiber.Ctx) {
c.JSON(&User{"John", 20})
// => {"name":"John", "age":20}
})
app.Listen(3000)
}
```
### Recover from panic
```go
func main() {
app := fiber.New()
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)
}
```
</details>
### 미들웨어
<details>
<summary>📜 Show code snippet</summary>
```go
func main() {
app := fiber.New()
// Match any post route
app.Use(func(c *fiber.Ctx) {
if c.IP() == "1.2.3.4" {
c.SendStatus(403)
return
}
c.Next()
})
// Match all routes starting with /api
app.Use("/api", func(c *fiber.Ctx) {
c.Set("Access-Control-Allow-Origin", "*")
c.Set("Access-Control-Allow-Headers", "X-Requested-With")
c.Next()
})
// POST /api/register
app.Post("/api/register", func(c *fiber.Ctx) {
username := c.Body("username")
password := c.Body("password")
// ..
})
app.Listen(3000)
}
```
</details>
### 404 처리
<details>
<summary>📜 Show code snippet</summary>
```go
func main() {
app := fiber.New()
// Serve static files from "public" directory
app.Static("./public")
// Last middleware
app.Use(func(c *fiber.Ctx) {
c.SendStatus(404) // => 404 "Not Found"
})
app.Listen(3000)
}
```
</details>
### JSON 응답
<details>
<summary>📜 Show code snippet</summary>
```go
func main() {
app := fiber.New()
type User struct {
Name string `json:"name"`
Age int `json:"age"`
}
// Serialize JSON
app.Get("/json", func(c *fiber.Ctx) {
c.JSON(&User{"John", 20})
})
app.Listen(3000)
}
```
</details>
### Recover
<details>
<summary>📜 Show code snippet</summary>
```go
func main() {
app := fiber.New()
app.Get("/json", func(c *fiber.Ctx) {
panic("Something went wrong!")
})
app.Recover(func(c *fiber.Ctx) {
c.Status(500).Send(c.Error())
})
app.Listen(3000)
}
```
</details>
## 💬 미디어
- [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_
@ -287,30 +273,32 @@ func main() {
## ☕ Supporters
<a href="https://www.buymeacoffee.com/fenny" target="_blank"><img src="https://github.com/gofiber/docs/blob/master/static/buy-morning-coffee-3x.gif" alt="Buy Me A Coffee" height="100" ></a>
<a href="https://www.buymeacoffee.com/fenny" target="_blank">
<img src="https://github.com/gofiber/docs/blob/master/static/buy-morning-coffee-3x.gif" alt="Buy Me A Coffee" height="100" >
</a>
<table>
<tr>
<td align="center">
<a href="https://github.com/bihe">
<img src="https://avatars1.githubusercontent.com/u/635852?s=460&v=4" width="75"></br>
<img src="https://avatars1.githubusercontent.com/u/635852?s=460&v=4" width="100px"></br>
<sub><b>HenrikBinggl</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/koddr">
<img src="https://avatars0.githubusercontent.com/u/11155743?s=460&v=4" width="75"></br>
<img src="https://avatars0.githubusercontent.com/u/11155743?s=460&v=4" width="100px"></br>
<sub><b>koddr</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/MarvinJWendt">
<img src="https://avatars1.githubusercontent.com/u/31022056?s=460&v=4" width="75"></br>
<img src="https://avatars1.githubusercontent.com/u/31022056?s=460&v=4" width="100px"></br>
<sub><b>MarvinJWendt</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/toishy">
<img src="https://avatars1.githubusercontent.com/u/31921460?s=460&v=4" width="75"></br>
<img src="https://avatars1.githubusercontent.com/u/31921460?s=460&v=4" width="100px"></br>
<sub><b>ToishY</b></sub>
</a>
</td>

263
.github/README_pt.md vendored
View File

@ -113,162 +113,151 @@ O Fiber é **inspirado** no Express, o framework web mais popular da Internet. C
Listados abaixo estão alguns exemplos comuns. Se você quiser ver mais exemplos de código, visite nosso [repositório de receitas](https://github.com/gofiber/recipes) ou a [documentação da API](https://fiber.wiki).
### Arquivos estáticos
### Serve static files
```go
func main() {
app := fiber.New()
app.Static("./public")
app.Static("/public")
// => http://localhost:3000/js/script.js
// => http://localhost:3000/css/style.css
app.Static("/prefix", "./public")
app.Static("/prefix", "/public")
// => http://localhost:3000/prefix/js/script.js
// => http://localhost:3000/prefix/css/style.css
app.Static("*", "/public/index.html")
// => http://localhost:3000/any/path/shows/index/html
app.Listen(3000)
}
```
### Roteamento
### Routing
```go
func main() {
app := fiber.New()
// GET /john
app.Get("/:name", func(c *fiber.Ctx) {
fmt.Printf("Hello %s!", c.Params("name"))
// => Hello john!
})
// GET /john
app.Get("/:name/:age?", func(c *fiber.Ctx) {
fmt.Printf("Name: %s, Age: %s", c.Params("name"), c.Params("age"))
// => Name: john, Age:
})
// GET /api/register
app.Get("/api*", func(c *fiber.Ctx) {
fmt.Printf("/api%s", c.Params("*"))
// => /api/register
})
app.Listen(3000)
}
```
### Middleware & Next
```go
func main() {
app := fiber.New()
// Match any route
app.Use(func(c *fiber.Ctx) {
fmt.Println("First middleware")
c.Next()
})
// Match all routes starting with /api
app.Use("/api", func(c *fiber.Ctx) {
fmt.Println("Second middleware")
c.Next()
})
// POST /api/register
app.Post("/api/register", func(c *fiber.Ctx) {
fmt.Println("Last middleware")
c.Send("Hello, World!")
})
app.Listen(3000)
}
```
<details>
<summary>📜 Show code snippet</summary>
<summary>📜 Show more code examples</summary>
```go
func main() {
app := fiber.New()
### Custom 404 response
// GET /john
app.Get("/:name", func(c *fiber.Ctx) {
fmt.Printf("Hello %s!", c.Params("name"))
// => Hello john!
})
```go
func main() {
app := fiber.New()
// GET /john
app.Get("/:name/:age?", func(c *fiber.Ctx) {
fmt.Printf("Name: %s, Age: %s", c.Params("name"), c.Params("age"))
// => Name: john, Age:
})
app.Static("/public")
app.Get("/demo", func(c *fiber.Ctx) {
c.Send("This is a demo!")
})
app.Post("/register", func(c *fiber.Ctx) {
c.Send("Welcome!")
})
// GET /api/register
app.Get("/api*", func(c *fiber.Ctx) {
fmt.Printf("/api%s", c.Params("*"))
// => /api/register
})
// Last middleware to match anything
app.Use(func(c *fiber.Ctx) {
c.SendStatus(404) // => 404 "Not Found"
})
app.Listen(3000)
app.Listen(3000)
}
```
### JSON Response
```go
func main() {
app := fiber.New()
type User struct {
Name string `json:"name"`
Age int `json:"age"`
}
```
// Serialize JSON
app.Get("/json", func(c *fiber.Ctx) {
c.JSON(&User{"John", 20})
// => {"name":"John", "age":20}
})
app.Listen(3000)
}
```
### Recover from panic
```go
func main() {
app := fiber.New()
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)
}
```
</details>
### Middleware
<details>
<summary>📜 Show code snippet</summary>
```go
func main() {
app := fiber.New()
// Match any post route
app.Use(func(c *fiber.Ctx) {
if c.IP() == "1.2.3.4" {
c.SendStatus(403)
return
}
c.Next()
})
// Match all routes starting with /api
app.Use("/api", func(c *fiber.Ctx) {
c.Set("Access-Control-Allow-Origin", "*")
c.Set("Access-Control-Allow-Headers", "X-Requested-With")
c.Next()
})
// POST /api/register
app.Post("/api/register", func(c *fiber.Ctx) {
username := c.Body("username")
password := c.Body("password")
// ..
})
app.Listen(3000)
}
```
</details>
### Lidando com 404
<details>
<summary>📜 Show code snippet</summary>
```go
func main() {
app := fiber.New()
// Serve static files from "public" directory
app.Static("./public")
// Last middleware
app.Use(func(c *fiber.Ctx) {
c.SendStatus(404) // => 404 "Not Found"
})
app.Listen(3000)
}
```
</details>
### Resposta em JSON
<details>
<summary>📜 Show code snippet</summary>
```go
func main() {
app := fiber.New()
type User struct {
Name string `json:"name"`
Age int `json:"age"`
}
// Serialize JSON
app.Get("/json", func(c *fiber.Ctx) {
c.JSON(&User{"John", 20})
})
app.Listen(3000)
}
```
</details>
### Recover
<details>
<summary>📜 Show code snippet</summary>
```go
func main() {
app := fiber.New()
app.Get("/json", func(c *fiber.Ctx) {
panic("Something went wrong!")
})
app.Recover(func(c *fiber.Ctx) {
c.Status(500).Send(c.Error())
})
app.Listen(3000)
}
```
</details>
## 💬 Mídia
- [Bem-vindo ao Fiber — uma estrutura da Web com estilo Express.js, escrita em Go com ❤️](https://dev.to/koddr/welcome-to-fiber-an-express-js-styled-fastest-web-framework-written-with-on-golang-497) _por [Vic Shóstak](https://github.com/koddr), 03 fev 2020_
@ -284,30 +273,32 @@ Se você quer **agradecer** e/ou apoiar o desenvolvimento ativo do `Fiber`:
## ☕ Supporters
<a href="https://www.buymeacoffee.com/fenny" target="_blank"><img src="https://github.com/gofiber/docs/blob/master/static/buy-morning-coffee-3x.gif" alt="Buy Me A Coffee" height="100" ></a>
<a href="https://www.buymeacoffee.com/fenny" target="_blank">
<img src="https://github.com/gofiber/docs/blob/master/static/buy-morning-coffee-3x.gif" alt="Buy Me A Coffee" height="100" >
</a>
<table>
<tr>
<td align="center">
<a href="https://github.com/bihe">
<img src="https://avatars1.githubusercontent.com/u/635852?s=460&v=4" width="75"></br>
<img src="https://avatars1.githubusercontent.com/u/635852?s=460&v=4" width="100px"></br>
<sub><b>HenrikBinggl</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/koddr">
<img src="https://avatars0.githubusercontent.com/u/11155743?s=460&v=4" width="75"></br>
<img src="https://avatars0.githubusercontent.com/u/11155743?s=460&v=4" width="100px"></br>
<sub><b>koddr</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/MarvinJWendt">
<img src="https://avatars1.githubusercontent.com/u/31022056?s=460&v=4" width="75"></br>
<img src="https://avatars1.githubusercontent.com/u/31022056?s=460&v=4" width="100px"></br>
<sub><b>MarvinJWendt</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/toishy">
<img src="https://avatars1.githubusercontent.com/u/31921460?s=460&v=4" width="75"></br>
<img src="https://avatars1.githubusercontent.com/u/31921460?s=460&v=4" width="100px"></br>
<sub><b>ToishY</b></sub>
</a>
</td>

256
.github/README_ru.md vendored
View File

@ -115,162 +115,150 @@ Fiber **вдохновлен** Express, самым популярным веб
Ниже перечислены некоторые из распространенных примеров. Если вы хотите увидеть больше примеров кода, пожалуйста, посетите наш [репозиторий рецептов](https://github.com/gofiber/recipes) или [документацию по API](https://fiber.wiki).
### Статические файлы
### Serve static files
```go
func main() {
app := fiber.New()
app.Static("./public")
app.Static("/public")
// => http://localhost:3000/js/script.js
// => http://localhost:3000/css/style.css
app.Static("/prefix", "./public")
app.Static("/prefix", "/public")
// => http://localhost:3000/prefix/js/script.js
// => http://localhost:3000/prefix/css/style.css
app.Static("*", "/public/index.html")
// => http://localhost:3000/any/path/shows/index/html
app.Listen(3000)
}
```
### Маршрутизация
### Routing
```go
func main() {
app := fiber.New()
// GET /john
app.Get("/:name", func(c *fiber.Ctx) {
fmt.Printf("Hello %s!", c.Params("name"))
// => Hello john!
})
// GET /john
app.Get("/:name/:age?", func(c *fiber.Ctx) {
fmt.Printf("Name: %s, Age: %s", c.Params("name"), c.Params("age"))
// => Name: john, Age:
})
// GET /api/register
app.Get("/api*", func(c *fiber.Ctx) {
fmt.Printf("/api%s", c.Params("*"))
// => /api/register
})
app.Listen(3000)
}
```
### Middleware & Next
```go
func main() {
app := fiber.New()
// Match any route
app.Use(func(c *fiber.Ctx) {
fmt.Println("First middleware")
c.Next()
})
// Match all routes starting with /api
app.Use("/api", func(c *fiber.Ctx) {
fmt.Println("Second middleware")
c.Next()
})
// POST /api/register
app.Post("/api/register", func(c *fiber.Ctx) {
fmt.Println("Last middleware")
c.Send("Hello, World!")
})
app.Listen(3000)
}
```
<details>
<summary>📜 Show code snippet</summary>
<summary>📜 Show more code examples</summary>
```go
func main() {
app := fiber.New()
### Custom 404 response
// GET /john
app.Get("/:name", func(c *fiber.Ctx) {
fmt.Printf("Hello %s!", c.Params("name"))
// => Hello john!
})
```go
func main() {
app := fiber.New()
// GET /john
app.Get("/:name/:age?", func(c *fiber.Ctx) {
fmt.Printf("Name: %s, Age: %s", c.Params("name"), c.Params("age"))
// => Name: john, Age:
})
app.Static("/public")
app.Get("/demo", func(c *fiber.Ctx) {
c.Send("This is a demo!")
})
app.Post("/register", func(c *fiber.Ctx) {
c.Send("Welcome!")
})
// GET /api/register
app.Get("/api*", func(c *fiber.Ctx) {
fmt.Printf("/api%s", c.Params("*"))
// => /api/register
})
// Last middleware to match anything
app.Use(func(c *fiber.Ctx) {
c.SendStatus(404) // => 404 "Not Found"
})
app.Listen(3000)
}
```
</details>
### Middleware
<details>
<summary>📜 Show code snippet</summary>
```go
func main() {
app := fiber.New()
// Match any post route
app.Use(func(c *fiber.Ctx) {
if c.IP() == "1.2.3.4" {
c.SendStatus(403)
return
}
c.Next()
})
// Match all routes starting with /api
app.Use("/api", func(c *fiber.Ctx) {
c.Set("Access-Control-Allow-Origin", "*")
c.Set("Access-Control-Allow-Headers", "X-Requested-With")
c.Next()
})
// POST /api/register
app.Post("/api/register", func(c *fiber.Ctx) {
username := c.Body("username")
password := c.Body("password")
// ..
})
app.Listen(3000)
}
```
</details>
### Обработка ошибки 404
<details>
<summary>📜 Show code snippet</summary>
```go
func main() {
app := fiber.New()
// Serve static files from "public" directory
app.Static("./public")
// Last middleware
app.Use(func(c *fiber.Ctx) {
c.SendStatus(404) // => 404 "Not Found"
})
app.Listen(3000)
}
```
</details>
app.Listen(3000)
}
```
### JSON Response
<details>
<summary>📜 Show code snippet</summary>
```go
func main() {
app := fiber.New()
```go
func main() {
app := fiber.New()
type User struct {
Name string `json:"name"`
Age int `json:"age"`
}
// Serialize JSON
app.Get("/json", func(c *fiber.Ctx) {
c.JSON(&User{"John", 20})
})
app.Listen(3000)
type User struct {
Name string `json:"name"`
Age int `json:"age"`
}
```
// Serialize JSON
app.Get("/json", func(c *fiber.Ctx) {
c.JSON(&User{"John", 20})
// => {"name":"John", "age":20}
})
app.Listen(3000)
}
```
### Recover from panic
```go
func main() {
app := fiber.New()
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)
}
```
</details>
### Recover
<details>
<summary>📜 Show code snippet</summary>
```go
func main() {
app := fiber.New()
app.Get("/json", func(c *fiber.Ctx) {
panic("Something went wrong!")
})
app.Recover(func(c *fiber.Ctx) {
c.Status(500).Send(c.Error())
})
app.Listen(3000)
}
```
</details>
## 💬 Медиа
- [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 г.*
@ -286,30 +274,32 @@ func main() {
## ☕ Supporters
<a href="https://www.buymeacoffee.com/fenny" target="_blank"><img src="https://github.com/gofiber/docs/blob/master/static/buy-morning-coffee-3x.gif" alt="Buy Me A Coffee" height="100" ></a>
<a href="https://www.buymeacoffee.com/fenny" target="_blank">
<img src="https://github.com/gofiber/docs/blob/master/static/buy-morning-coffee-3x.gif" alt="Buy Me A Coffee" height="100" >
</a>
<table>
<tr>
<td align="center">
<a href="https://github.com/bihe">
<img src="https://avatars1.githubusercontent.com/u/635852?s=460&v=4" width="75"></br>
<img src="https://avatars1.githubusercontent.com/u/635852?s=460&v=4" width="100px"></br>
<sub><b>HenrikBinggl</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/koddr">
<img src="https://avatars0.githubusercontent.com/u/11155743?s=460&v=4" width="75"></br>
<img src="https://avatars0.githubusercontent.com/u/11155743?s=460&v=4" width="100px"></br>
<sub><b>koddr</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/MarvinJWendt">
<img src="https://avatars1.githubusercontent.com/u/31022056?s=460&v=4" width="75"></br>
<img src="https://avatars1.githubusercontent.com/u/31022056?s=460&v=4" width="100px"></br>
<sub><b>MarvinJWendt</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/toishy">
<img src="https://avatars1.githubusercontent.com/u/31921460?s=460&v=4" width="75"></br>
<img src="https://avatars1.githubusercontent.com/u/31921460?s=460&v=4" width="100px"></br>
<sub><b>ToishY</b></sub>
</a>
</td>

View File

@ -110,159 +110,149 @@ Fiber **受** Internet上最流行的Web框架Expressjs的**启发** 。我们
下面列出了一些常见示例。如果您想查看更多代码示例,请访问我们的[Recipes存储库](https://github.com/gofiber/recipes)或访问我们的[API文档](https://fiber.wiki) 。
### 静态文件
### Serve static files
```go
func main() {
app := fiber.New()
app.Static("./public")
app.Static("/public")
// => http://localhost:3000/js/script.js
// => http://localhost:3000/css/style.css
app.Static("/prefix", "./public")
app.Static("/prefix", "/public")
// => http://localhost:3000/prefix/js/script.js
// => http://localhost:3000/prefix/css/style.css
app.Static("*", "/public/index.html")
// => http://localhost:3000/any/path/shows/index/html
app.Listen(3000)
}
```
### 路由
### Routing
```go
func main() {
app := fiber.New()
// GET /john
app.Get("/:name", func(c *fiber.Ctx) {
fmt.Printf("Hello %s!", c.Params("name"))
// => Hello john!
})
// GET /john
app.Get("/:name/:age?", func(c *fiber.Ctx) {
fmt.Printf("Name: %s, Age: %s", c.Params("name"), c.Params("age"))
// => Name: john, Age:
})
// GET /api/register
app.Get("/api*", func(c *fiber.Ctx) {
fmt.Printf("/api%s", c.Params("*"))
// => /api/register
})
app.Listen(3000)
}
```
### Middleware & Next
```go
func main() {
app := fiber.New()
// Match any route
app.Use(func(c *fiber.Ctx) {
fmt.Println("First middleware")
c.Next()
})
// Match all routes starting with /api
app.Use("/api", func(c *fiber.Ctx) {
fmt.Println("Second middleware")
c.Next()
})
// POST /api/register
app.Post("/api/register", func(c *fiber.Ctx) {
fmt.Println("Last middleware")
c.Send("Hello, World!")
})
app.Listen(3000)
}
```
<details>
<summary>📜 Show code snippet</summary>
<summary>📜 Show more code examples</summary>
```go
func main() {
app := fiber.New()
### Custom 404 response
// GET /john
app.Get("/:name", func(c *fiber.Ctx) {
fmt.Printf("Hello %s!", c.Params("name"))
// => Hello john!
})
```go
func main() {
app := fiber.New()
// GET /john
app.Get("/:name/:age?", func(c *fiber.Ctx) {
fmt.Printf("Name: %s, Age: %s", c.Params("name"), c.Params("age"))
// => Name: john, Age:
})
app.Static("/public")
app.Get("/demo", func(c *fiber.Ctx) {
c.Send("This is a demo!")
})
app.Post("/register", func(c *fiber.Ctx) {
c.Send("Welcome!")
})
// GET /api/register
app.Get("/api*", func(c *fiber.Ctx) {
fmt.Printf("/api%s", c.Params("*"))
// => /api/register
})
// Last middleware to match anything
app.Use(func(c *fiber.Ctx) {
c.SendStatus(404) // => 404 "Not Found"
})
app.Listen(3000)
app.Listen(3000)
}
```
### JSON Response
```go
func main() {
app := fiber.New()
type User struct {
Name string `json:"name"`
Age int `json:"age"`
}
```
</details>
### 中间件
// Serialize JSON
app.Get("/json", func(c *fiber.Ctx) {
c.JSON(&User{"John", 20})
// => {"name":"John", "age":20}
})
<details>
<summary>📜 Show code snippet</summary>
app.Listen(3000)
}
```
```go
func main() {
app := fiber.New()
// Match any post route
app.Use(func(c *fiber.Ctx) {
if c.IP() == "1.2.3.4" {
c.SendStatus(403)
return
}
c.Next()
})
### Recover from panic
// Match all routes starting with /api
app.Use("/api", func(c *fiber.Ctx) {
c.Set("Access-Control-Allow-Origin", "*")
c.Set("Access-Control-Allow-Headers", "X-Requested-With")
c.Next()
})
```go
func main() {
app := fiber.New()
// POST /api/register
app.Post("/api/register", func(c *fiber.Ctx) {
username := c.Body("username")
password := c.Body("password")
// ..
})
app.Get("/", func(c *fiber.Ctx) {
panic("Something went wrong!")
})
app.Listen(3000)
}
```
</details>
app.Recover(func(c *fiber.Ctx) {
c.Status(500).Send(c.Error())
// => 500 "Something went wrong!"
})
### 404处理
<details>
<summary>📜 Show code snippet</summary>
```go
func main() {
app := fiber.New()
// Serve static files from "public" directory
app.Static("./public")
// Last middleware
app.Use(func(c *fiber.Ctx) {
c.SendStatus(404) // => 404 "Not Found"
})
app.Listen(3000)
}
```
</details>
### JSON响应
<details>
<summary>📜 Show code snippet</summary>
```go
func main() {
app := fiber.New()
type User struct {
Name string `json:"name"`
Age int `json:"age"`
}
// Serialize JSON
app.Get("/json", func(c *fiber.Ctx) {
c.JSON(&User{"John", 20})
})
app.Listen(3000)
}
```
</details>
### Recover
<details>
<summary>📜 Show code snippet</summary>
```go
func main() {
app := fiber.New()
app.Get("/json", func(c *fiber.Ctx) {
panic("Something went wrong!")
})
app.Recover(func(c *fiber.Ctx) {
c.Status(500).Send(c.Error())
})
app.Listen(3000)
}
```
app.Listen(3000)
}
```
</details>
@ -281,30 +271,32 @@ func main() {
## ☕ Supporters
<a href="https://www.buymeacoffee.com/fenny" target="_blank"><img src="https://github.com/gofiber/docs/blob/master/static/buy-morning-coffee-3x.gif" alt="Buy Me A Coffee" height="100" ></a>
<a href="https://www.buymeacoffee.com/fenny" target="_blank">
<img src="https://github.com/gofiber/docs/blob/master/static/buy-morning-coffee-3x.gif" alt="Buy Me A Coffee" height="100" >
</a>
<table>
<tr>
<td align="center">
<a href="https://github.com/bihe">
<img src="https://avatars1.githubusercontent.com/u/635852?s=460&v=4" width="75"></br>
<img src="https://avatars1.githubusercontent.com/u/635852?s=460&v=4" width="100px"></br>
<sub><b>HenrikBinggl</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/koddr">
<img src="https://avatars0.githubusercontent.com/u/11155743?s=460&v=4" width="75"></br>
<img src="https://avatars0.githubusercontent.com/u/11155743?s=460&v=4" width="100px"></br>
<sub><b>koddr</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/MarvinJWendt">
<img src="https://avatars1.githubusercontent.com/u/31022056?s=460&v=4" width="75"></br>
<img src="https://avatars1.githubusercontent.com/u/31022056?s=460&v=4" width="100px"></br>
<sub><b>MarvinJWendt</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/toishy">
<img src="https://avatars1.githubusercontent.com/u/31921460?s=460&v=4" width="75"></br>
<img src="https://avatars1.githubusercontent.com/u/31921460?s=460&v=4" width="100px"></br>
<sub><b>ToishY</b></sub>
</a>
</td>

View File

@ -24,14 +24,14 @@ import (
"strings"
"time"
"github.com/valyala/fasthttp"
"github.com/valyala/fasthttp/reuseport"
fasthttp "github.com/valyala/fasthttp"
reuseport "github.com/valyala/fasthttp/reuseport"
)
const (
// Version : Fiber release
Version = "1.6.1"
// Website : Fiber documentation
// Website : Fiber website
Website = "https://fiber.wiki"
banner = "\x1b[1;32m" + ` ______ __ ______ ______ ______
/\ ___\ /\ \ /\ == \ /\ ___\ /\ == \
@ -300,7 +300,6 @@ func (grp *Group) Static(args ...string) {
prefix = filepath.Clean(prefix)
prefix = filepath.ToSlash(prefix)
}
fmt.Println(prefix, root)
grp.app.Static(prefix, root)
}

2
go.mod
View File

@ -1,6 +1,6 @@
module github.com/gofiber/fiber
go 1.11
go 1.13
require (
github.com/CloudyKit/fastprinter v0.0.0-20200109182630-33d98a066a53 // indirect

View File

@ -17,7 +17,7 @@ import (
"strings"
jsoniter "github.com/json-iterator/go"
"github.com/valyala/fasthttp"
fasthttp "github.com/valyala/fasthttp"
)
// Accepts : https://fiber.wiki/context#accepts
@ -135,7 +135,7 @@ func (ctx *Ctx) AcceptsLanguages(offers ...string) string {
return ""
}
// BaseUrl : https://fiber.wiki/context#baseurl
// BaseUrl will be removed in v2
func (ctx *Ctx) BaseUrl() string {
fmt.Println("Fiber deprecated c.BaseUrl(), this will be removed in v2: Use c.BaseURL() instead")
return ctx.BaseURL()
@ -148,6 +148,7 @@ func (ctx *Ctx) BaseURL() string {
// BasicAuth : https://fiber.wiki/context#basicauth
func (ctx *Ctx) BasicAuth() (user, pass string, ok bool) {
fmt.Println("Fiber deprecated c.BasicAuth(), this will be removed in v2 and be available as a separate middleware")
auth := ctx.Get(fasthttp.HeaderAuthorization)
if auth == "" {
return
@ -283,7 +284,7 @@ func (ctx *Ctx) Hostname() string {
return getString(ctx.Fasthttp.URI().Host())
}
// Ip is deprecated, this will be removed in v2: Use c.IP() instead
// Ip will be removed in v2
func (ctx *Ctx) Ip() string {
fmt.Println("Fiber deprecated c.Ip(), this will be removed in v2: Use c.IP() instead")
return ctx.IP()
@ -294,7 +295,7 @@ func (ctx *Ctx) IP() string {
return ctx.Fasthttp.RemoteIP().String()
}
// Ips is deprecated, this will be removed in v2: Use c.IPs() instead
// Ips will be removed in v2
func (ctx *Ctx) Ips() []string { // NOLINT
fmt.Println("Fiber deprecated c.Ips(), this will be removed in v2: Use c.IPs() instead")
return ctx.IPs()
@ -310,7 +311,7 @@ func (ctx *Ctx) IPs() []string {
}
// Is : https://fiber.wiki/context#is
func (ctx *Ctx) Is(ext string) bool {
func (ctx *Ctx) IS(ext string) bool {
if ext[0] != '.' {
ext = "." + ext
}
@ -346,7 +347,7 @@ func (ctx *Ctx) MultipartForm() (*multipart.Form, error) {
return ctx.Fasthttp.MultipartForm()
}
// OriginalUrl is deprecated, this will be removed in v2: Use c.OriginalURL() instead
// OriginalUrl will be removed in v2
func (ctx *Ctx) OriginalUrl() string {
fmt.Println("Fiber deprecated c.OriginalUrl(), this will be removed in v2: Use c.OriginalURL() instead")
return ctx.OriginalURL()
@ -429,7 +430,7 @@ func (ctx *Ctx) Subdomains(offset ...int) (subs []string) {
return subs
}
// Xhr is deprecated, this will be removed in v2: Use c.XHR() instead
// Xhr will be removed in v2
func (ctx *Ctx) Xhr() bool {
fmt.Println("Fiber deprecated c.Xhr(), this will be removed in v2: Use c.XHR() instead")
return ctx.XHR()

View File

@ -23,7 +23,7 @@ import (
"github.com/cbroglie/mustache"
"github.com/eknkc/amber"
jsoniter "github.com/json-iterator/go"
"github.com/valyala/fasthttp"
fasthttp "github.com/valyala/fasthttp"
"github.com/yosssi/ace"
)
@ -176,12 +176,10 @@ func (ctx *Ctx) Format(args ...interface{}) {
}
}
// HeadersSent : https://fiber.wiki/context#headerssent
func (ctx *Ctx) HeadersSent() {
// HeadersSent indicates if the app sent HTTP headers for the response.
// func (ctx *Ctx) HeadersSent() {}
}
// Json is deprecated, this will be removed in v2: Use c.JSON() instead
// Json will be removed in v2
func (ctx *Ctx) Json(v interface{}) error {
fmt.Println("Fiber deprecated c.Json(), this will be removed in v2: Use c.JSON() instead")
return ctx.JSON(v)
@ -200,19 +198,19 @@ func (ctx *Ctx) JSON(v interface{}) error {
return nil
}
// JsonBytes is deprecated, this will be removed in v2: Use c.JSONBytes() instead
// JsonBytes ...
func (ctx *Ctx) JsonBytes(raw []byte) {
fmt.Println("Fiber deprecated c.JsonBytes(), this will be removed in v2: Use c.JSONBytes() instead")
ctx.JSONBytes(raw)
}
// JSONBytes : https://fiber.wiki/context#jsonbytes
// JSONBytes will be removed in v2
func (ctx *Ctx) JSONBytes(raw []byte) {
fmt.Println("Fiber deprecated c.JSONBytes(), this will function be removed in v2")
ctx.Fasthttp.Response.Header.SetContentType(mimeApplicationJSON)
ctx.Fasthttp.Response.SetBodyString(getString(raw))
}
// Jsonp is deprecated, this will be removed in v2: Use c.JSONP() instead
// Jsonp will be removed in v2
func (ctx *Ctx) Jsonp(v interface{}, cb ...string) error {
fmt.Println("Fiber deprecated c.Jsonp(), this will be removed in v2: Use c.JSONP() instead")
return ctx.JSONP(v, cb...)
@ -238,14 +236,14 @@ func (ctx *Ctx) JSONP(v interface{}, cb ...string) error {
return nil
}
// JsonString is deprecated, this will be removed in v2: Use c.JSONString() instead
// JsonString ...
func (ctx *Ctx) JsonString(raw string) {
fmt.Println("Fiber deprecated c.JsonString(), this will be removed in v2: Use c.JSONString() instead")
ctx.JSONString(raw)
}
// JSONString : https://fiber.wiki/context#json
// JSONString will be removed in v2
func (ctx *Ctx) JSONString(raw string) {
fmt.Println("Fiber deprecated c.JSONString(), this function will be removed in v2")
ctx.Fasthttp.Response.Header.SetContentType(mimeApplicationJSON)
ctx.Fasthttp.Response.SetBodyString(raw)
}
@ -465,14 +463,14 @@ func (ctx *Ctx) Write(args ...interface{}) {
}
}
// Xml is deprecated, this will be removed in v2: Use c.XML() instead
// Xml ...
func (ctx *Ctx) Xml(v interface{}) error {
fmt.Println("Fiber deprecated c.Xml(), this will be removed in v2: Use c.XML() instead")
return ctx.XML(v)
}
// XML : https://fiber.wiki/context#xml
// XML will be removed in v2
func (ctx *Ctx) XML(v interface{}) error {
fmt.Println("Fiber deprecated c.XML(), this function will be removed in v2")
raw, err := xml.Marshal(v)
if err != nil {
return err

View File

@ -15,7 +15,7 @@ import (
"strings"
"sync"
"github.com/valyala/fasthttp"
fasthttp "github.com/valyala/fasthttp"
)
// Ctx is the context that contains everything

10
router_test.go Normal file
View File

@ -0,0 +1,10 @@
package fiber
import "testing"
func BenchmarkFib10(b *testing.B) {
// run the Fib function b.N times
// for n := 0; n < b.N; n++ {
// Fib(10)
// }
}

View File

@ -19,7 +19,7 @@ import (
"time"
"unsafe"
"github.com/gorilla/schema"
schema "github.com/gorilla/schema"
)
var schemaDecoder = schema.NewDecoder()