1
0
mirror of https://github.com/gofiber/fiber.git synced 2025-02-25 08:03:47 +00:00
fiber/docs/examples.md

126 lines
2.1 KiB
Markdown
Raw Normal View History

2020-01-08 16:39:59 -05:00
# Examples
2020-01-11 04:59:51 +01:00
#### Multiple File Upload
2020-01-08 16:39:59 -05:00
```go
package main
import "github.com/fenny/fiber"
func main() {
2020-01-13 06:20:53 +01:00
// lol
app := fiber.New()
app.Post("/", func(c *fiber.Ctx) {
// Parse the multipart form
if form := c.MultipartForm(); form != nil {
// => *multipart.Form
// Get all files from "documents" key
files := form.File["documents"]
// => []*multipart.FileHeader
// Loop trough files
for _, file := range files {
fmt.Println(file.Filename, file.Size, file.Header["Content-Type"][0])
// => "tutorial.pdf" 360641 "application/pdf"
// Save the files to disk
c.SaveFile(file, fmt.Sprintf("./%s", file.Filename))
}
}
})
app.Listen(8080)
2020-01-08 16:39:59 -05:00
}
```
2020-01-11 04:59:51 +01:00
#### 404 Handling
2020-01-08 16:39:59 -05:00
```go
package main
import "github.com/fenny/fiber"
func main() {
2020-01-13 06:20:53 +01:00
app := fiber.New()
2020-01-08 16:39:59 -05:00
2020-01-13 06:20:53 +01:00
app.Get("./static")
app.Get(notFound)
2020-01-08 16:39:59 -05:00
2020-01-13 06:20:53 +01:00
app.Listen(8080)
2020-01-08 16:39:59 -05:00
}
func notFound(c *fiber.Ctx) {
2020-01-13 06:20:53 +01:00
c.Status(404).Send("Not Found")
2020-01-08 16:39:59 -05:00
}
```
2020-01-11 04:59:51 +01:00
#### Static Caching
2020-01-08 16:39:59 -05:00
```go
package main
import "github.com/fenny/fiber"
func main() {
2020-01-13 06:20:53 +01:00
app := fiber.New()
app.Get(cacheControl)
app.Get("./static")
app.Listen(8080)
2020-01-08 16:39:59 -05:00
}
func cacheControl(c *fiber.Ctx) {
2020-01-13 06:20:53 +01:00
c.Set("Cache-Control", "max-age=2592000, public")
c.Next()
2020-01-08 16:39:59 -05:00
}
```
2020-01-11 04:59:51 +01:00
#### Enable CORS
2020-01-08 16:39:59 -05:00
```go
package main
import "./fiber"
func main() {
2020-01-13 06:20:53 +01:00
app := fiber.New()
2020-01-08 16:39:59 -05:00
2020-01-13 06:20:53 +01:00
app.All("/api", enableCors)
app.Get("/api", apiHandler)
2020-01-08 16:39:59 -05:00
2020-01-13 06:20:53 +01:00
app.Listen(8080)
2020-01-08 16:39:59 -05:00
}
func enableCors(c *fiber.Ctx) {
2020-01-13 06:20:53 +01:00
c.Set("Access-Control-Allow-Origin", "*")
c.Set("Access-Control-Allow-Headers", "X-Requested-With")
c.Next()
2020-01-08 16:39:59 -05:00
}
func apiHandler(c *fiber.Ctx) {
2020-01-13 06:20:53 +01:00
c.Send("Hi, I'm API!")
2020-01-08 16:39:59 -05:00
}
```
2020-01-11 04:59:51 +01:00
#### Returning JSON
2020-01-08 16:39:59 -05:00
```go
package main
import "./fiber"
type Data struct {
2020-01-13 06:20:53 +01:00
Name string
Age int
2020-01-08 16:39:59 -05:00
}
func main() {
2020-01-13 06:20:53 +01:00
app := fiber.New()
app.Get("/json", func(c *fiber.Ctx) {
data := SomeData{
Name: "John",
Age: 20,
}
c.Json(data)
// or
err := c.Json(data)
if err != nil {
c.Send("Something went wrong!")
}
})
app.Listen(8080)
2020-01-08 16:39:59 -05:00
}
```
*Caught a mistake? [Edit this page on GitHub!](https://github.com/Fenny/fiber/blob/master/docs/examples.md)*