🛠 separate config

pull/1009/head
Fenny 2020-11-11 14:03:16 +01:00
parent d8eb5a5c25
commit 687717f113
10 changed files with 317 additions and 245 deletions

View File

@ -0,0 +1,94 @@
package logger
import (
"io"
"os"
"time"
"github.com/gofiber/fiber/v2"
)
// Config defines the config for middleware.
type Config struct {
// Next defines a function to skip this middleware when returned true.
//
// Optional. Default: nil
Next func(c *fiber.Ctx) bool
// Format defines the logging tags
//
// Optional. Default: [${time}] ${status} - ${latency} ${method} ${path}\n
Format string
// TimeFormat https://programming.guide/go/format-parse-string-time-date-example.html
//
// Optional. Default: 15:04:05
TimeFormat string
// TimeZone can be specified, such as "UTC" and "America/New_York" and "Asia/Chongqing", etc
//
// Optional. Default: "Local"
TimeZone string
// TimeInterval is the delay before the timestamp is updated
//
// Optional. Default: 500 * time.Millisecond
TimeInterval time.Duration
// Output is a writter where logs are written
//
// Default: os.Stderr
Output io.Writer
enableColors bool
enableLatency bool
timeZoneLocation *time.Location
}
// ConfigDefault is the default config
var ConfigDefault = Config{
Next: nil,
Format: "[${time}] ${status} - ${latency} ${method} ${path}\n",
TimeFormat: "15:04:05",
TimeZone: "Local",
TimeInterval: 500 * time.Millisecond,
Output: os.Stderr,
enableColors: true,
}
// Helper function to set default values
func configDefault(config ...Config) Config {
// Return default config if nothing provided
if len(config) < 1 {
return ConfigDefault
}
// Override default config
cfg := config[0]
// Enable colors if no custom format or output is given
if cfg.Format == "" && cfg.Output == nil {
cfg.enableColors = true
}
// Set default values
if cfg.Next == nil {
cfg.Next = ConfigDefault.Next
}
if cfg.Format == "" {
cfg.Format = ConfigDefault.Format
}
if cfg.TimeZone == "" {
cfg.TimeZone = ConfigDefault.TimeZone
}
if cfg.TimeFormat == "" {
cfg.TimeFormat = ConfigDefault.TimeFormat
}
if int(cfg.TimeInterval) <= 0 {
cfg.TimeInterval = ConfigDefault.TimeInterval
}
if cfg.Output == nil {
cfg.Output = ConfigDefault.Output
}
return cfg
}

View File

