1
0
mirror of https://github.com/actix/actix-website synced 2025-06-27 15:39:02 +02:00

Update url-dispatch

This commit is contained in:
Yuki Okushi
2019-12-29 04:10:02 +09:00
parent 1d6ccff3e8
commit 7a973d6fe9
16 changed files with 90 additions and 89 deletions

View File

@ -1,7 +1,8 @@
use actix_web::{guard, web, App, HttpResponse};
#[rustfmt::skip]
pub fn main() {
#[actix_rt::main]
async fn main() -> std::io::Result<()> {
use actix_web::HttpServer;
HttpServer::new(|| {
@ -16,8 +17,7 @@ App::new().service(
)
// </cfg>
})
.bind("127.0.0.1:8088")
.unwrap()
.bind("127.0.0.1:8088")?
.run()
.unwrap();
.await
}

View File

@ -1,11 +1,12 @@
use actix_web::{guard, web, App, HttpRequest, HttpResponse, HttpServer, Responder};
fn index(_req: HttpRequest) -> impl Responder {
async fn index(_req: HttpRequest) -> impl Responder {
"Welcome!"
}
// <default>
pub fn main() {
#[actix_rt::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| {
App::new()
.service(web::resource("/").route(web::get().to(index)))
@ -15,9 +16,8 @@ pub fn main() {
.to(|| HttpResponse::MethodNotAllowed()),
)
})
.bind("127.0.0.1:8088")
.unwrap()
.bind("127.0.0.1:8088")?
.run()
.unwrap();
.await
}
// </default>

View File

@ -9,7 +9,8 @@ impl Guard for ContentTypeHeader {
}
}
pub fn main() {
#[actix_rt::main]
async fn main() -> std::io::Result<()> {
use actix_web::{web, App, HttpServer};
HttpServer::new(|| {
@ -20,9 +21,8 @@ pub fn main() {
.to(|| HttpResponse::Ok()),
)
})
.bind("127.0.0.1:8088")
.unwrap()
.bind("127.0.0.1:8088")?
.run()
.unwrap();
.await
}
// </guard>

View File

@ -1,7 +1,8 @@
// <guard2>
use actix_web::{guard, web, App, HttpResponse, HttpServer};
pub fn main() {
#[actix_rt::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| {
App::new().route(
"/",
@ -10,9 +11,8 @@ pub fn main() {
.to(|| HttpResponse::MethodNotAllowed()),
)
})
.bind("127.0.0.1:8088")
.unwrap()
.bind("127.0.0.1:8088")?
.run()
.unwrap();
.await
}
// </guard2>

View File

@ -16,19 +16,19 @@ pub mod urls;
// <main>
use actix_web::{web, App, HttpResponse, HttpServer};
fn index() -> HttpResponse {
async fn index() -> HttpResponse {
HttpResponse::Ok().body("Hello")
}
fn main() {
#[actix_rt::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| {
App::new()
.route("/", web::get().to(index))
.route("/user", web::post().to(index))
})
.bind("127.0.0.1:8088")
.unwrap()
.bind("127.0.0.1:8088")?
.run()
.unwrap();
.await
}
// </main>

View File

