Router
struct Router
An router handler for composing handlers based on the request path.
Implementions
Methods
[src]
pub fn new(): Router {
return Router { tree: Tree.new(), handlers: [] };
}
Creates a new router.
[src]
pub fn new(): Router {
return Router { tree: Tree.new(), handlers: [] };
}
Creates a new router.
Creates a new router.
at
pub fn at(self, path: string, handler: Handler): Router throws RouterError
[src]
pub fn at(self, path: string, handler: Handler): Router throws RouterError {
let id = self.handlers.len();
self.handlers.push(handler);
try self.tree.add(path, id);
return self;
}
Adds a handler for a path.
at
pub fn at(self, path: string, handler: Handler): Router throws RouterError
[src]
pub fn at(self, path: string, handler: Handler): Router throws RouterError {
let id = self.handlers.len();
self.handlers.push(handler);
try self.tree.add(path, id);
return self;
}
Adds a handler for a path.
Adds a handler for a path.
Example
nv
use std.net.http.server.{Router, func_handler, Response, Request, get};
fn get_item(req: Request): Response throws {
return Response.text("Item found!");
}
fn create_item(req: Request): Response throws {
return Response.text("Item created!");
}
fn update_item(req: Request): Response throws {
return Response.text("Item updated!");
}
fn delete_item(req: Request): Response throws {
return Response.text("Item deleted!");
}
fn main() throws {
let router = try Router.new()
.at("/",
get(func_handler(get_item))
.post(func_handler(create_item))
.put(func_handler(update_item))
.delete(func_handler(delete_item))
);
}
Errors
Throws a RouterError
if the path is invalid or already exists.
[src]
pub fn handle(self, request: Request): Response throws {
if (let matches = self.tree.matches(request.uri().path())) {
request.path_params = matches.path_params;
return try self.handlers[matches.value].handle(request);
} else {
throw HttpError {
status: http.NotFound,
source: "router: no match",
};
}
}
[src]
pub fn handle(self, request: Request): Response throws {
if (let matches = self.tree.matches(request.uri().path())) {
request.path_params = matches.path_params;
return try self.handlers[matches.value].handle(request);
} else {
throw HttpError {
status: http.NotFound,
source: "router: no match",
};
}
}