feature

package
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: May 15, 2024 License: AGPL-3.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

LuaPool is a pool of Lua state instances.

Functions

func PreCompileLuaFeatures

func PreCompileLuaFeatures() (err error)

PreCompileLuaFeatures pre-compiles Lua features. It checks if the configuration for Lua features is loaded and if the LuaFeatures variable is already set. If the LuaFeatures variable is not set, it creates a new instance of PreCompiledLuaFeatures. If the LuaFeatures variable is already set, it resets it using the Reset method. Then it loops through the features in the configuration and creates a new LuaFeature instance for each feature. The LuaFeature instance is created using the NewLuaFeature function, passing the name and script path from the configuration. If there is an error creating the LuaFeature instance, the error is returned. The compiled Lua feature is added to the LuaFeatures variable using the Add method. Finally, it returns nil if there are no errors.

Types

type LuaFeature

type LuaFeature struct {
	Name           string
	CompiledScript *lua.FunctionProto
}

LuaFeature represents a Lua feature that has been compiled. It contains a name identifying the feature and the compiled Lua script.

func NewLuaFeature

func NewLuaFeature(name string, scriptPath string) (*LuaFeature, error)

NewLuaFeature creates a new instance of LuaFeature with the given name and script path. If the name or script path is empty, it returns an error. The function compiles the Lua script using lualib.CompileLua and assigns the compiled script to the CompiledScript field of the LuaFeature. The function returns the created LuaFeature instance and nil error if successful. Otherwise, it returns nil and the appropriate error.

type PreCompiledLuaFeatures

type PreCompiledLuaFeatures struct {
	LuaScripts []*LuaFeature
	Mu         sync.RWMutex
}

PreCompiledLuaFeatures represents a collection of pre-compiled Lua features. It contains an array of LuaFeature objects and a read-write mutex for synchronization.

var LuaFeatures *PreCompiledLuaFeatures

LuaFeatures is a pointer to a PreCompiledLuaFeatures object. It represents a collection of pre-compiled Lua scripts that can be executed.

The PreCompiledLuaFeatures struct has the following properties: - `LuaScripts`: a slice of LuaFeature objects representing the individual pre-compiled Lua scripts. - `mu`: a mutex used to synchronize access to the LuaScripts slice.

The PreCompiledLuaFeatures has two methods: - `Add(luaFeature *LuaFeature)`: adds a LuaFeature object to the LuaScripts slice. - `Reset()`: clears the LuaScripts slice.

Usage example: The PreCompileLuaFeatures function initializes the LuaFeatures variable by pre-compiling the Lua scripts specified in the configuration.

The CallFeatureLua method of the Request struct executes the pre-compiled Lua scripts stored in LuaFeatures on the provided gin.Context. It retrieves a read lock on the LuaFeatures object and creates a new Lua state. It then sets up the necessary Lua libraries and global variables. The executeScripts method is called to execute each pre-compiled Lua script in order, passing in the request and the Lua state. If a script triggers or aborts the execution of features, the execution is halted and the method returns the appropriate values.

func (*PreCompiledLuaFeatures) Add

func (a *PreCompiledLuaFeatures) Add(luaFeature *LuaFeature)

Add appends the given LuaFeature to the slice of LuaScripts in PreCompiledLuaFeatures.

func (*PreCompiledLuaFeatures) Reset

func (a *PreCompiledLuaFeatures) Reset()

Reset resets the slice of LuaScripts in PreCompiledLuaFeatures by creating a new empty slice. The method also acquires a lock on the PreCompiledLuaFeatures mutex before resetting the slice and defers the unlocking of the mutex until the method returns.

type Request

type Request struct {
	// Logs holds the custom log key-value pairs.
	Logs *lualib.CustomLogKeyValue

	// Context contains additional context data.
	*lualib.Context

	*lualib.CommonRequest
}

Request represents a request data structure with all the necessary information about a connection and SSL usage.

func (*Request) CallFeatureLua

func (r *Request) CallFeatureLua(ctx *gin.Context) (triggered bool, abortFeatures bool, err error)

CallFeatureLua executes Lua scripts for a given request context. It acquires a read lock on the LuaFeatures mutex. It creates a new Lua state and preloads necessary libraries. It sets global variables in the Lua state. It sets fields for the request in the Lua state. It executes the Lua scripts for the request. It returns the triggered flag, abortFeatures flag, and related error if any.

Jump to

Keyboard shortcuts

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