adaptor

package
v3.0.0-beta.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 7, 2024 License: MIT Imports: 9 Imported by: 4

README

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/v3
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
HTTPMiddleware HTTPHandlerFunc(mw func(http.Handler) http.Handler) fiber.Handler func(http.Handler) http.Handler -> 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/v3"
)

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

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

	// http.HandlerFunc -> fiber.Handler
	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!")
}
net/http middleware to Fiber
package main

import (
	"log"
	"net/http"

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

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

	// http middleware -> fiber.Handler
	app.Use(adaptor.HTTPMiddleware(logMiddleware))

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

func logMiddleware(next http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		log.Println("log middleware")
		next.ServeHTTP(w, r)
	})
}
Fiber Handler to net/http
package main

import (
	"net/http"

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

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

  	// fiber.Handler -> 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/v3"
	"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!")
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConvertRequest

func ConvertRequest(c fiber.Ctx, forServer bool) (*http.Request, error)

ConvertRequest converts a fiber.Ctx to a http.Request. forServer should be set to true when the http.Request is going to be passed to a http.Handler.

func CopyContextToFiberContext

func CopyContextToFiberContext(context any, requestContext *fasthttp.RequestCtx)

CopyContextToFiberContext copies the values of context.Context to a fasthttp.RequestCtx.

func FiberApp

func FiberApp(app *fiber.App) http.HandlerFunc

FiberApp wraps fiber app to net/http handler func

func FiberHandler

func FiberHandler(h fiber.Handler) http.Handler

FiberHandler wraps fiber handler to net/http handler

func FiberHandlerFunc

func FiberHandlerFunc(h fiber.Handler) http.HandlerFunc

FiberHandlerFunc wraps fiber handler to net/http handler func

func HTTPHandler

func HTTPHandler(h http.Handler) fiber.Handler

HTTPHandler wraps net/http handler to fiber handler

func HTTPHandlerFunc

func HTTPHandlerFunc(h http.HandlerFunc) fiber.Handler

HTTPHandlerFunc wraps net/http handler func to fiber handler

func HTTPMiddleware

func HTTPMiddleware(mw func(http.Handler) http.Handler) fiber.Handler

HTTPMiddleware wraps net/http middleware to fiber middleware

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL