router

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2024 License: MPL-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Vars

func Vars(w server.ResponseWriter) map[string]string

Get the variables associated with a route

This will only work if the underlying ResponseWriter is a VariablesResponseWriter

If the ResponseWriter does not have vars, then nil is returned

Types

type Matcher

type Matcher interface {
	// Match a request.
	//
	// Return nil if the request does not match.
	// Return a ResponseWriter if the path does match.
	//
	// The returned ResponseWriter should be created from `w`. Some matchers will
	// inject variables into the ResponseWriter while others will make no
	// alterations.
	Match(w server.ResponseWriter, r *server.Request) server.ResponseWriter
}

type Route

type Route struct {
	// contains filtered or unexported fields
}

A route for a Router

This route matches against `Path()`. If the r.Path matches, then the given handler from `Handle()` or `HandleFunc()` will be served.

func (*Route) Handle

func (self *Route) Handle(handler server.Handler) *Route

Set the handler for this route

func (*Route) HandleFunc

func (self *Route) HandleFunc(handler server.HandlerFunc) *Route

Set the handler for this route

func (*Route) Match

func (*Route) Matcher

func (self *Route) Matcher(m Matcher) *Route

Assign a custom matcher to the route

func (*Route) Path

func (self *Route) Path(path string) *Route

Assign a path to the route.

This will match against the `r.Path`. The path must match exactly and there may be variables in the path.

Variables are designated by "{NAME}", "{NAME:TYPE}", or "{NAME~REGEX}". There are 4 types available:

* segment (default) `[^/]+`

* int `0|-?[1-9]\d*`

* path `.*`

* slug `[a-zA-Z0-9-]+`

The matched variables will be stored in the response writer and can be accessed by calling `router.Vars(w)`.

func (*Route) Prefix

func (self *Route) Prefix(path string) *Route

Match against a prefix

the route matches if `r.Path` begins with `path`

func (*Route) ServeGemini

func (self *Route) ServeGemini(w server.ResponseWriter, r *server.Request)

type Router

type Router struct {
	// contains filtered or unexported fields
}

func NewRouter

func NewRouter() *Router

Create a new router

func (*Router) HandlePath

func (self *Router) HandlePath(path string, handler server.Handler)

Assign a path to the route.

This will match against the `r.Path`. The path must match exactly and there may be variables in the path.

Variables are designated by "{NAME}", "{NAME:TYPE}", or "{NAME~REGEX}". There are 4 types available:

* segment (default) `[^/]+`

* int `0|-?[1-9]\d*`

* path `.*`

* slug `[a-zA-Z0-9-]+`

The matched variables will be stored in the response writer and can be accessed by calling `router.Vars(w)`.

func (*Router) HandlePathFunc

func (self *Router) HandlePathFunc(path string, handler server.HandlerFunc)

Assign a path to the route.

This will match against the `r.Path`. The path must match exactly and there may be variables in the path.

Variables are designated by "{NAME}", "{NAME:TYPE}", or "{NAME~REGEX}". There are 4 types available:

* segment (default) `[^/]+`

* int `0|-?[1-9]\d*`

* path `.*`

* slug `[a-zA-Z0-9-]+`

The matched variables will be stored in the response writer and can be accessed by calling `router.Vars(w)`.

func (*Router) HandlePrefix

func (self *Router) HandlePrefix(path string, handler server.Handler)

Match against a prefix

the route matches if `r.Path` begins with `path`

func (*Router) HandlePrefixFunc

func (self *Router) HandlePrefixFunc(path string, handler server.HandlerFunc)

Match against a prefix

the route matches if `r.Path` begins with `path`

func (*Router) Handler

func (self *Router) Handler() *Route

Create a new route

func (*Router) ServeGemini

func (self *Router) ServeGemini(w server.ResponseWriter, r *server.Request)

type VariablesResponseWriter

type VariablesResponseWriter struct {
	server.ResponseWriter
	Vars map[string]string
}

func NewVariablesResponseWriter

func NewVariablesResponseWriter(w server.ResponseWriter) *VariablesResponseWriter

Create a new VariablesResponseWriter from a given ResponseWriter

func (*VariablesResponseWriter) Flush

func (self *VariablesResponseWriter) Flush() error

func (*VariablesResponseWriter) Header

func (self *VariablesResponseWriter) Header() *server.Header

func (*VariablesResponseWriter) PutVars

Put variables into the ResponseWriter

func (*VariablesResponseWriter) Unwrap

func (self *VariablesResponseWriter) Unwrap() any

func (*VariablesResponseWriter) Write

func (self *VariablesResponseWriter) Write(data []byte) (int, error)

func (*VariablesResponseWriter) WriteHeader

func (self *VariablesResponseWriter) WriteHeader(status gmitools.StatusCode) error

Jump to

Keyboard shortcuts

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