@ -18,53 +18,6 @@ import (
"github.com/valyala/fasthttp"
)
// Config defines the config for middleware.
type Config struct {
// Next defines a function to skip this middleware when returned true.
//
// Optional. Default: nil
Next func(c *fiber.Ctx) bool
// Format defines the logging tags
//
// Optional. Default: [${time}] ${status} - ${latency} ${method} ${path}\n
Format string
// TimeFormat https://programming.guide/go/format-parse-string-time-date-example.html
//
// Optional. Default: 15:04:05
TimeFormat string
// TimeZone can be specified, such as "UTC" and "America/New_York" and "Asia/Chongqing", etc
//
// Optional. Default: "Local"
TimeZone string
// TimeInterval is the delay before the timestamp is updated
//
// Optional. Default: 500 * time.Millisecond
TimeInterval time.Duration
// Output is a writter where logs are written
//
// Default: os.Stderr
Output io.Writer
enableColors bool
enableLatency bool
timeZoneLocation *time.Location
}
// ConfigDefault is the default config
var ConfigDefault = Config{
Next: nil,
Format: "[${time}] ${status} - ${latency} ${method} ${path}\n",
TimeFormat: "15:04:05",
TimeZone: "Local",
TimeInterval: 500 * time.Millisecond,
Output: os.Stderr,
}
// Logger variables
const (
TagPid = "pid"
@ -117,39 +70,7 @@ const (
// New creates a new middleware handler
func New(config ...Config) fiber.Handler {
// Set default config
cfg := ConfigDefault
// Override config if provided
if len(config) > 0 {
cfg = config[0]
// Enable colors if no custom format or output is given
if cfg.Format == "" && cfg.Output == nil {
cfg.enableColors = true
}
// Set default values
if cfg.Next == nil {
cfg.Next = ConfigDefault.Next
}
if cfg.Format == "" {
cfg.Format = ConfigDefault.Format
}
if cfg.TimeZone == "" {
cfg.TimeZone = ConfigDefault.TimeZone
}
if cfg.TimeFormat == "" {
cfg.TimeFormat = ConfigDefault.TimeFormat
}
if int(cfg.TimeInterval) <= 0 {
cfg.TimeInterval = ConfigDefault.TimeInterval
}
if cfg.Output == nil {
cfg.Output = ConfigDefault.Output
}
} else {
cfg.enableColors = true
}
cfg := configDefault(config...)
// Get timezone location
tz, err := time.LoadLocation(cfg.TimeZone)

View File

@ -0,0 +1,55 @@
package proxy
import (
"github.com/gofiber/fiber/v2"
)
// Config defines the config for middleware.
type Config struct {
// Next defines a function to skip this middleware when returned true.
//
// Optional. Default: nil
Next func(c *fiber.Ctx) bool
// Servers defines a list of <scheme>://<host> HTTP servers,
//
// which are used in a round-robin manner.
// i.e.: "https://foobar.com, http://www.foobar.com"
//
// Required
Servers []string
// ModifyRequest allows you to alter the request
//
// Optional. Default: nil
ModifyRequest fiber.Handler
// ModifyResponse allows you to alter the response
//
// Optional. Default: nil
ModifyResponse fiber.Handler
}
// ConfigDefault is the default config
var ConfigDefault = Config{
Next: nil,
ModifyRequest: nil,
ModifyResponse: nil,
}
// Helper function to set default values
func configDefault(config ...Config) Config {
// Return default config if nothing provided
if len(config) < 1 {
return ConfigDefault
}
// Override default config
cfg := config[0]
// Set default values
if len(cfg.Servers) == 0 {
panic("Servers cannot be empty")
}
return cfg
}

View File

@ -9,39 +9,6 @@ import (
"github.com/valyala/fasthttp"
)
// Config defines the config for middleware.
type Config struct {
// Next defines a function to skip this middleware when returned true.
//
// Optional. Default: nil
Next func(c *fiber.Ctx) bool
// Servers defines a list of <scheme>://<host> HTTP servers,
//
// which are used in a round-robin manner.
// i.e.: "https://foobar.com, http://www.foobar.com"
//
// Required
Servers []string
// ModifyRequest allows you to alter the request
//
// Optional. Default: nil
ModifyRequest fiber.Handler
// ModifyResponse allows you to alter the response
//
// Optional. Default: nil
ModifyResponse fiber.Handler
}
// ConfigDefault is the default config
var ConfigDefault = Config{
Next: nil,
ModifyRequest: nil,
ModifyResponse: nil,
}
// New is deprecated
func New(config Config) fiber.Handler {
fmt.Println("proxy.New is deprecated, please use proxy.Balancer instead")
@ -50,16 +17,8 @@ func New(config Config) fiber.Handler {
// Balancer creates a load balancer among multiple upstream servers
func Balancer(config Config) fiber.Handler {
// Override config if provided
cfg := config
// Set default values
if cfg.Next == nil {
cfg.Next = ConfigDefault.Next
}
if len(cfg.Servers) == 0 {
panic("Servers cannot be empty")
}
// Set default config
cfg := configDefault(config)
client := fasthttp.Client{
NoDefaultUserAgentHeader: true,

View File

@ -0,0 +1,31 @@
package recover
import (
"github.com/gofiber/fiber/v2"
)
// Config defines the config for middleware.
type Config struct {
// Next defines a function to skip this middleware when returned true.
//
// Optional. Default: nil
Next func(c *fiber.Ctx) bool
}
// ConfigDefault is the default config
var ConfigDefault = Config{
Next: nil,
}
// Helper function to set default values
func configDefault(config ...Config) Config {
// Return default config if nothing provided
if len(config) < 1 {
return ConfigDefault
}
// Override default config
cfg := config[0]
return cfg
}

View File

@ -6,28 +6,10 @@ import (
"github.com/gofiber/fiber/v2"
)
// Config defines the config for middleware.
type Config struct {
// Next defines a function to skip this middleware when returned true.
//
// Optional. Default: nil
Next func(c *fiber.Ctx) bool
}
// ConfigDefault is the default config
var ConfigDefault = Config{
Next: nil,
}
// New creates a new middleware handler
func New(config ...Config) fiber.Handler {
// Set default config
cfg := ConfigDefault
// Override config if provided
if len(config) > 0 {
cfg = config[0]
}
cfg := configDefault(config...)
// Return new handler
return func(c *fiber.Ctx) (err error) {

View File

@ -0,0 +1,61 @@
package requestid
import (
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/utils"
)
// Config defines the config for middleware.
type Config struct {
// Next defines a function to skip this middleware when returned true.
//
// Optional. Default: nil
Next func(c *fiber.Ctx) bool
// Header is the header key where to get/set the unique request ID
//
// Optional. Default: "X-Request-ID"
Header string
// Generator defines a function to generate the unique identifier.
//
// Optional. Default: utils.UUID
Generator func() string
// ContextKey defines the key used when storing the request ID in
// the locals for a specific request.
//
// Optional. Default: requestid
ContextKey string
}
// ConfigDefault is the default config
var ConfigDefault = Config{
Next: nil,
Header: fiber.HeaderXRequestID,
Generator: utils.UUID,
ContextKey: "requestid",
}
// Helper function to set default values
func configDefault(config ...Config) Config {
// Return default config if nothing provided
if len(config) < 1 {
return ConfigDefault
}
// Override default config
cfg := config[0]
// Set default values
if cfg.Header == "" {
cfg.Header = ConfigDefault.Header
}
if cfg.Generator == nil {
cfg.Generator = ConfigDefault.Generator
}
if cfg.ContextKey == "" {
cfg.ContextKey = ConfigDefault.ContextKey
}
return cfg
}

View File

@ -2,61 +2,12 @@ package requestid
import (
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/utils"
)
// Config defines the config for middleware.
type Config struct {
// Next defines a function to skip this middleware when returned true.
//
// Optional. Default: nil
Next func(c *fiber.Ctx) bool
// Header is the header key where to get/set the unique request ID
//
// Optional. Default: "X-Request-ID"
Header string
// Generator defines a function to generate the unique identifier.
//
// Optional. Default: utils.UUID
Generator func() string
// ContextKey defines the key used when storing the request ID in
// the locals for a specific request.
//
// Optional. Default: requestid
ContextKey string
}
// ConfigDefault is the default config
var ConfigDefault = Config{
Next: nil,
Header: fiber.HeaderXRequestID,
Generator: utils.UUID,
ContextKey: "requestid",
}
// New creates a new middleware handler
func New(config ...Config) fiber.Handler {
// Set default config
cfg := ConfigDefault
// Override config if provided
if len(config) > 0 {
cfg = config[0]
// Set default values
if cfg.Header == "" {
cfg.Header = ConfigDefault.Header
}
if cfg.Generator == nil {
cfg.Generator = ConfigDefault.Generator
}
if cfg.ContextKey == "" {
cfg.ContextKey = ConfigDefault.ContextKey
}
}
cfg := configDefault(config...)
// Return new handler
return func(c *fiber.Ctx) error {

View File

@ -0,0 +1,69 @@
package session
import (
"time"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/internal/storage/memory"
"github.com/gofiber/fiber/v2/utils"
)
// Config defines the config for middleware.
type Config struct {
// Possible values:
// - "header:<name>"
// - "query:<name>"
// - "param:<name>"
// - "form:<name>"
// - "cookie:<name>"
//
// Optional. Default value "cookie:_csrf".
// TODO: When to override Cookie.Value?
KeyLookup string
// Optional. Session ID generator function.
//
// Default: utils.UUID
KeyGenerator func() string
// Optional. Cookie to set values on
//
// NOTE: Value, MaxAge and Expires will be overriden by the session ID and expiration
// TODO: Should this be a pointer, if yes why?
Cookie fiber.Cookie
// Allowed session duration
//
// Optional. Default: 24 hours
Expiration time.Duration
// Store interface
// Optional. Default: memory.New()
Storage fiber.Storage
}
// ConfigDefault is the default config
var ConfigDefault = Config{
Cookie: fiber.Cookie{
Value: "session_id",
},
Expiration: 30 * time.Minute,
KeyGenerator: utils.UUID,
}
// Helper function to set default values
func configDefault(config ...Config) Config {
// Return default config if nothing provided
if len(config) < 1 {
return ConfigDefault
}
// Override default config
cfg := config[0]
// Set default values
if cfg.Storage == nil {
cfg.Storage = memory.New()
}
return cfg
}

View File

@ -2,68 +2,17 @@ package session
import (
"fmt"
"time"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/internal/storage/memory"
"github.com/gofiber/fiber/v2/utils"
)
type Config struct {
// Possible values:
// - "header:<name>"
// - "query:<name>"
// - "param:<name>"
// - "form:<name>"
// - "cookie:<name>"
//
// Optional. Default value "cookie:_csrf".
// TODO: When to override Cookie.Value?
KeyLookup string
// Optional. Session ID generator function.
//
// Default: utils.UUID
KeyGenerator func() string
// Optional. Cookie to set values on
//
// NOTE: Value, MaxAge and Expires will be overriden by the session ID and expiration
// TODO: Should this be a pointer, if yes why?
Cookie fiber.Cookie
// Allowed session duration
//
// Optional. Default: 24 hours
Expiration time.Duration
// Store interface
// Optional. Default: memory.New()
Storage fiber.Storage
}
var ConfigDefault = Config{
Cookie: fiber.Cookie{
Value: "session_id",
},
Expiration: 30 * time.Minute,
KeyGenerator: utils.UUID,
}
type Store struct {
Config
}
func New(config ...Config) *Store {
cfg := ConfigDefault
if len(config) > 0 {
cfg = config[0]
}
if cfg.Storage == nil {
cfg.Storage = memory.New()
}
// Set default config
cfg := configDefault(config...)
return &Store{
cfg,