Skip to content

router #

fn extract_route_path #

fn extract_route_path(path string) !(string, string, string)

extract_route_path returns the name, the parameter name (if present), and the remaining children route paths

fn get_ctx #

fn get_ctx(r &ctx.Req) voidptr

get_ctx accesses the global context

fn new #

fn new() Router

type GroupCallbackFn #

type GroupCallbackFn = fn (mut group map[string]&Route)

fn (map[string]&Route) route #

fn (mut routes map[string]&Route) route(method Method, path string, handlers ...ctx.HandlerFunc)

route creates a new route based on the given method, path, and the handlers. See router.Method for the list of available methods.

fn (map[string]&Route) group #

fn (mut routes map[string]&Route) group(path string, callback GroupCallbackFn)

group adds a series of routes into the desired prefix

fn (map[string]&Route) use #

fn (mut routes map[string]&Route) use(middlewares ...ctx.MiddlewareFunc)

enum Method #

enum Method {
	get
	post
	patch
	put
	delete
	options
}

List of supported HTTP methods.

struct Route #

struct Route {
	name       string
	param_name string
	method     Method
	kind       Kind
mut:
	children    map[string]&Route
	methods     map[string][]ctx.HandlerFunc
	middlewares []ctx.MiddlewareFunc
}

fn (Route) str #

fn (r &Route) str() string

empty str to avoid cgen error

fn (Route) find #

fn (routes map[string]&Route) find(method string, path string) !(map[string]string, []ctx.MiddlewareFunc, []ctx.HandlerFunc)

find searches the matching route and returns the injected params data and the route handlers.

struct Router #

struct Router {
pub mut:
	on_error ctx.HandlerFunc = ctx.error_route
mut:
	routes      map[string]&Route
	middlewares []ctx.MiddlewareFunc
	ctx         context.Context = context.todo() // TODO: remove in the near future
}

fn (Router) receive #

fn (r Router) receive(method string, path string, raw_headers []string, body []u8) (int, []u8, []u8)

fn (Router) respond_error #

fn (r Router) respond_error(code int) []u8

fn (Router) inject #

deprecated: use Router.inject_context() or Req.ctx instead to inject value. As for accessing value, use router.get_ctx() instead of accessing r.ctx directly.
fn (mut r Router) inject(data voidptr)

fn (Router) inject_context #

fn (mut r Router) inject_context(ctx_ context.Context)

inject_context injects the context to the handler context.

Todo: to be removed in the future. must be in a form of a middleware instead.

fn (Router) route #

fn (mut r Router) route(method Method, path string, handlers ...ctx.HandlerFunc)

route is a shortcut method to r.routes.route method

fn (Router) group #

fn (mut r Router) group(prefix string, callback GroupCallbackFn)

group is a shortcut method to r.routes.group method

fn (Router) use #

fn (mut r Router) use(handlers ...ctx.MiddlewareFunc)

use registers handlers as app-wide middlewares