router

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Jan 21, 2021 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// FullMatchTarget is a match for full regular expression. All regexp router without expression
	// will use the expression.
	FullMatchTarget = ".*"
	// TailMatchTarget is a match expression for tail only.
	TailMatchTarget = "*"
)

Variables

This section is empty.

Functions

func Parse

func Parse(path string) (Router, Router, error)

Parse parses a path to a router tree. It returns the root router and the leaf router. you can add middlewares and executor to the routers. A valid path should like:

/segments/{segment}/resources/{resource}
/segments/{segment:[a-z]{1,2}}.log/paths/{path:*}

func Split

func Split(path string) ([]string, error)

Split splits string segments and regexp segments.

For instance:

/segments/{segment:[a-z]{1,2}}.log/paths/{path:*}

TO:

/segments/ {segment:[a-z]{1,2}} .log/paths/ {path:*}

Types

type Container

type Container interface {
	// Set sets key-value into the container.
	Set(key, value string)
	// Get gets a value by key from the container.
	Get(key string) (string, bool)
}

Container is a key-value container. It saves key-values from path.

type Inspector

type Inspector interface {
	// Inspect finds a valid executor to execute target context.
	// It returns an error if it can't find a valid executor.
	Inspect(context.Context) (executor.MiddlewareExecutor, error)
}

Inspector can select an executor to execute.

type RouteKind

type RouteKind string

RouteKind is kind of routers.

const (
	// String means the router has a fixed string.
	String RouteKind = "String"
	// Regexp means the router has a regular expression.
	Regexp RouteKind = "Regexp"
	// Path means the router matches the rest. Path router only can
	// be placed at the leaf node.
	Path RouteKind = "Path"
)

type Router

type Router interface {
	// Target returns the matching target of the node.
	// It can be a fixed string or a regular expression.
	Target() string
	// Kind returns the kind of the router node.
	Kind() RouteKind
	// Match find an executor matched by path.
	// The context contains information to inspect executor.
	// The container can save key-value pair from the path.
	// If the router is the leaf node to match the path, it will return
	// the first executor which Inspect() returns true.
	Match(ctx context.Context, c Container, path string) (executor.MiddlewareExecutor, error)
	// AddMiddleware adds middleware to the router node.
	// If the router matches a path, all middlewares in the router
	// will be executed by the returned executor.
	AddMiddleware(ms ...definition.Middleware)
	// Middlewares returns all middlewares of the router.
	// Don't modify the returned values.
	Middlewares() []definition.Middleware
	// SetInspector sets inspector to the router node.
	SetInspector(inspector Inspector)
	// Inspector gets inspector from the router node.
	// Don't modify the returned values.
	Inspector() Inspector
	// Merge merges r to the current router. The type of r should be same
	// as the current one or it panics.
	//
	// For instance:
	//  Router A: /namespaces/ -> {namespace}
	//  Router B: /nameless/ -> {other}
	// Result:
	//  /name -> spaces/ -> {namespace}
	//       |-> less/ -> {other}
	Merge(r Router) (Router, error)
}

Router describes the interface of a router node.

Jump to

Keyboard shortcuts

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