hook

package
v1.4.6 Latest Latest
Warning

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

Go to latest
Published: Jan 29, 2025 License: GPL-3.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// LuaScripts is a map that stores precompiled Lua scripts, allowing safe concurrent access and manipulation.
	LuaScripts = make(map[string]*PrecompiledLuaScript)
)

Functions

func InitHTTPClient

func InitHTTPClient()

InitHTTPClient initializes the global httpClient variable with a pre-configured instance from util.NewHTTPClient.

func PreCompileLuaHooks added in v1.3.9

func PreCompileLuaHooks() error

PreCompileLuaHooks precompiles Lua scripts for pre-defined hooks in the configuration. If Lua hooks are enabled in the configuration and no custom location exists, a new custom location is created. Iterates through the hooks list, precompiles each Lua script, and associates it with the corresponding HTTP method and location.

func PreCompileLuaScript

func PreCompileLuaScript(filePath string) (err error)

PreCompileLuaScript pre-compiles the Lua callback script and replaces the current luaScript with the new one. If the LoadableConfig has a Lua callback and the current luaScript is nil, a new instance of PrecompiledLuaScript is created. The function calls NewLuaHook to get the new pre-compiled Lua callback script. If an error occurs during the pre-compilation, the function returns the error. If no error occurs, the new Lua callback script replaces the current luaScript's luaScript. The function returns nil if it executes successfully.

func RunLuaHook added in v1.3.9

func RunLuaHook(ctx *gin.Context) (gin.H, error)

RunLuaHook executes a precompiled Lua script based on a hook parameter from the gin.Context.

func RunLuaInit

func RunLuaInit(ctx context.Context, hook string) error

RunLuaInit initializes and runs a Lua script based on the specified hook.

Types

type CustomHook added in v1.3.9

type CustomHook map[HttpMethod]*PrecompiledLuaScript

CustomHook maps an HTTP method to its corresponding precompiled Lua script for handling specific requests.

func NewCustomHook added in v1.3.9

func NewCustomHook(httpMethod string, script *PrecompiledLuaScript) CustomHook

NewCustomHook creates a new CustomHook with the specified HTTP method and precompiled Lua script.

func (CustomHook) GetScript added in v1.3.9

func (h CustomHook) GetScript(method string) *PrecompiledLuaScript

GetScript retrieves a precompiled Lua script associated with the provided HTTP method string. method: The HTTP method as a string. Returns the corresponding *PrecompiledLuaScript, or nil if not found.

func (CustomHook) SetScript added in v1.3.9

func (h CustomHook) SetScript(method string, script *PrecompiledLuaScript)

SetScript assigns a precompiled Lua script to a specific HTTP method. method: The HTTP method as a string. script: A pointer to the precompiled Lua script to be associated with the method.

type CustomLocation added in v1.3.9

type CustomLocation map[Location]CustomHook

CustomLocation is a map where each key is a Location and each value is a CustomHook.

func NewCustomLocation added in v1.3.9

func NewCustomLocation() CustomLocation

NewCustomLocation creates a new CustomLocation, which is a map of Location keys to CustomHook values.

func (CustomLocation) GetCustomHook added in v1.3.9

func (l CustomLocation) GetCustomHook(location string) CustomHook

GetCustomHook retrieves the CustomHook associated with the provided location string. Returns the corresponding CustomHook if found, otherwise nil.

func (CustomLocation) GetScript added in v1.3.9

func (l CustomLocation) GetScript(location, method string) *PrecompiledLuaScript

GetScript retrieves a precompiled Lua script for a specified location and method. Parameters:

  • location: The location string to search for.
  • method: The HTTP method string to search for.

Returns:

*PrecompiledLuaScript: The precompiled Lua script if found, otherwise nil.

func (CustomLocation) SetScript added in v1.3.9

func (l CustomLocation) SetScript(location, method string, script *PrecompiledLuaScript)

SetScript assigns a precompiled Lua script to a specific HTTP method for the given location.

type HttpMethod added in v1.3.9

type HttpMethod string

HttpMethod represents the HTTP methods used in HTTP requests.

type Location added in v1.3.9

type Location string

Location represents a string type specifically used to denote a location.

type PrecompiledLuaScript

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

PrecompiledLuaScript represents a type that holds a precompiled Lua script and allows safe concurrent access to the script.

func NewLuaHook

func NewLuaHook(filePath string) (*PrecompiledLuaScript, error)

NewLuaHook compiles a Lua script based on the provided file path and returns a new instance of PrecompiledLuaScript with the compiled script. If there is an error during the compilation, the function returns nil and the error. The returned PrecompiledLuaScript can be used to replace the luaScript of the current luaScript.

func (*PrecompiledLuaScript) GetPrecompiledScript

func (p *PrecompiledLuaScript) GetPrecompiledScript() *lua.FunctionProto

GetPrecompiledScript is a method of the PrecompiledLuaScript struct. It returns the precompiled Lua script as a pointer to the lua.FunctionProto. The method locks the read/write mutex of the PrecompiledLuaScript using RLock() before returning the luaScript. It then unlocks the mutex using RUnlock() in a deferred statement.

Returns:

  • luaScript *lua.FunctionProto: The precompiled Lua script as a pointer to the lua.FunctionProto.

Example usage:

script := p.GetPrecompiledScript()
// Use the script for further processing

func (*PrecompiledLuaScript) Replace

func (p *PrecompiledLuaScript) Replace(luaScript *PrecompiledLuaScript)

Replace is a method of the PrecompiledLuaScript struct that replaces the luaScript of p with the luaScript of luaCallback. The method locks the read/write mutex of the PrecompiledLuaScript using Lock() before assigning the new luaScript to the target PrecompiledLuaScript. It then unlocks the mutex using Unlock() in a deferred statement.

Jump to

Keyboard shortcuts

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