middlewares

package
v0.3.8 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2024 License: MIT Imports: 22 Imported by: 0

Documentation

Index

Constants

View Source
const (
	AuthUserKey      = "user"
	AuthProxyUserKey = "proxy_user"
)

Variables

View Source
var (
	DefaultSchemas = []string{
		"http://",
		"https://",
	}
	ExtensionSchemas = []string{
		"chrome-extension://",
		"safari-extension://",
		"moz-extension://",
		"ms-browser-extension://",
	}
	FileSchemas = []string{
		"file://",
	}
	WebSocketSchemas = []string{
		"ws://",
		"wss://",
	}
)

Functions

func NewBasicAuthMiddleware

func NewBasicAuthMiddleware() fw.IMiddlewareCtl

func NewLogMiddleware

func NewLogMiddleware(logger *logrus.Logger) fw.IMiddlewareCtl

func NewLoggerMiddleware

func NewLoggerMiddleware() fw.IMiddlewareGlobal

func NewRecoveryMiddleware

func NewRecoveryMiddleware(o *RecoveryOptions, logger *logrus.Logger) fw.IMiddlewareGlobal

func NewResponseRewriteMiddleware

func NewResponseRewriteMiddleware() fw.IMiddlewareGlobal

func NewServerDownMiddleware

func NewServerDownMiddleware() fw.IMiddlewareGlobal

func NewWebsocketHubMiddleware

func NewWebsocketHubMiddleware() fw.IMiddlewareCtl

func NewWebsocketMiddleware

func NewWebsocketMiddleware() fw.IMiddlewareMethod

Types

type Accounts

type Accounts map[string]string

type BasicAuthMiddleware

type BasicAuthMiddleware struct {
	*fw.MiddlewareCtl
	// contains filtered or unexported fields
}

func (*BasicAuthMiddleware) Execute added in v0.3.0

type Client

type Client struct {
	ID string
	// contains filtered or unexported fields
}

type Config

type Config struct {
	AllowAllOrigins bool

	// AllowOrigins is a list of origins a cross-domain request can be executed from.
	// If the special "*" value is present in the list, all origins will be allowed.
	// Default value is []
	AllowOrigins []string

	// AllowOriginFunc is a custom function to validate the origin. It takes the origin
	// as an argument and returns true if allowed or false otherwise. If this option is
	// set, the content of AllowOrigins is ignored.
	AllowOriginFunc func(origin string) bool

	// Same as AllowOriginFunc except also receives the full request context.
	// This function should use the context as a read only source and not
	// have any side effects on the request, such as aborting or injecting
	// values on the request.
	AllowOriginWithContextFunc func(c *fw.Context, origin string) bool

	// AllowMethods is a list of methods the client is allowed to use with
	// cross-domain requests. Default value is simple methods (GET, POST, PUT, PATCH, DELETE, HEAD, and OPTIONS)
	AllowMethods []string

	// AllowPrivateNetwork indicates whether the response should include allow private network header
	AllowPrivateNetwork bool

	// AllowHeaders is list of non simple headers the client is allowed to use with
	// cross-domain requests.
	AllowHeaders []string

	// AllowCredentials indicates whether the request can include user credentials like
	// cookies, HTTP authentication or client side SSL certificates.
	AllowCredentials bool

	// ExposeHeaders indicates which headers are safe to expose to the API of a CORS
	// API specification
	ExposeHeaders []string

	// MaxAge indicates how long (with second-precision) the results of a preflight request
	// can be cached
	MaxAge time.Duration

	// Allows to add origins like http://some-domain/*, https://api.* or http://some.*.subdomain.com
	AllowWildcard bool

	// Allows usage of popular browser extensions schemas
	AllowBrowserExtensions bool

	// Allows to add custom schema like tauri://
	CustomSchemas []string

	// Allows usage of WebSocket protocol
	AllowWebSockets bool

	// Allows usage of file:// schema (dangerous!) use it only when you 100% sure it's needed
	AllowFiles bool

	// Allows to pass custom OPTIONS response status code for old browsers / clients
	OptionsResponseStatusCode int
}

Config represents all available options for the middleware.

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns a generic default configuration mapped to localhost.

func (*Config) AddAllowHeaders

func (c *Config) AddAllowHeaders(headers ...string)

AddAllowHeaders is allowed to add custom headers

func (*Config) AddAllowMethods

func (c *Config) AddAllowMethods(methods ...string)

AddAllowMethods is allowed to add custom methods

func (*Config) AddExposeHeaders

func (c *Config) AddExposeHeaders(headers ...string)

AddExposeHeaders is allowed to add custom expose headers

func (Config) Validate

func (c Config) Validate() error

Validate is check configuration of user defined.

type CorsMiddleware

type CorsMiddleware struct {
	*fw.MiddlewareGlobal
	// contains filtered or unexported fields
}

