1
0
mirror of https://github.com/actix/actix-website synced 2024-12-04 20:51:54 +01:00
actix-website/examples/url-dispatch/src/urls.rs
2019-12-29 04:10:02 +09:00

31 lines
870 B
Rust

// <url>
use actix_web::{guard, http::header, HttpRequest, HttpResponse, Result};
async fn index(req: HttpRequest) -> Result<HttpResponse> {
let url = req.url_for("foo", &["1", "2", "3"])?; // <- generate url for "foo" resource
Ok(HttpResponse::Found()
.header(header::LOCATION, url.as_str())
.finish())
}
#[actix_rt::main]
async fn main() -> std::io::Result<()> {
use actix_web::{web, App, HttpServer};
HttpServer::new(|| {
App::new()
.service(
web::resource("/test/{a}/{b}/{c}")
.name("foo") // <- set resource name, then it could be used in `url_for`
.guard(guard::Get())
.to(|| HttpResponse::Ok()),
)
.route("/test/", web::get().to(index))
})
.bind("127.0.0.1:8088")?
.run()
.await
}
// </url>