RuleGo RuleGo
🏠Home
  • Quick Start
  • Rule Chain
  • Standard Components
  • Extension Components
  • Custom Components
  • Visualization
  • RuleGo-Server
  • RuleGo-MCP-Server
  • AOP
  • Trigger
  • Advanced Topics
  • Performance
  • Standard Components
  • Extension Components
  • Custom Components
  • Components Marketplace
  • Overview
  • Quick Start
  • Routing
  • DSL
  • API
  • Options
  • Components
🔥Editor (opens new window)
  • RuleGo Editor (opens new window)
  • RuleGo Server (opens new window)
  • Github (opens new window)
  • Gitee (opens new window)
  • Changelog (opens new window)
  • English
  • 简体中文
🏠Home
  • Quick Start
  • Rule Chain
  • Standard Components
  • Extension Components
  • Custom Components
  • Visualization
  • RuleGo-Server
  • RuleGo-MCP-Server
  • AOP
  • Trigger
  • Advanced Topics
  • Performance
  • Standard Components
  • Extension Components
  • Custom Components
  • Components Marketplace
  • Overview
  • Quick Start
  • Routing
  • DSL
  • API
  • Options
  • Components
🔥Editor (opens new window)
  • RuleGo Editor (opens new window)
  • RuleGo Server (opens new window)
  • Github (opens new window)
  • Gitee (opens new window)
  • Changelog (opens new window)
  • English
  • 简体中文

广告采用随机轮播方式显示 ❤️成为赞助商
  • Quick Start

  • Rule Chain

  • Standard Components

  • Extension Components

  • Custom Components

  • Components marketplace

  • Visualization

  • AOP

  • Trigger

  • Advanced Topic

  • RuleGo-Server

  • FAQ

  • Endpoint Module

    • Endpoint Overview
    • Quick Start
    • Router
      • Create Router
      • Router Options
      • Add processing functions
      • Response
      • Set output end
      • Wait
      • End route
    • DSL
    • API
    • Options
    • Endpoints

  • Support

目录

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/")
1

# Router Options

When creating a Router, you can specify Options:

endpoint.NewRouter(opts ...endpoint.RouterOption)
1
  • WithRuleGoFunc Function to dynamically retrieve the rule chain pool
endpoint.WithRuleGoFunc(f func(exchange *Exchange) *rulego.RuleGo)
1
  • WithRuleGo Change the rule chain pool, defaulting to use rulego.DefaultRuleGo
endpoint.WithRuleGo(ruleGo *rulego.RuleGo)
1
  • WithRuleConfig Change the rule engine configuration
endpoint.WithRuleConfig(config types.Config)
1

# 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
})
1
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"))
1
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")
1

# 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")
1
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
}())
1
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);
`})
1
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()
1
2
3
4
Edit this page on GitHub (opens new window)
Last Updated: 2025/04/02, 01:29:50
Quick Start
DSL

← Quick Start DSL→

Theme by Vdoing | Copyright © 2023-2025 RuleGo Team | Apache 2.0 License

  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式