1
0
mirror of https://github.com/actix/actix-website synced 2024-12-03 20:22:13 +01:00
actix-website/examples/url-dispatch/src/url_ext.rs

24 lines
584 B
Rust
Raw Normal View History

2018-05-24 19:13:55 +02:00
// <ext>
2020-09-12 17:21:54 +02:00
use actix_web::{get, App, HttpRequest, HttpServer, Responder};
2018-05-24 19:13:55 +02:00
2020-09-12 17:21:54 +02:00
#[get("/")]
2019-12-28 20:10:02 +01:00
async fn index(req: HttpRequest) -> impl Responder {
2023-03-13 18:59:23 +01:00
let url = req.url_for("youtube", ["oHg5SJYRHA0"]).unwrap();
2018-05-24 19:13:55 +02:00
assert_eq!(url.as_str(), "https://youtube.com/watch/oHg5SJYRHA0");
2019-06-17 10:12:11 +02:00
2022-02-26 04:56:24 +01:00
url.to_string()
2018-05-24 19:13:55 +02:00
}
2020-09-12 17:21:54 +02:00
#[actix_web::main]
2019-12-28 20:10:02 +01:00
async fn main() -> std::io::Result<()> {
2019-06-26 00:20:36 +02:00
HttpServer::new(|| {
App::new()
2020-09-12 17:21:54 +02:00
.service(index)
2019-06-26 00:20:36 +02:00
.external_resource("youtube", "https://youtube.com/watch/{video_id}")
})
2022-02-26 04:56:24 +01:00
.bind(("127.0.0.1", 8080))?
2019-06-26 00:20:36 +02:00
.run()
2019-12-28 20:10:02 +01:00
.await
2018-05-24 19:13:55 +02:00
}
// </ext>