1
0
mirror of https://github.com/actix/actix-website synced 2025-02-08 22:36:07 +01:00
2023-03-13 17:59:23 +00:00

32 lines
876 B
Rust

// <url>
use actix_web::{get, guard, http::header, HttpRequest, HttpResponse, Result};
#[get("/test/")]
async fn index(req: HttpRequest) -> Result<HttpResponse> {
let url = req.url_for("foo", ["1", "2", "3"])?; // <- generate url for "foo" resource
Ok(HttpResponse::Found()
.insert_header((header::LOCATION, url.as_str()))
.finish())
}
#[actix_web::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),
)
.service(index)
})
.bind(("127.0.0.1", 8080))?
.run()
.await
}
// </url>