Skip to content
master
Go to file
Code

Latest commit

Bumps [github.com/gofiber/fiber/v2](https://github.com/gofiber/fiber) from 2.0.3 to 2.1.0.
- [Release notes](https://github.com/gofiber/fiber/releases)
- [Commits](gofiber/fiber@v2.0.3...v2.1.0)

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
bd30f13

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
 
 
 
 
 
 
 
 
 
 

README.md

Adaptor

Release Discord Test Security Linter

Converter for net/http handlers to/from Fiber request handlers, special thanks to @arsmn!

Install

go get -u github.com/gofiber/fiber/v2
go get -u github.com/gofiber/adaptor/v2

Functions

Name Signature Description
HTTPHandler HTTPHandler(h http.Handler) fiber.Handler http.Handler -> Fiber handler
HTTPHandlerFunc HTTPHandlerFunc(h http.HandlerFunc) fiber.Handler http.HandlerFunc -> Fiber handler
FiberHandler FiberHandler(h fiber.Handler) http.Handler Fiber handler -> http.Handler
FiberHandlerFunc FiberHandlerFunc(h fiber.Handler) http.HandlerFunc Fiber handler -> http.HandlerFunc
FiberApp FiberApp(app *fiber.App) http.HandlerFunc Fiber app -> http.HandlerFunc

net/http to Fiber

package main

import (
	"fmt"
	"net/http"

	"github.com/gofiber/adaptor/v2"
	"github.com/gofiber/fiber/v2"
)

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

	// http.Handler -> func(*fiber.Ctx)
	app.Get("/", adaptor.HTTPHandler(handler(greet)))

	// http.HandlerFunc -> func(*fiber.Ctx)
	app.Get("/func", adaptor.HTTPHandlerFunc(greet))

	// Listen on port 3000
	app.Listen(":3000")
}

func handler(f http.HandlerFunc) http.Handler {
	return http.HandlerFunc(f)
}

func greet(w http.ResponseWriter, r *http.Request) {
	fmt.Fprint(w, "Hello World!")
}

Fiber Handler to net/http

package main

import (
	"net/http"

	"github.com/gofiber/adaptor/v2"
	"github.com/gofiber/fiber/v2"
)

func main() {
	// func(c *fiber.Ctx) -> http.Handler
	http.Handle("/", adaptor.FiberHandler(greet))

  	// func(c *fiber.Ctx) -> http.HandlerFunc
	http.HandleFunc("/func", adaptor.FiberHandlerFunc(greet))

	// Listen on port 3000
	http.ListenAndServe(":3000", nil)
}

func greet(c *fiber.Ctx) error {
	return c.SendString("Hello World!")
}

Fiber App to net/http

package main

import (
	"github.com/gofiber/adaptor/v2"
	"github.com/gofiber/fiber/v2"
	"net/http"
)
func main() {
	app := fiber.New()

	app.Get("/greet", greet)

	// Listen on port 3000
	http.ListenAndServe(":3000", adaptor.FiberApp(app))
}

func greet(c *fiber.Ctx) error {
	return c.SendString("Hello World!")
}

About

🧬 Adaptor middleware to convert net/http handlers from/to Fiber request handlers

Resources

License

Packages

No packages published

Languages

You can’t perform that action at this time.