ro

package module
v0.0.0-...-3b9cb1d Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2022 License: BSD-3-Clause Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const MethodWild = "*"

MethodWild wild HTTP method

Variables

View Source
var (

	// MatchedRoutePathParam is the param name under which the path of the matched
	// route is stored, if Router.SaveMatchedRoutePath is set.
	MatchedRoutePathParam = fmt.Sprintf("__matchedRoutePath::%s__", bytes.Rand(make([]byte, 15)))
)

Functions

This section is empty.

Types

type A

type A []any

type Auth

type Auth interface {
	GetID() int
	GetName() string
	Model() any
}

type Ctx

type Ctx struct {
	FastHTTP *fasthttp.RequestCtx

	Auth Auth
}

type Group

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

Group is a sub-router to group paths

func (*Group) Any

func (g *Group) Any(path string, h Handler)

Any is a shortcut for group.Handle(router.MethodWild, path, handler)

WARNING: Use only for routes where the request method is not important

func (*Group) Connect

func (g *Group) Connect(path string, h Handler)

OPTIONS is a shortcut for group.Handle(fasthttp.MethodOptions, path, handler)

func (*Group) Delete

func (g *Group) Delete(path string, h Handler)

Delete is a shortcut for group.Handle(fasthttp.MethodDelete, path, handler)

func (*Group) Get

func (g *Group) Get(path string, h Handler)

Get is a shortcut for group.Handle(fasthttp.MethodGet, path, handler)

func (*Group) Group

func (g *Group) Group(path string) *Group

Group returns a new group. Path auto-correction, including trailing slashes, is enabled by default.

func (*Group) Handle

func (g *Group) Handle(method, path string, h Handler)

Handle registers a new request handler with the given path and method.

For GET, POST, PUT, PATCH and DELETE requests the respective shortcut functions can be used.

This function is intended for bulk loading and to allow the usage of less frequently used, non-standardized or custom methods (e.g. for internal communication with a proxy).

func (*Group) Head

func (g *Group) Head(path string, h Handler)

Head is a shortcut for group.Handle(fasthttp.MethodHead, path, handler)

func (*Group) Options

func (g *Group) Options(path string, h Handler)

Options is a shortcut for group.Handle(fasthttp.MethodOptions, path, handler)

func (*Group) Patch

func (g *Group) Patch(path string, h Handler)

Patch is a shortcut for group.Handle(fasthttp.MethodPatch, path, handler)

func (*Group) Post

func (g *Group) Post(path string, h Handler)

Post is a shortcut for group.Handle(fasthttp.MethodPost, path, handler)

func (*Group) Put

func (g *Group) Put(path string, h Handler)

Put is a shortcut for group.Handle(fasthttp.MethodPut, path, handler)

func (*Group) ServeFiles

func (g *Group) ServeFiles(path string, rootPath string)

ServeFiles serves files from the given file system root. The path must end with "/{filepath:*}", files are then served from the local path /defined/root/dir/{filepath:*}. For example if root is "/etc" and {filepath:*} is "passwd", the local file "/etc/passwd" would be served. Internally a fasthttp.FSHandler is used, therefore http.NotFound is used instead Use:

router.ServeFiles("/src/{filepath:*}", "./")

func (*Group) ServeFilesCustom

func (g *Group) ServeFilesCustom(path string, fs *fasthttp.FS)

ServeFilesCustom serves files from the given file system settings. The path must end with "/{filepath:*}", files are then served from the local path /defined/root/dir/{filepath:*}. For example if root is "/etc" and {filepath:*} is "passwd", the local file "/etc/passwd" would be served. Internally a fasthttp.FSHandler is used, therefore http.NotFound is used instead of the Router's NotFound handler. Use:

router.ServeFilesCustom("/src/{filepath:*}", *customFS)

func (*Group) Trace

func (g *Group) Trace(path string, h Handler)

OPTIONS is a shortcut for group.Handle(fasthttp.MethodOptions, path, handler)

type H

