fiber/docs/examples.md
2020-01-08 18:07:21 -05:00

2.0 KiB

Examples

Multiple File Upload


package main

import "github.com/fenny/fiber"

func main() {
	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)
}

404 Handling

package main

import "github.com/fenny/fiber"

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

	app.Get("./static")
	app.Get(notFound)

	app.Listen(8080)
}

func notFound(c *fiber.Ctx) {
	c.Status(404).Send("Not Found")
}

Static Caching

package main

import "github.com/fenny/fiber"

func main() {
	app := fiber.New()
	app.Get(cacheControl)
	app.Get("./static")
	app.Listen(8080)
}

func cacheControl(c *fiber.Ctx) {
	c.Set("Cache-Control", "max-age=2592000, public")
	c.Next()
}

Enable CORS

package main

import "./fiber"

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

	app.All("/api", enableCors)
	app.Get("/api", apiHandler)

	app.Listen(8080)
}

func enableCors(c *fiber.Ctx) {
	c.Set("Access-Control-Allow-Origin", "*")
	c.Set("Access-Control-Allow-Headers", "X-Requested-With")
	c.Next()
}
func apiHandler(c *fiber.Ctx) {
	c.Send("Hi, I'm API!")
}

Returning JSON

package main

import "./fiber"

type Data struct {
	Name string
	Age  int
}

func main() {
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)
}

Caught a mistake? Edit this page on GitHub!