1
0
mirror of https://github.com/gofiber/fiber.git synced 2025-02-16 02:52:45 +00:00

improve mw

This commit is contained in:
hi019 2020-11-21 12:36:16 -05:00
parent 52ec20c3b4
commit bc7b240158
11 changed files with 197 additions and 99 deletions

View File

@ -1,8 +1,10 @@
# Basic Authentication
# Basic Authentication Middleware
Basic Authentication middleware for [Fiber](https://github.com/gofiber/fiber) that provides an HTTP basic authentication. It calls the next handler for valid credentials and [401 Unauthorized](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/401) or a custom response for missing or invalid credentials.
## Table of Contents
- [Basic Authentication](#basic-authentication)
- [Basic Authentication Middleware](#basic-authentication-middleware)
- [Table of Contents](#table-of-contents)
- [Signatures](#signatures)
- [Examples](#examples)
@ -10,13 +12,14 @@ Basic Authentication middleware for [Fiber](https://github.com/gofiber/fiber) th
- [Config](#config)
- [Default Config](#default-config)
## Signatures
```go
func New(config Config) fiber.Handler
```
## Examples
First import the middleware from Fiber,
```go
@ -64,6 +67,7 @@ app.Use(basicauth.New(basicauth.Config{
```
## Config
```go
// Config defines the config for middleware.
type Config struct {
@ -112,6 +116,7 @@ type Config struct {
```
## Default Config
```go
var ConfigDefault = Config{
Next: nil,

View File

@ -1,8 +1,10 @@
# Cache
Cache middleware for [Fiber](https://github.com/gofiber/fiber) designed to intercept responses and cache them. This middleware will cache the `Body`, `Content-Type` and `StatusCode` using the `c.Path()` (or a string returned by the Key function) as unique identifier. Special thanks to [@codemicro](https://github.com/codemicro/fiber-cache) for creating this middleware for Fiber core!
# Cache Middleware
Cache middleware for [Fiber](https://github.com/gofiber/fiber) designed to intercept responses and cache them. This middleware will cache the `Body`, `Content-Type` and `StatusCode` using the `c.Path()` (or a string returned by the Key function) as unique identifier. Special thanks to [@codemicro](https://github.com/codemicro/fiber-cache) for creating this middleware for Fiber core!
## Table of Contents
- [Cache](#cache)
- [Cache Middleware](#cache-middleware)
- [Table of Contents](#table-of-contents)
- [Signatures](#signatures)
- [Examples](#examples)
@ -11,13 +13,14 @@ Cache middleware for [Fiber](https://github.com/gofiber/fiber) designed to inter
- [Config](#config)
- [Default Config](#default-config-1)
## Signatures
```go
func New(config ...Config) fiber.Handler
```
## Examples
First import the middleware from Fiber,
```go
@ -30,6 +33,7 @@ import (
Then create a Fiber app with `app := fiber.New()`.
### Default Config
```go
app.Use(cache.New())
```
@ -47,6 +51,7 @@ app.Use(cache.New(cache.Config{
```
### Config
```go
// Config defines the config for middleware.
type Config struct {
@ -80,6 +85,7 @@ type Config struct {
```
### Default Config
```go
// ConfigDefault is the default config
var ConfigDefault = Config{

View File

@ -1,7 +1,8 @@
# Compress
# Compress Middleware
Compression middleware for [Fiber](https://github.com/gofiber/fiber) that will compress the response using `gzip`, `deflate` and `brotli` compression depending on the [Accept-Encoding](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Encoding) header.
- [Compress](#compress)
- [Compress Middleware](#compress-middleware)
- [Signatures](#signatures)
- [Examples](#examples)
- [Default Config](#default-config)
@ -10,13 +11,14 @@ Compression middleware for [Fiber](https://github.com/gofiber/fiber) that will c
- [Default Config](#default-config-1)
- [Constants](#constants)
## Signatures
```go
func New(config ...Config) fiber.Handler
```
## Examples
First import the middleware from Fiber,
```go
@ -29,6 +31,7 @@ import (
Then create a Fiber app with `app := fiber.New()`.
### Default Config
```go
app.Use(compress.New())
```
@ -51,6 +54,7 @@ app.Use(compress.New(compress.Config{
```
## Config
```go
// Config defines the config for middleware.
type Config struct {
@ -71,6 +75,7 @@ type Config struct {
```
## Default Config
```go
var ConfigDefault = Config{
Next: nil,
@ -79,6 +84,7 @@ var ConfigDefault = Config{
```
## Constants
```go
// Compression levels
const (

View File

@ -1,13 +1,17 @@
# Cross-Origin Resource Sharing (CORS)
# Cross-Origin Resource Sharing (CORS) Middleware
CORS middleware for [Fiber](https://github.com/gofiber/fiber) that that can be used to enable [Cross-Origin Resource Sharing](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS) with various options.
## Table of Contents
- [Signatures](#signatures)
- [Examples](#examples)
- [Config](#config)
- [Default Config](#default-config)
- [Cross-Origin Resource Sharing (CORS) Middleware](#cross-origin-resource-sharing-cors-middleware)
- [Table of Contents](#table-of-contents)
- [Signatures](#signatures)
- [Examples](#examples)
- [Default Config](#default-config)
- [Custom Config](#custom-config)
- [Config](#config)
- [Default Config](#default-config-1)
## Signatures

View File

@ -1,4 +1,4 @@
# CSRF
# CSRF Middleware
CSRF middleware for [Fiber](https://github.com/gofiber/fiber) that provides [Cross-site request forgery](https://en.wikipedia.org/wiki/Cross-site_request_forgery) protection by passing a csrf token via cookies. This cookie value will be used to compare against the client csrf token in POST requests. When the csrf token is invalid, this middleware will delete the `_csrf` cookie and return the `fiber.ErrForbidden` error.
CSRF Tokens are generated on GET requests.
@ -7,10 +7,15 @@ _NOTE: This middleware uses our [Storage](https://github.com/gofiber/storage) pa
## Table of Contents
- [Signatures](#signatures)
- [Examples](#examples)
- [Config](#config)
- [Default Config](#default-config)
- [CSRF Middleware](#csrf-middleware)
- [Table of Contents](#table-of-contents)
- [Signatures](#signatures)
- [Examples](#examples)
- [Default Config](#default-config)
- [Custom Config](#custom-config)
- [Custom Storage/Database](#custom-storagedatabase)
- [Config](#config)
- [Default Config](#default-config-1)
## Signatures

View File

@ -1,20 +1,28 @@
# ETag
# ETag Middleware
ETag middleware for [Fiber](https://github.com/gofiber/fiber) that lets caches be more efficient and save bandwidth, as a web server does not need to resend a full response if the content has not changed.
### Table of Contents
- [Signatures](#signatures)
- [Examples](#examples)
- [Config](#config)
- [Default Config](#default-config)
## Table of Contents
- [ETag Middleware](#etag-middleware)
- [Table of Contents](#table-of-contents)
- [Signatures](#signatures)
- [Examples](#examples)
- [Default Config](#default-config)
- [Default Config](#default-config-1)
- [Config](#config)
- [Default Config](#default-config-2)
## Signatures
### Signatures
```go
func New(config ...Config) fiber.Handler
```
### Examples
Import the middleware package that is part of the Fiber web framework
## Examples
First import the middleware from Fiber,
```go
import (
"github.com/gofiber/fiber/v2"
@ -22,9 +30,11 @@ import (
)
```
After you initiate your Fiber app, you can use the following possibilities:
Then create a Fiber app with `app := fiber.New()`.
### Default Config
```go
// Default middleware config
app.Use(etag.New())
// Get / receives Etag: "13-1831710635" in response header
@ -33,7 +43,19 @@ app.Get("/", func(c *fiber.Ctx) error {
})
```
### Config
### Default Config
```go
app.Use(etag.New())
// Get / receives Etag: "13-1831710635" in response header
app.Get("/", func(c *fiber.Ctx) error {
return c.SendString("Hello, World!")
})
```
## Config
```go
// Config defines the config for middleware.
type Config struct {
@ -54,7 +76,8 @@ type Config struct {
}
```
### Default Config
## Default Config
```go
var ConfigDefault = Config{
Weak: false,

View File

@ -1,15 +1,19 @@
# Expvar
# Expvar Middleware
Expvar middleware for [Fiber](https://github.com/gofiber/fiber) that serves via its HTTP server runtime exposed variants in the JSON format. The package is typically only imported for the side effect of registering its HTTP handlers. The handled path is `/debug/vars`.
- [Signatures](#signatures)
- [Examples](#example)
- [Expvar Middleware](#expvar-middleware)
- [Signatures](#signatures)
- [Example](#example)
## Signatures
### Signatures
```go
func New() fiber.Handler
```
### Example
## Example
Import the expvar package that is part of the Fiber web framework
```go

View File

@ -1,41 +1,52 @@
# Favicon Authentication
# Favicon Middleware
Favicon middleware for [Fiber](https://github.com/gofiber/fiber) that ignores favicon requests or caches a provided icon in memory to improve performance by skipping disk access. User agents request favicon.ico frequently and indiscriminately, so you may wish to exclude these requests from your logs by using this middleware before your logger middleware.
**Note** This middleware is exclusively for serving the default, implicit favicon, which is GET /favicon.ico.
### Table of Contents
- [Signatures](#signatures)
- [Examples](#examples)
- [Config](#config)
- [Default Config](#default-config)
## Table of Contents
- [Favicon Middleware](#favicon-middleware)
- [Table of Contents](#table-of-contents)
- [Signatures](#signatures)
- [Examples](#examples)
- [Default Config](#default-config)
- [Custom Config](#custom-config)
- [Config](#config)
- [Default Config](#default-config-1)
## Signatures
### Signatures
```go
func New(config ...Config) fiber.Handler
```
### Examples
Import the middleware package that is part of the Fiber web framework
## Examples
First import the middleware from Fiber,
```go
import (
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/favicon"
"github.com/gofiber/fiber/v2/middleware/cors"
)
```
After you initiate your Fiber app, you can use the following possibilities:
```go
// Provide a minimal config
app.Use(favicon.New())
Then create a Fiber app with `app := fiber.New()`.
// Or extend your config for customization
### Default Config
```go
app.Use(favicon.New())
```
### Custom Config
```go
app.Use(favicon.New(favicon.Config{
File: "./favicon.ico",
}))
```
### Config
```go
// Config defines the config for middleware.
type Config struct {
@ -52,6 +63,7 @@ type Config struct {
```
### Default Config
```go
var ConfigDefault = Config{
Next: nil,

View File

@ -1,30 +1,45 @@
# Filesystem middleware
Filesystem middleware for [Fiber](https://github.com/gofiber/fiber) that enables you to serve files from a directory.
# Filesystem Middleware
Filesystem middleware for [Fiber](https://github.com/gofiber/fiber) that enables you to serve files from a directory.
⚠️ **`:params` & `:optionals?` within the prefix path are not supported!**
### Table of Contents
- [Signatures](#signatures)
- [Examples](#examples)
- [Config](#config)
- [Default Config](#default-config)
## Table of Contents
- [Filesystem Middleware](#filesystem-middleware)
- [Table of Contents](#table-of-contents)
- [Signatures](#signatures)
- [Examples](#examples)
- [Config](#config)
- [pkger](#pkger)
- [packr](#packr)
- [go.rice](#gorice)
- [fileb0x](#fileb0x)
- [statik](#statik)
- [Config](#config-1)
- [Default Config](#default-config)
### Signatures
## Signatures
```go
func New(config Config) fiber.Handler
```
### Examples
Import the middleware package that is part of the Fiber web framework
## Examples
First import the middleware from Fiber,
```go
import (
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/filesystem"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/filesystem"
)
```
After you initiate your Fiber app, you can use the following possibilities:
Then create a Fiber app with `app := fiber.New()`.
### Config
```go
// Provide a minimal config
app.Use(filesystem.New(filesystem.Config{
@ -41,8 +56,9 @@ app.Use(filesystem.New(filesystem.Config{
}))
```
## pkger
https://github.com/markbates/pkger
### pkger
[Pkger](https://github.com/markbates/pkger) can be used to embed files in a Golang excecutable.
```go
package main
@ -65,8 +81,9 @@ func main() {
}
```
## packr
https://github.com/gobuffalo/packr
### packr
[Packr](https://github.com/gobuffalo/packr) can be used to embed files in a Golang excecutable.
```go
package main
@ -89,7 +106,8 @@ func main() {
}
```
## go.rice
### go.rice
https://github.com/GeertJohan/go.rice
```go
@ -113,8 +131,9 @@ func main() {
}
```
## fileb0x
https://github.com/UnnoTed/fileb0x
### fileb0x
[Fileb0x](https://github.com/UnnoTed/fileb0x) can be used to embed files in a Golang excecutable.
```go
package main
@ -137,8 +156,9 @@ func main() {
}
```
## statik
https://github.com/rakyll/statik
### statik
[Statik](https://github.com/rakyll/statik) can be used to embed files in a Golang excecutable.
```go
package main
@ -167,7 +187,8 @@ func main() {
}
```
### Config
## Config
```go
// Config defines the config for middleware.
type Config struct {
@ -206,6 +227,7 @@ type Config struct {
```
### Default Config
```go
var ConfigDefault = Config{
Next: nil,
@ -214,4 +236,4 @@ var ConfigDefault = Config{
Index: "/index.html",
MaxAge: 0,
}
```
```

View File

@ -1,6 +1,6 @@
# Limiter
# Limiter Middleware
Limiter middleware for [Fiber](https://github.com/gofiber/fiber) used to limit repeated requests to public APIs and/or endpoints such as password reset etc. Also useful for API clients, web crawling, or other tasks that need to be throttled.
Limiter middleware for [Fiber](https://github.com/gofiber/fiber) that is used to limit repeat requests to public APIs and/or endpoints such as password reset. It is also useful for API clients, web crawling, or other tasks that need to be throttled.
_NOTE: This middleware uses our [Storage](https://github.com/gofiber/storage) package to support various databases through a single interface. The default configuration for this middleware saves data to memory, see the examples below for other databases._
@ -8,10 +8,15 @@ _NOTE: This middleware uses our [Storage](https://github.com/gofiber/storage) pa
## Table of Contents
- [Signatures](#signatures)
- [Examples](#examples)
- [Config](#config)
- [Default Config](#default-config)
- [Limiter Middleware](#limiter-middleware)
- [Table of Contents](#table-of-contents)
- [Signatures](#signatures)
- [Examples](#examples)
- [Default Config](#default-config)
- [Custom Config](#custom-config)
- [Custom Storage/Database](#custom-storagedatabase)
- [Config](#config)
- [Default Config](#default-config-1)
## Signatures

View File

@ -1,14 +1,20 @@
# Logger
# Logger Middleware
Logger middleware for [Fiber](https://github.com/gofiber/fiber) that logs HTTP request/response details.
### Table of Contents
- [Signatures](#signatures)
- [Examples](#examples)
- [Config](#config)
- [Default Config](#default-config)
- [Constants](#constants)
## Table of Contents
- [Logger Middleware](#logger-middleware)
- [Table of Contents](#table-of-contents)
- [Signatures](#signatures)
- [Examples](#examples)
- [Default Config](#default-config)
- [Logging Request ID](#logging-request-id)
- [Changing TimeZone & TimeFormat](#changing-timezone--timeformat)
- [Custom File Writer](#custom-file-writer)
- [Config](#config)
- [Default Config](#default-config-1)
- [Constants](#constants)
### Signatures
## Signatures
```go
func New(config ...Config) fiber.Handler
```
@ -22,13 +28,13 @@ import (
)
```
#### **Initialization / Default Config**
### Default Config
```go
// Default middleware config
app.Use(logger.New())
```
#### **Logging Request ID**
### Logging Request ID
```go
app.Use(requestid.New())
@ -38,7 +44,7 @@ app.Use(requestid.New())
}))
```
#### **Changing TimeZone & TimeFormat**
### Changing TimeZone & TimeFormat
```go
app.Use(logger.New(logger.Config{
@ -48,7 +54,7 @@ app.Use(logger.New(logger.Config{
}))
```
#### **Custom File Writer**
### Custom File Writer
```go
file, err := os.OpenFile("./123.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
@ -61,7 +67,7 @@ app.Use(logger.New(logger.Config{
}))
```
### Config
## Config
```go
// Config defines the config for middleware.
type Config struct {
@ -97,7 +103,7 @@ type Config struct {
}
```
### Default Config
## Default Config
```go
var ConfigDefault = Config{
Next: nil,
@ -109,7 +115,7 @@ var ConfigDefault = Config{
}
```
### Constants
## Constants
```go
// Logger variables
const (