gorouter

package module
v0.0.0-...-3271e4a Latest Latest
Warning

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

Go to latest
Published: Feb 20, 2023 License: MIT Imports: 9 Imported by: 3

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidMatcher = errors.New("invalid matcher")
	ErrPathNotFound   = errors.New("path not found")
)
View Source
var (
	ErrNoMatchersSatisfied = errors.New("no matchers satisfied")
)

Functions

This section is empty.

Types

type DefaultRoute

type DefaultRoute[R any] struct {
	Method      string
	Path        string
	HandlerFunc func(R)
	ParamNames  []string
}

func (*DefaultRoute[R]) AddParamName

func (r *DefaultRoute[R]) AddParamName(name string)

func (*DefaultRoute[R]) GetHandler

func (r *DefaultRoute[R]) GetHandler() func(R)

func (*DefaultRoute[R]) GetMethod

func (r *DefaultRoute[R]) GetMethod() string

func (*DefaultRoute[R]) GetParamNames

func (r *DefaultRoute[R]) GetParamNames() []string

func (*DefaultRoute[R]) GetPath

func (r *DefaultRoute[R]) GetPath() string

type Route

type Route[R any] interface {
	GetMethod() string
	GetPath() string
	GetHandler() func(R)
	GetParamNames() []string
	AddParamName(string)
}

type RouteContext

type RouteContext struct {
	W http.ResponseWriter
	R *http.Request

	Method string
	Path   string
	UserId string // LoggedOn User
	Params map[string]string
	Query  url.Values
}

func (*RouteContext) FormFile

func (c *RouteContext) FormFile(name string, maxMemory int64) (*multipart.FileHeader, error)

func (*RouteContext) JSON

func (c *RouteContext) JSON(status int, v interface{}) error

func (*RouteContext) Redirect

func (c *RouteContext) Redirect(status int, url string)

func (*RouteContext) ShouldBindJSON

func (c *RouteContext) ShouldBindJSON(v interface{}) error

func (*RouteContext) Status

func (c *RouteContext) Status(status int)

func (*RouteContext) String

func (c *RouteContext) String(status int, str string)

func (*RouteContext) Stringf

func (c *RouteContext) Stringf(status int, format string, data ...interface{})

type RouteMatcher

type RouteMatcher[R any] interface {
	// Match tries to match the URL path.
	// Will return any remainder as well as a bool indicating whether a match was found or not.
	// Route Context is for the matcher to add a value to the context. It may be null (e.g., if it is used in add route).
	Match(method, path string, params *RouteParamList) (string, bool)

	// TokenMatch tries to match the template strings for a token.
	TokenMatch(token string, route Route[R]) (string, bool)

	// GetToken attempts to retrieve the next token. Returns if it was successful or not.
	// Router assumes the function will prefill the variables with the correct data
	// and then return true.
	GetToken(path string, route Route[R]) (string, bool)
}

func MatchPathToMatcher

func MatchPathToMatcher[R any](path string, route Route[R], tests []RouteMatcher[R]) (RouteMatcher[R], string, error)

MatchPathToMatcher tries to match a path to an effective RouteMatcher through its GetToken method. As it goes through a list of matchers sequentially and will accept the first acceptable matcher, the lower priority matchers should be at the bottom.

type RouteMatcherPlaceholder

type RouteMatcherPlaceholder[R any] struct{}

func (*RouteMatcherPlaceholder[R]) GetToken

func (r *RouteMatcherPlaceholder[R]) GetToken(path string, route Route[R]) (string, bool)

func (*RouteMatcherPlaceholder[R]) Match

func (r *RouteMatcherPlaceholder[R]) Match(method, path string, params *RouteParamList) (string, bool)

func (*RouteMatcherPlaceholder[R]) TokenMatch

func (r *RouteMatcherPlaceholder[R]) TokenMatch(path string, route Route[R]) (string, bool)

TokenMatch is called when we are adding a route. It checks if the token matches. Therefore, we need to add params into the route as well. In this sense, it does the same thing as GetToken.

type RouteMatcherRoot

type RouteMatcherRoot[R any] struct{}

Matches the root.

func (*RouteMatcherRoot[R]) GetToken

func (r *RouteMatcherRoot[R]) GetToken(path string, route Route[R]) (string, bool)

func (*RouteMatcherRoot[R]) Match

func (r *RouteMatcherRoot[R]) Match(method, path string, params *RouteParamList) (string, bool)

func (*RouteMatcherRoot[R]) TokenMatch

func (r *RouteMatcherRoot[R]) TokenMatch(path string, route Route[R]) (string, bool)

type RouteMatcherString

type RouteMatcherString[R any] struct {
	Path string
}

Matches a static string

func (*RouteMatcherString[R]) GetToken

func (r *RouteMatcherString[R]) GetToken(path string, route Route[R]) (string, bool)

func (*RouteMatcherString[R]) Match

func (r *RouteMatcherString[R]) Match(method, path string, params *RouteParamList) (string, bool)

func (*RouteMatcherString[R]) TokenMatch

func (r *RouteMatcherString[R]) TokenMatch(path string, route Route[R]) (string, bool)

type RouteParamList

type RouteParamList []string

func (*RouteParamList) Add

func (r *RouteParamList) Add(param string)

type RouterNode

type RouterNode[R any] struct {
	Children      []*RouterNode[R]
	Matcher       RouteMatcher[R]
	Route         map[string]Route[R]
	RouteMatchers func() []RouteMatcher[R]
}

func NewRouter

func NewRouter[R any]() *RouterNode[R]

func (*RouterNode[R]) AddRoute

func (r *RouterNode[R]) AddRoute(route Route[R]) error

func (*RouterNode[R]) Match

func (r *RouterNode[R]) Match(method, path string, ctx *RouteContext) (Route[R], error)

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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