Router
# Create Router
Router is a type used to define routing rules, and is the routing abstraction of various Endpoint services. It can specify input end, transformation function, processing function, output end, etc.
You can use the NewRouter function to create a pointer of type Router, and then use the From method to specify the input end, returning a pointer of type From.
router := endpoint.NewRouter().From("/api/v1/msg/")
# Router Options
When creating a Router, you can specify Options:
endpoint.NewRouter(opts ...endpoint.RouterOption)
- WithRuleGoFunc Function to dynamically retrieve the rule chain pool
endpoint.WithRuleGoFunc(f func(exchange *Exchange) *rulego.RuleGo)
- WithRuleGo Change the rule chain pool, defaulting to use rulego.DefaultRuleGo
endpoint.WithRuleGo(ruleGo *rulego.RuleGo)
- WithRuleConfig Change the rule engine configuration
endpoint.WithRuleConfig(config types.Config)
# Add processing functions
The From type has two methods to add processing functions: Transform and Process. Both methods accept a function of type Process as an argument and return a pointer of type From.
The Transform method is used to transform the input message into a RuleMsg type.
The Process method is used to process the input or output message.
The Process type function accepts a pointer of type Exchange as an argument and returns a boolean value indicating whether to continue executing the next processing function.
The Exchange type is a structure that contains an input message and an output message, which are used to pass data in the processing function.
router := endpoint.NewRouter().From("/api/v1/msg/").Transform(func(exchange *endpoint.Exchange) bool {
//Transformation logic
return true
}).Process(func(exchange *endpoint.Exchange) bool {
//Processing logic
return true
})
2
3
4
5
6
7
# Response
You can use the Out message of Exchange in the transformation or processing function to respond to the client
//Response error
exchange.Out.SetStatusCode(http.StatusMethodNotAllowed)
//Response header
exchange.Out.Headers().Set("Content-Type", "application/json")
//Response content
exchange.Out.SetBody([]byte("ok"))
2
3
4
5
6
Note: mqtt endpoint calling SetBody() will use the specified topic to public data from the broker, specify the topic using the following method
exchange.Out.Headers().Set("topic", "your topic")
# Set output end
The From type has two methods to set the output end: To and ToComponent. Both methods return a pointer of type To.
- The To method is used to specify the flow target path or component.
- The ToComponent method is used to specify the output component.
The parameter of the To method is a string that represents the component path, in the format of {executorType}:{path}. executorType is the executor component type, path is the component path. For example: "chain:{chainId}" means to execute the rule chain registered in rulego, "component:{nodeType}" means to execute the component registered in config.ComponentsRegistry.
You can register custom executor component types in DefaultExecutorFactory. The To method can also accept some component configuration parameters as optional parameters.
router := endpoint.NewRouter().From("/api/v1/msg/").Transform(func(exchange *endpoint.Exchange) bool {
//Transformation logic
return true
}).To("chain:default")
2
3
4
The parameter of the ToComponent method is a component of type types.Node, which you can customize or use existing components.
router := endpoint.NewRouter().From("/api/v1/msg/").Transform(func(exchange *endpoint.Exchange) bool {
//Transformation logic
return true
}).ToComponent(func() types.Node {
//Define log component, process data
var configuration = make(types.Configuration)
configuration["jsScript"] = `
return 'log::Incoming message:\n' + JSON.stringify(msg) + '\nIncoming metadata:\n' + JSON.stringify(metadata);
`
logNode := &action.LogNode{}
_ = logNode.Init(config, configuration)
return logNode
}())
2
3
4
5
6
7
8
9
10
11
12
13
You can also use the To method to call the component
router := endpoint.NewRouter().From("/api/v1/msg/").Transform(func(exchange *endpoint.Exchange) bool {
//Transformation logic
return true
}).To"component:log", types.Configuration{"jsScript": `
return 'log::Incoming message:\n' + JSON.stringify(msg) + '\nIncoming metadata:\n' + JSON.stringify(metadata);
`})
2
3
4
5
6
# RuleContextOption
Set the execution context for the rule chain Options
# Wait
Synchronously wait for the To component execution result and return to the parent process. For example: http endpoint needs to respond the rule chain processing result to the client, you need to set this semantics, otherwise do not set it, it will affect the throughput.
# End route
The To type has a method to end the route: End. The End method returns a pointer of type Router.
router := endpoint.NewRouter().From("/api/v1/msg/").Transform(func(exchange *endpoint.Exchange) bool {
//Transformation logic
return true
}).To("chain:default").End()
2
3
4