router

package
v0.5.2 Latest Latest
Warning

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

Go to latest
Published: Jan 24, 2024 License: MIT Imports: 3 Imported by: 0

README

Router

Go doc

暂无介绍...

目录导航

列出了该 package 下所有的函数及类型定义,可通过目录导航进行快捷跳转 ❤️

展开 / 折叠目录导航

包级函数定义

函数名称 描述
NewMultistage 创建一个支持多级分类的路由器
WithRouteTrim 路由修剪选项

类型定义

类型 名称 描述
STRUCT MultistageBind 多级分类路由绑定函数
STRUCT Multistage 支持多级分类的路由器
STRUCT MultistageOption 路由器选项

详情信息

func NewMultistage[HandleFunc any](options ...MultistageOption[HandleFunc]) *Multistage[HandleFunc]

创建一个支持多级分类的路由器

示例代码:


func ExampleNewMultistage() {
	router.NewMultistage[func()]()
}


func WithRouteTrim[HandleFunc any](handle func (route any) any) MultistageOption[HandleFunc]

路由修剪选项

  • 将在路由注册前对路由进行对应处理

MultistageBind STRUCT

多级分类路由绑定函数

type MultistageBind[HandleFunc any] func(HandleFunc)

func (MultistageBind) Bind(handleFunc HandleFunc)

将处理函数绑定到预设的路由中


Multistage STRUCT

支持多级分类的路由器

type Multistage[HandleFunc any] struct {
	routes map[any]HandleFunc
	subs   map[any]*Multistage[HandleFunc]
	tag    any
	trim   func(route any) any
}

func (*Multistage) Register(routes ...any) MultistageBind[HandleFunc]

注册路由是结合 Sub 和 Route 的快捷方式,用于一次性注册多级路由

  • 该函数将返回一个注册函数,可通过调用其将路由绑定到特定处理函数,例如:router.Register("a", "b").Bind(onExec())

示例代码:


func ExampleMultistage_Register() {
	r := router.NewMultistage[func()]()
	r.Register("System", "Network", "Ping")(func() {
	})
}


func (*Multistage) Route(route any, handleFunc HandleFunc)

为特定路由绑定处理函数,被绑定的处理函数将可以通过 Match 函数进行匹配

示例代码:


func ExampleMultistage_Route() {
	r := router.NewMultistage[func()]()
	r.Route("ServerTime", func() {
	})
}


func (*Multistage) Match(routes ...any) HandleFunc

匹配已绑定处理函数的路由,返回处理函数

  • 如果未找到将会返回空指针

示例代码:


func ExampleMultistage_Match() {
	r := router.NewMultistage[func()]()
	r.Route("ServerTime", func() {
	})
	r.Register("System", "Network", "Ping").Bind(func() {
	})
	r.Match("ServerTime")()
	r.Match("System", "Network", "Ping")()
}

查看 / 收起单元测试

func TestMultistage_Match(t *testing.T) {
	r := router.NewMultistage[func()]()
	r.Sub("System").Route("Heartbeat", func() {
		fmt.Println("Heartbeat")
	})
	r.Route("ServerTime", func() {
		fmt.Println("ServerTime")
	})
	r.Register("System", "Network", "Ping")(func() {
		fmt.Println("Ping")
	})
	r.Register("System", "Network", "Echo").Bind(onEcho)
	r.Match("System", "Heartbeat")()
	r.Match("ServerTime")()
	r.Match("System", "Network", "Ping")()
	r.Match("System", "Network", "Echo")()
	fmt.Println(r.Match("None") == nil)
}


func (*Multistage) Sub(route any) *Multistage[HandleFunc]

获取子路由器

示例代码:


func ExampleMultistage_Sub() {
	r := router.NewMultistage[func()]()
	r.Sub("System").Route("Heartbeat", func() {
	})
}


MultistageOption STRUCT

路由器选项

type MultistageOption[HandleFunc any] func(multistage *Multistage[HandleFunc])

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Multistage added in v0.1.7

type Multistage[HandleFunc any] struct {
	// contains filtered or unexported fields
}

Multistage 支持多级分类的路由器

func NewMultistage added in v0.1.7

func NewMultistage[HandleFunc any](options ...MultistageOption[HandleFunc]) *Multistage[HandleFunc]

NewMultistage 创建一个支持多级分类的路由器

Example
package main

import (
	"github.com/kercylan98/minotaur/server/router"
)

func main() {
	router.NewMultistage[func()]()

}
Output:

func (*Multistage[HandleFunc]) Match added in v0.1.7

func (slf *Multistage[HandleFunc]) Match(routes ...any) HandleFunc

Match 匹配已绑定处理函数的路由,返回处理函数

  • 如果未找到将会返回空指针
Example
package main

import (
	"github.com/kercylan98/minotaur/server/router"
)

func main() {
	r := router.NewMultistage[func()]()

	r.Route("ServerTime", func() {})
	r.Register("System", "Network", "Ping").Bind(func() {})

	r.Match("ServerTime")()
	r.Match("System", "Network", "Ping")()

}
Output:

func (*Multistage[HandleFunc]) Register added in v0.1.7

func (slf *Multistage[HandleFunc]) Register(routes ...any) MultistageBind[HandleFunc]

Register 注册路由是结合 Sub 和 Route 的快捷方式,用于一次性注册多级路由

  • 该函数将返回一个注册函数,可通过调用其将路由绑定到特定处理函数,例如:router.Register("a", "b").Bind(onExec())
Example
package main

import (
	"github.com/kercylan98/minotaur/server/router"
)

func main() {
	r := router.NewMultistage[func()]()

	r.Register("System", "Network", "Ping")(func() {
		// ...
	})

}
Output:

func (*Multistage[HandleFunc]) Route added in v0.1.7

func (slf *Multistage[HandleFunc]) Route(route any, handleFunc HandleFunc)

Route 为特定路由绑定处理函数,被绑定的处理函数将可以通过 Match 函数进行匹配

Example
package main

import (
	"github.com/kercylan98/minotaur/server/router"
)

func main() {
	r := router.NewMultistage[func()]()

	r.Route("ServerTime", func() {
		// ...
	})

}
Output:

func (*Multistage[HandleFunc]) Sub added in v0.1.7

func (slf *Multistage[HandleFunc]) Sub(route any) *Multistage[HandleFunc]

Sub 获取子路由器

Example
package main

import (
	"github.com/kercylan98/minotaur/server/router"
)

func main() {
	r := router.NewMultistage[func()]()

	r.Sub("System").Route("Heartbeat", func() {
		// ...
	})

}
Output:

type MultistageBind added in v0.1.7

type MultistageBind[HandleFunc any] func(HandleFunc)

MultistageBind 多级分类路由绑定函数

func (MultistageBind[HandleFunc]) Bind added in v0.1.7

func (slf MultistageBind[HandleFunc]) Bind(handleFunc HandleFunc)

Bind 将处理函数绑定到预设的路由中

type MultistageOption added in v0.1.7

type MultistageOption[HandleFunc any] func(multistage *Multistage[HandleFunc])

MultistageOption 路由器选项

func WithRouteTrim added in v0.1.7

func WithRouteTrim[HandleFunc any](handle func(route any) any) MultistageOption[HandleFunc]

WithRouteTrim 路由修剪选项

  • 将在路由注册前对路由进行对应处理

Jump to

Keyboard shortcuts

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