API
# Endpoint Interface v0.21.0+
// Endpoint is an interface defining the basic structure of an endpoint in the rulego.
type Endpoint interface {
types.Node
// Id returns a unique identifier for the endpoint.
Id() string
// SetOnEvent sets the event listener function for the endpoint.
SetOnEvent(onEvent OnEvent)
// Start initiates the service.
Start() error
// AddInterceptors adds global interceptors to the endpoint.
AddInterceptors(interceptors ...Process)
// AddRouter adds a router with optional parameters and returns a router ID.
// Some endpoints will return a new router ID, which needs to be updated with a new router ID
AddRouter(router Router, params ...interface{}) (string, error)
// RemoveRouter removes a router by its ID with optional parameters.
RemoveRouter(routerId string, params ...interface{}) error
}
// DynamicEndpoint is an interface for dynamically defining an endpoint using a DSL (`types.EndpointDsl`).
type DynamicEndpoint interface {
Endpoint
SetId(id string)
// SetConfig sets the configuration for the dynamic endpoint.
SetConfig(config types.Config)
// SetRouterOptions sets options for the router.
SetRouterOptions(opts ...RouterOption)
// SetRestart restart the endpoint.
SetRestart(restart bool)
// SetInterceptors sets the interceptors for the dynamic endpoint.
SetInterceptors(interceptors ...Process)
// Reload reloads the dynamic endpoint with a new DSL configuration.
// If need to reload the endpoint, use `endpoint.DynamicEndpointOptions.WithRestart(true)`
// Else the endpoint only update the route without restarting
// Routing conflict, endpoint must be restarted
Reload(dsl []byte, opts ...DynamicEndpointOption) error
// ReloadFromDef reloads the dynamic endpoint with a new DSL configuration.
ReloadFromDef(def types.EndpointDsl, opts ...DynamicEndpointOption) error
// AddOrReloadRouter reloads or add the router with a new DSL configuration.
AddOrReloadRouter(dsl []byte, opts ...DynamicEndpointOption) error
// Definition returns the DSL definition of the dynamic endpoint.
Definition() types.EndpointDsl
// DSL returns the DSL configuration of the dynamic endpoint.
DSL() []byte
// Target returns the target endpoint.
Target() Endpoint
}
// Factory is an interface defining that creates Endpoints.
type Factory interface {
// NewFromDsl creates a new DynamicEndpoint instance from DSL.
NewFromDsl(dsl []byte, opts ...DynamicEndpointOption) (DynamicEndpoint, error)
// NewFromDef creates a new DynamicEndpoint instance from DSL.
NewFromDef(def types.EndpointDsl, opts ...DynamicEndpointOption) (DynamicEndpoint, error)
// NewFromType creates a new Endpoint instance from type.
NewFromType(componentType string, ruleConfig types.Config, configuration interface{}) (Endpoint, error)
}
// Pool is an interface defining operations for managing a pool of endpoints.
type Pool interface {
// New creates a new dynamic endpoint.
New(id string, del []byte, opts ...DynamicEndpointOption) (DynamicEndpoint, error)
// Get retrieves a dynamic endpoint by its ID.
Get(id string) (DynamicEndpoint, bool)
// Del removes a dynamic endpoint instance by its ID.
Del(id string)
// Stop releases all dynamic endpoint instances.
Stop()
// Reload reloads all dynamic endpoint instances.
Reload(opts ...DynamicEndpointOption)
// Range iterates over all dynamic endpoint instances.
Range(f func(key, value any) bool)
// Factory returns the factory used to create endpoint instances.
Factory() Factory
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
Edit this page on GitHub (opens new window)
Last Updated: 2024/10/23, 10:13:01