Skip to content

Fenny/adaptor

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

49 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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
go get -u github.com/gofiber/adaptor

Functions

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

net/http to Fiber

package main

import (
	"fmt"
	"net/http"

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

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 to net/http

package main

import (
	"net/http"

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

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) {
	c.Send("Hello World!")
}

About

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

Resources

License

Stars

0 stars

Watchers

0 watching

Forks

Packages

 
 
 

Contributors

Languages

  • Go 100.0%