1
0
mirror of https://github.com/actix/actix-website synced 2025-02-08 22:36:07 +01:00

32 lines
876 B
Rust
Raw Normal View History

2018-05-24 10:13:55 -07:00
// <url>
2020-09-12 16:21:54 +01:00
use actix_web::{get, guard, http::header, HttpRequest, HttpResponse, Result};
2018-05-24 10:13:55 -07:00
2020-09-12 16:21:54 +01:00
#[get("/test/")]
2019-12-29 04:10:02 +09:00
async fn index(req: HttpRequest) -> Result<HttpResponse> {
2023-03-13 17:59:23 +00:00
let url = req.url_for("foo", ["1", "2", "3"])?; // <- generate url for "foo" resource
2019-06-25 18:20:36 -04:00
2018-05-24 10:13:55 -07:00
Ok(HttpResponse::Found()
2022-02-26 03:56:24 +00:00
.insert_header((header::LOCATION, url.as_str()))
2018-05-24 10:13:55 -07:00
.finish())
}
2020-09-12 16:21:54 +01:00
#[actix_web::main]
2019-12-29 04:10:02 +09:00
async fn main() -> std::io::Result<()> {
2019-06-28 13:31:30 -04:00
use actix_web::{web, App, HttpServer};
2019-06-25 18:20:36 -04:00
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())
2022-02-26 03:56:24 +00:00
.to(HttpResponse::Ok),
2019-06-25 18:20:36 -04:00
)
2020-09-12 16:21:54 +01:00
.service(index)
2019-06-25 18:20:36 -04:00
})
2022-02-26 03:56:24 +00:00
.bind(("127.0.0.1", 8080))?
2019-06-25 18:20:36 -04:00
.run()
2019-12-29 04:10:02 +09:00
.await
2018-05-24 10:13:55 -07:00
}
// </url>