1
0
mirror of https://github.com/fafhrd91/actix-web synced 2025-06-25 06:39:22 +02:00

HttpRequest::resource() returns current matched resource

This commit is contained in:
Nikolay Kim
2018-04-01 17:37:22 -07:00
parent b2e771df2c
commit 17c27ef42d
11 changed files with 191 additions and 106 deletions

View File

@ -34,16 +34,14 @@ use db::{CreateUser, DbExecutor};
/// State with DbExecutor address
struct State {
struct App {
db: Addr<Syn, DbExecutor>,
}
/// Async request handler
fn index(req: HttpRequest<State>) -> Box<Future<Item=HttpResponse, Error=Error>> {
let name = &req.match_info()["name"];
fn index(name: Path<(String,)>, state: State<App>) -> FutureResponse<HttpResponse> {
// send async `CreateUser` message to a `DbExecutor`
req.state().db.send(CreateUser{name: name.to_owned()})
state.db.send(CreateUser{name: name.into_inner()})
.from_err()
.and_then(|res| {
match res {
@ -72,7 +70,7 @@ fn main() {
App::with_state(State{db: addr.clone()})
// enable logger
.middleware(middleware::Logger::default())
.resource("/{name}", |r| r.method(http::Method::GET).a(index))})
.resource("/{name}", |r| r.method(http::Method::GET).with2(index))})
.bind("127.0.0.1:8080").unwrap()
.start();