func NewCorsMiddleware

func NewCorsMiddleware(config Config) *CorsMiddleware

func NewDefaultCorsMiddleware

func NewDefaultCorsMiddleware() *CorsMiddleware

func (*CorsMiddleware) Execute added in v0.3.0

func (*CorsMiddleware) Router added in v0.3.0

func (c *CorsMiddleware) Router(ctx *fw.MiddlewareContext) []*fw.RouteItem

type Data

type Data[T int | string] struct {
	Code    T
	Message string
	Data    interface{}
}

type Hub

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

func (*Hub) Broadcast

func (h *Hub) Broadcast(message []byte)

func (*Hub) SendByClient

func (h *Hub) SendByClient(id string, message []byte)

type LogMiddleware

type LogMiddleware struct {
	*fw.MiddlewareCtl
	Logger *logrus.Logger `inject:""`
	// contains filtered or unexported fields
}

LogMiddleware for logging request info. can be used on Controller or Method

func (*LogMiddleware) Execute added in v0.3.0

func (w *LogMiddleware) Execute(ctx *fw.MiddlewareContext) fw.HandlerFunc

type LogParams

type LogParams struct {
	// TimeStamp shows the time after the server returns a response.
	TimeStamp time.Time
	// StatusCode is HTTP response code.
	StatusCode int
	// Latency is how much time the server cost to process a certain request.
	Latency time.Duration
	// ClientIP equals Context's ClientIP method.
	ClientIP string
	// Method is the HTTP method given to the request.
	Method string
	// Path is a path the client requests.
	Path string
	// ErrorMessage is set if error has occurred in processing the request.
	ErrorMessage string
	// BodySize is the size of the Response Body
	BodySize int
	// Keys are the keys set on the request's context.
	Keys map[string]any

	Protocol  string
	UserAgent string
}

func (*LogParams) ClientIPWithColor

func (p *LogParams) ClientIPWithColor(f string) (string, color.Color)

func (*LogParams) LatencyWithColor

func (p *LogParams) LatencyWithColor(f string) (string, color.Color)

func (*LogParams) MethodWithColor

func (p *LogParams) MethodWithColor(f string) (string, color.Color)

func (*LogParams) StatusCodeWithColor

func (p *LogParams) StatusCodeWithColor(f string) (string, color.Color)

func (*LogParams) TimeStampWithColor

func (p *LogParams) TimeStampWithColor(f string) (string, color.Color)

type LoggerMiddleware

type LoggerMiddleware struct {
	*fw.MiddlewareGlobal
	Logger types.ILogger `inject:""`
}

func (*LoggerMiddleware) Execute added in v0.3.0

type RecoveryMiddleware

type RecoveryMiddleware struct {
	*fw.MiddlewareGlobal

	Logger *logrus.Logger `inject:""`
	// contains filtered or unexported fields
}

RecoveryMiddleware globally recover from panic

func (*RecoveryMiddleware) Execute added in v0.3.0

type RecoveryOptions

type RecoveryOptions struct {

	// if true, panic info and request info will show as web page
	// else output json to response
	// won't output stack trace to response unless the environment FW_DEBUG=true
	NiceWeb bool
	// whether output to console
	// won't output stack trace to console unless the environment FW_DEBUG=true
	Console bool
}

type ResponseRewriterMiddleware

type ResponseRewriterMiddleware struct {
	*fw.MiddlewareGlobal
}

func (*ResponseRewriterMiddleware) Execute added in v0.3.0

type ServerDownMiddleware

type ServerDownMiddleware struct {
	*fw.MiddlewareGlobal
	// contains filtered or unexported fields
}

ServerDownMiddleware is a middleware which provides an api to mark server down.

func (*ServerDownMiddleware) DoInitOnce added in v0.3.2

func (s *ServerDownMiddleware) DoInitOnce()

func (*ServerDownMiddleware) Execute added in v0.3.0

func (*ServerDownMiddleware) Router added in v0.3.0

type ServerDownOption added in v0.3.2

type ServerDownOption struct {
	Method string `yaml:"method" default:"PATCH"`
	Path   string `yaml:"path" default:"/serverDown/{key}"`
	Key    string `yaml:"key" default:""`
}

type WebsocketHubMiddleware

type WebsocketHubMiddleware struct {
	*fw.MiddlewareCtl
	// contains filtered or unexported fields
}

WebsocketHubMiddleware used for chat

func (*WebsocketHubMiddleware) Execute added in v0.3.0

func (*WebsocketHubMiddleware) Router added in v0.3.0

type WebsocketMiddleware

type WebsocketMiddleware struct {
	*fw.MiddlewareMethod
	// contains filtered or unexported fields
}

WebsocketMiddleware used for simple websocket communication with server

func (*WebsocketMiddleware) Execute added in v0.3.0

Jump to

Keyboard shortcuts

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