adaptor

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 23, 2020 License: MIT Imports: 7 Imported by: 1

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
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!")
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FiberHandler

func FiberHandler(h func(*fiber.Ctx)) http.Handler

FiberHandler wraps fiber handler to net/http handler

func FiberHandlerFunc

func FiberHandlerFunc(h func(*fiber.Ctx)) http.HandlerFunc

FiberHandlerFunc wraps fiber handler to net/http handler func

func HTTPHandler

func HTTPHandler(h http.Handler) func(*fiber.Ctx)

HTTPHandler wraps net/http handler to fiber handler

func HTTPHandlerFunc

func HTTPHandlerFunc(h http.HandlerFunc) func(*fiber.Ctx)

HTTPHandlerFunc wraps net/http handler func to fiber handler

Types

This section is empty.

Jump to

Keyboard shortcuts

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