@ -1,14 +1,15 @@
// <minfo>
use actix_web::{HttpRequest, HttpResponse, Result};
fn index(req: HttpRequest) -> Result<String> {
async fn index(req: HttpRequest) -> Result<String> {
let v1: u8 = req.match_info().get("v1").unwrap().parse().unwrap();
let v2: u8 = req.match_info().query("v2").parse().unwrap();
let (v3, v4): (u8, u8) = req.match_info().load().unwrap();
Ok(format!("Values {} {} {} {}", v1, v2, v3, v4))
}
pub fn main() {
#[actix_rt::main]
async fn main() -> std::io::Result<()> {
use actix_web::{web, App, HttpServer};
HttpServer::new(|| {
@ -16,9 +17,8 @@ pub fn main() {
.route("/a/{v1}/{v2}/", web::get().to(index))
.route("", web::get().to(|| HttpResponse::Ok()))
})
.bind("127.0.0.1:8088")
.unwrap()
.bind("127.0.0.1:8088")?
.run()
.unwrap();
.await
}
// </minfo>

View File

@ -1,11 +1,12 @@
// <norm>
use actix_web::{middleware, HttpResponse};
fn index() -> HttpResponse {
async fn index() -> HttpResponse {
HttpResponse::Ok().body("Hello")
}
pub fn main() {
#[actix_rt::main]
async fn main() -> std::io::Result<()> {
use actix_web::{web, App, HttpServer};
HttpServer::new(|| {
@ -13,9 +14,8 @@ pub fn main() {
.wrap(middleware::NormalizePath)
.route("/resource/", web::to(index))
})
.bind("127.0.0.1:8088")
.unwrap()
.bind("127.0.0.1:8088")?
.run()
.unwrap();
.await
}
// </norm>

View File

@ -7,16 +7,16 @@ fn index() -> HttpResponse {
// <norm>
use actix_web::{http::Method, middleware, web, App, HttpServer};
pub fn main() {
#[actix_rt::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| {
App::new()
.wrap(middleware::NormalizePath)
.route("/resource/", web::get().to(index))
.default_service(web::route().method(Method::GET))
})
.bind("127.0.0.1:8088")
.unwrap()
.bind("127.0.0.1:8088")?
.run()
.unwrap();
.await
}
// </norm>

View File

@ -1,11 +1,12 @@
// <path>
use actix_web::{web, Result};
fn index(info: web::Path<(String, u32)>) -> Result<String> {
async fn index(info: web::Path<(String, u32)>) -> Result<String> {
Ok(format!("Welcome {}! id: {}", info.0, info.1))
}
pub fn main() {
#[actix_rt::main]
async fn main() -> std::io::Result<()> {
use actix_web::{App, HttpServer};
HttpServer::new(|| {
@ -14,9 +15,8 @@ pub fn main() {
web::get().to(index),
)
})
.bind("127.0.0.1:8088")
.unwrap()
.bind("127.0.0.1:8088")?
.run()
.unwrap();
.await
}
// </path>

View File

@ -8,11 +8,12 @@ struct Info {
}
// extract path info using serde
fn index(info: web::Path<Info>) -> Result<String> {
async fn index(info: web::Path<Info>) -> Result<String> {
Ok(format!("Welcome {}!", info.username))
}
pub fn main() {
#[actix_rt::main]
async fn main() -> std::io::Result<()> {
use actix_web::{App, HttpServer};
HttpServer::new(|| {
@ -21,9 +22,8 @@ pub fn main() {
web::get().to(index),
)
})
.bind("127.0.0.1:8088")
.unwrap()
.bind("127.0.0.1:8088")?
.run()
.unwrap();
.await
}
// </path>

View File

@ -2,18 +2,18 @@
use actix_web::{HttpRequest, Result};
use std::path::PathBuf;
fn index(req: HttpRequest) -> Result<String> {
async fn index(req: HttpRequest) -> Result<String> {
let path: PathBuf = req.match_info().query("tail").parse().unwrap();
Ok(format!("Path {:?}", path))
}
pub fn main() {
#[actix_rt::main]
async fn main() -> std::io::Result<()> {
use actix_web::{web, App, HttpServer};
HttpServer::new(|| App::new().route(r"/a/{tail:.*}", web::get().to(index)))
.bind("127.0.0.1:8088")
.unwrap()
.bind("127.0.0.1:8088")?
.run()
.unwrap();
.await
}
// </pbuf>

View File

@ -1,15 +1,16 @@
use actix_web::{web, App, HttpResponse, HttpServer};
// <scope>
fn show_users() -> HttpResponse {
async fn show_users() -> HttpResponse {
HttpResponse::Ok().body("Show users")
}
fn user_detail(path: web::Path<(u32,)>) -> HttpResponse {
async fn user_detail(path: web::Path<(u32,)>) -> HttpResponse {
HttpResponse::Ok().body(format!("User detail: {}", path.0))
}
pub fn main() {
#[actix_rt::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| {
App::new().service(
web::scope("/users")
@ -17,9 +18,8 @@ pub fn main() {
.route("/show/{id}", web::get().to(user_detail)),
)
})
.bind("127.0.0.1:8088")
.unwrap()
.bind("127.0.0.1:8088")?
.run()
.unwrap();
.await
}
// </scope>

View File

@ -1,14 +1,15 @@
// <ext>
use actix_web::{HttpRequest, Responder};
fn index(req: HttpRequest) -> impl Responder {
async fn index(req: HttpRequest) -> impl Responder {
let url = req.url_for("youtube", &["oHg5SJYRHA0"]).unwrap();
assert_eq!(url.as_str(), "https://youtube.com/watch/oHg5SJYRHA0");
url.into_string()
}
pub fn main() {
#[actix_rt::main]
async fn main() -> std::io::Result<()> {
use actix_web::{web, App, HttpServer};
HttpServer::new(|| {
@ -17,9 +18,8 @@ pub fn main() {
.external_resource("youtube", "https://youtube.com/watch/{video_id}")
.route("/", actix_web::web::get().to(index))
})
.bind("127.0.0.1:8088")
.unwrap()
.bind("127.0.0.1:8088")?
.run()
.unwrap();
.await
}
// </ext>

View File

@ -1,7 +1,7 @@
// <url>
use actix_web::{guard, http::header, HttpRequest, HttpResponse, Result};
fn index(req: HttpRequest) -> Result<HttpResponse> {
async fn index(req: HttpRequest) -> Result<HttpResponse> {
let url = req.url_for("foo", &["1", "2", "3"])?; // <- generate url for "foo" resource
Ok(HttpResponse::Found()
@ -9,7 +9,8 @@ fn index(req: HttpRequest) -> Result<HttpResponse> {
.finish())
}
pub fn main() {
#[actix_rt::main]
async fn main() -> std::io::Result<()> {
use actix_web::{web, App, HttpServer};
HttpServer::new(|| {
@ -22,9 +23,8 @@ pub fn main() {
)
.route("/test/", web::get().to(index))
})
.bind("127.0.0.1:8088")
.unwrap()
.bind("127.0.0.1:8088")?
.run()
.unwrap();
.await
}
// </url>