type H map[string]any

type Handler

type Handler = func(c *Ctx) any

type Router

type Router struct {

	// If enabled, adds the matched route path onto the ctx.UserValue context
	// before invoking the handler.
	// The matched route path is only added to handlers of routes that were
	// registered when this option was enabled.
	SaveMatchedRoutePath bool

	// Enables automatic redirection if the current route can't be matched but a
	// handler for the path with (without) the trailing slash exists.
	// For example if /foo/ is requested but a route only exists for /foo, the
	// client is redirected to /foo with http status code 301 for GET requests
	// and 308 for all other request methods.
	RedirectTrailingSlash bool

	// If enabled, the router tries to fix the current request path, if no
	// handle is registered for it.
	// First superfluous path elements like ../ or // are removed.
	// Afterwards the router does a case-insensitive lookup of the cleaned path.
	// If a handle can be found for this route, the router makes a redirection
	// to the corrected path with status code 301 for GET requests and 308 for
	// all other request methods.
	// For example /FOO and /..//Foo could be redirected to /foo.
	// RedirectTrailingSlash is independent of this option.
	RedirectFixedPath bool

	// If enabled, the router checks if another method is allowed for the
	// current route, if the current request can not be routed.
	// If this is the case, the request is answered with 'Method Not Allowed'
	// and HTTP status code 405.
	// If no other Method is allowed, the request is delegated to the NotFound
	// handler.
	HandleMethodNotAllowed bool

	// If enabled, the router automatically replies to OPTIONS requests.
	// Custom OPTIONS handlers take priority over automatic replies.
	HandleOPTIONS bool

	// An optional fasthttp.RequestHandler that is called on automatic OPTIONS requests.
	// The handler is only called if HandleOPTIONS is true and no OPTIONS
	// handler for the specific path was set.
	// The "Allowed" header is set before calling the handler.
	GlobalOPTIONS Handler

	// Configurable fasthttp.RequestHandler which is called when no matching route is
	// found. If it is not set, default NotFound is used.
	NotFound Handler

	// Configurable fasthttp.RequestHandler which is called when a request
	// cannot be routed and HandleMethodNotAllowed is true.
	// If it is not set, ctx.Error with fasthttp.StatusMethodNotAllowed is used.
	// The "Allow" header with allowed request methods is set before the handler
	// is called.
	MethodNotAllowed Handler

	// Function to handle panics recovered from http handlers.
	// It should be used to generate a error page and return the http error code
	// 500 (Internal Server Error).
	// The handler can be used to keep your server from crashing because of
	// unrecovered panics.
	PanicHandler func(*Ctx, interface{})
	// contains filtered or unexported fields
}

Router is a fasthttp.RequestHandler which can be used to dispatch requests to different handler functions via configurable routes

func New

func New() *Router

New returns a new router. Path auto-correction, including trailing slashes, is enabled by default.

func (*Router) Any

func (r *Router) Any(path string, h Handler)

Any is a shortcut for router.Handle(router.MethodWild, path, handler)

WARNING: Use only for routes where the request method is not important

func (*Router) Connect

func (r *Router) Connect(path string, h Handler)

Connect is a shortcut for router.Handle(fasthttp.MethodConnect, path, handler)

func (*Router) Delete

func (r *Router) Delete(path string, h Handler)

Delete is a shortcut for router.Handle(fasthttp.MethodDelete, path, handler)

func (*Router) Get

func (r *Router) Get(path string, h Handler)

Get is a shortcut for router.Handle(fasthttp.MethodGet, path, handler)

func (*Router) Group

func (r *Router) Group(path string) *Group

Group returns a new group. Path auto-correction, including trailing slashes, is enabled by default.

func (*Router) Handle

func (r *Router) Handle(method, path string, h Handler)

Handle registers a new request handler with the given path and method.

For GET, POST, PUT, PATCH and DELETE requests the respective shortcut functions can be used.

This function is intended for bulk loading and to allow the usage of less frequently used, non-standardized or custom methods (e.g. for internal communication with a proxy).

