fiber

package
v1.4.2 Latest Latest
Warning

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

Go to latest
Published: Feb 16, 2024 License: BSD-3-Clause Imports: 10 Imported by: 0

README

Fiber (webserver)

Setting up the preconfigured webserver:


app := fiber.New(...)

// register your routes here

fiber.UseFiber(...) // starts the webserver

It will integrate logging, metrics and basic error handling into your webserver app.

Error handling

app.Get("/path", func(ctx *fiber.Ctx) error {
	// whenever you return a `fiber.Error`:
	// - the status code will be set automatically
	// - the error will be logged
	// - and the error string wil be returned in the response body
	return fiber.NewFiberError(errors.New("your error"), fiber.StatusBadRequest)
})

Middleware usage

If you want to add middleware to your fiber app, you can do this like in the following:

func newMiddlewareMap() *MiddlewareHandlerMap {
	return &MiddlewareHandlerMap{
		// fiber supports middleware restriction to paths. Use an empty string for global middleware.
		// all middleware will be initialized in the same order that it is defined.
		"/path": []fiber.Handler{
			func(ctx *fiber.Ctx) error {
				// your middleware code here
				return ctx.Next()
			},
		},
	}
}
Predefined middleware
  • middleware.NoCors() middleware that disables CORS for development environments
Used environment variables
variable name default use
HTTP_REQUEST_BODY_LIMIT 4 body size limit for all http requests
CORS_ALLOW_ORIGINS list of allowed origins, example: https://becklyn.com, https://www.becklyn.com
CORS_ALLOW_HEADERS list of allowed headers, example: Content-Type, Accept
CORS_EXPOSE_HEADERS list of exposable headers, example: Content-Type, Accept
CORS_ALLOW_CREDENTIALS bool that specifies if credentials are allowed

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewFiber

func NewFiber(options *NewFiberOptions) *fiber.App

func UseFiber

func UseFiber(options *UseFiberOptions)

Types

type Error

type Error struct {
	// contains filtered or unexported fields
}

func NewError

func NewError(err error, statusCode int) *Error

func (*Error) Error

func (e *Error) Error() string

func (*Error) StatusCode

func (e *Error) StatusCode() int

type MiddlewareHandlerMap

type MiddlewareHandlerMap map[string][]fiber.Handler

func NewEmptyMiddlewareHandlerMap

func NewEmptyMiddlewareHandlerMap() *MiddlewareHandlerMap

type NewFiberOptions

type NewFiberOptions struct {
	Logger     golog.Logger
	Middleware *MiddlewareHandlerMap
}

type UseFiberOptions

type UseFiberOptions struct {
	Lifecycle *app.Lifecycle
	Fiber     *fiber.App
	Logger    golog.Logger
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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