func (*Router) Handler

func (r *Router) Handler(ctx *Ctx)

Handler makes the router implement the http.Handler interface.

func (*Router) Head

func (r *Router) Head(path string, h Handler)

Head is a shortcut for router.Handle(fasthttp.MethodHead, path, handler)

func (*Router) List

func (r *Router) List() map[string][]string

List returns all registered routes grouped by method

func (*Router) Listen

func (r *Router) Listen(addr string) error

func (*Router) Lookup

func (r *Router) Lookup(method, path string, ctx *Ctx) (Handler, bool)

Lookup allows the manual lookup of a method + path combo. This is e.g. useful to build a framework around this router. If the path was found, it returns the handler function. Otherwise the second return value indicates whether a redirection to the same path with an extra / without the trailing slash should be performed.

func (*Router) Mutable

func (r *Router) Mutable(v bool)

Mutable allows updating the route handler

It's disabled by default

WARNING: Use with care. It could generate unexpected behaviours

func (*Router) Options

func (r *Router) Options(path string, h Handler)

Options is a shortcut for router.Handle(fasthttp.MethodOptions, path, handler)

func (*Router) Patch

func (r *Router) Patch(path string, h Handler)

Patch is a shortcut for router.Handle(fasthttp.MethodPatch, path, handler)

func (*Router) Post

func (r *Router) Post(path string, h Handler)

Post is a shortcut for router.Handle(fasthttp.MethodPost, path, handler)

func (*Router) Put

func (r *Router) Put(path string, h Handler)

Put is a shortcut for router.Handle(fasthttp.MethodPut, path, handler)

func (*Router) ServeFiles

func (r *Router) ServeFiles(path string, rootPath string)

ServeFiles serves files from the given file system root. The path must end with "/{filepath:*}", files are then served from the local path /defined/root/dir/{filepath:*}. For example if root is "/etc" and {filepath:*} is "passwd", the local file "/etc/passwd" would be served. Internally a fasthttp.FSHandler is used, therefore fasthttp.NotFound is used instead Use:

router.ServeFiles("/src/{filepath:*}", "./")

func (*Router) ServeFilesCustom

func (r *Router) ServeFilesCustom(path string, fs *fasthttp.FS)

ServeFilesCustom serves files from the given file system settings. The path must end with "/{filepath:*}", files are then served from the local path /defined/root/dir/{filepath:*}. For example if root is "/etc" and {filepath:*} is "passwd", the local file "/etc/passwd" would be served. Internally a fasthttp.FSHandler is used, therefore http.NotFound is used instead of the Router's NotFound handler. Use:

router.ServeFilesCustom("/src/{filepath:*}", *customFS)

func (*Router) Trace

func (r *Router) Trace(path string, h Handler)

Trace is a shortcut for router.Handle(fasthttp.MethodTrace, path, handler)

type Tree

type Tree struct {

	// If enabled, the node handler could be updated
	Mutable bool
	// contains filtered or unexported fields
}

Tree is a routes storage

func (*Tree) Add

func (t *Tree) Add(path string, h Handler)

Add adds a node with the given handle to the path.

WARNING: Not concurrency-safe!

func (*Tree) FindCaseInsensitivePath

func (t *Tree) FindCaseInsensitivePath(path string, fixTrailingSlash bool, buf *bytebufferpool.ByteBuffer) bool

FindCaseInsensitivePath makes a case-insensitive lookup of the given path and tries to find a handler. It can optionally also fix trailing slashes. It returns the case-corrected path and a bool indicating whether the lookup was successful.

func (*Tree) Get

func (t *Tree) Get(path string, ctx *Ctx) (Handler, bool)

Get returns the handle registered with the given path (key). The values of param/wildcard are saved as ctx.UserValue. If no handle can be found, a TSR (trailing slash redirect) recommendation is made if a handle exists with an extra (without the) trailing slash for the given path.

Jump to

Keyboard shortcuts

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