mirror of
https://github.com/actix/actix-website
synced 2025-06-27 15:39:02 +02:00
update examples to v4 (#258)
This commit is contained in:
@ -12,12 +12,12 @@ App::new().service(
|
||||
web::route()
|
||||
.guard(guard::Get())
|
||||
.guard(guard::Header("content-type", "text/plain"))
|
||||
.to(|| HttpResponse::Ok()),
|
||||
.to(HttpResponse::Ok),
|
||||
),
|
||||
)
|
||||
// </cfg>
|
||||
})
|
||||
.bind("127.0.0.1:8080")?
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -14,10 +14,10 @@ async fn main() -> std::io::Result<()> {
|
||||
.default_service(
|
||||
web::route()
|
||||
.guard(guard::Not(guard::Get()))
|
||||
.to(|| HttpResponse::MethodNotAllowed()),
|
||||
.to(HttpResponse::MethodNotAllowed),
|
||||
)
|
||||
})
|
||||
.bind("127.0.0.1:8080")?
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -1,11 +1,16 @@
|
||||
// <guard>
|
||||
use actix_web::{dev::RequestHead, guard::Guard, http, HttpResponse};
|
||||
use actix_web::{
|
||||
guard::{Guard, GuardContext},
|
||||
http, HttpResponse,
|
||||
};
|
||||
|
||||
struct ContentTypeHeader;
|
||||
|
||||
impl Guard for ContentTypeHeader {
|
||||
fn check(&self, req: &RequestHead) -> bool {
|
||||
req.headers().contains_key(http::header::CONTENT_TYPE)
|
||||
fn check(&self, req: &GuardContext) -> bool {
|
||||
req.head()
|
||||
.headers()
|
||||
.contains_key(http::header::CONTENT_TYPE)
|
||||
}
|
||||
}
|
||||
|
||||
@ -16,12 +21,10 @@ async fn main() -> std::io::Result<()> {
|
||||
HttpServer::new(|| {
|
||||
App::new().route(
|
||||
"/",
|
||||
web::route()
|
||||
.guard(ContentTypeHeader)
|
||||
.to(|| HttpResponse::Ok()),
|
||||
web::route().guard(ContentTypeHeader).to(HttpResponse::Ok),
|
||||
)
|
||||
})
|
||||
.bind("127.0.0.1:8080")?
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -8,10 +8,10 @@ async fn main() -> std::io::Result<()> {
|
||||
"/",
|
||||
web::route()
|
||||
.guard(guard::Not(guard::Get()))
|
||||
.to(|| HttpResponse::MethodNotAllowed()),
|
||||
.to(HttpResponse::MethodNotAllowed),
|
||||
)
|
||||
})
|
||||
.bind("127.0.0.1:8080")?
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -26,7 +26,7 @@ async fn main() -> std::io::Result<()> {
|
||||
.route("/", web::get().to(index))
|
||||
.route("/user", web::post().to(index))
|
||||
})
|
||||
.bind("127.0.0.1:8080")?
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -12,7 +12,7 @@ async fn index(req: HttpRequest) -> Result<String> {
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
HttpServer::new(|| App::new().service(index))
|
||||
.bind("127.0.0.1:8080")?
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -16,7 +16,7 @@ async fn main() -> std::io::Result<()> {
|
||||
.wrap(middleware::NormalizePath::default())
|
||||
.route("/resource/", web::to(index))
|
||||
})
|
||||
.bind("127.0.0.1:8080")?
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
use actix_web::HttpResponse;
|
||||
|
||||
#[get("/resource/")]
|
||||
fn index() -> HttpResponse {
|
||||
async fn index() -> HttpResponse {
|
||||
HttpResponse::Ok().body("Hello")
|
||||
}
|
||||
|
||||
@ -16,7 +16,7 @@ async fn main() -> std::io::Result<()> {
|
||||
.service(index)
|
||||
.default_service(web::route().method(Method::GET))
|
||||
})
|
||||
.bind("127.0.0.1:8080")?
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -10,7 +10,7 @@ async fn index(info: web::Path<(String, u32)>) -> Result<String> {
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
HttpServer::new(|| App::new().service(index))
|
||||
.bind("127.0.0.1:8080")?
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -16,7 +16,7 @@ async fn index(info: web::Path<Info>) -> Result<String> {
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
HttpServer::new(|| App::new().service(index))
|
||||
.bind("127.0.0.1:8080")?
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
// <resource>
|
||||
use actix_web::{guard, web, App, HttpResponse};
|
||||
|
||||
fn index() -> HttpResponse {
|
||||
async fn index() -> HttpResponse {
|
||||
HttpResponse::Ok().body("Hello")
|
||||
}
|
||||
|
||||
@ -12,8 +12,8 @@ pub fn main() {
|
||||
web::resource("/user/{name}")
|
||||
.name("user_detail")
|
||||
.guard(guard::Header("content-type", "application/json"))
|
||||
.route(web::get().to(|| HttpResponse::Ok()))
|
||||
.route(web::put().to(|| HttpResponse::Ok())),
|
||||
.route(web::get().to(HttpResponse::Ok))
|
||||
.route(web::put().to(HttpResponse::Ok)),
|
||||
);
|
||||
}
|
||||
// </resource>
|
||||
|
@ -20,7 +20,7 @@ async fn main() -> std::io::Result<()> {
|
||||
.service(user_detail),
|
||||
)
|
||||
})
|
||||
.bind("127.0.0.1:8080")?
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -6,7 +6,7 @@ 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()
|
||||
url.to_string()
|
||||
}
|
||||
|
||||
#[actix_web::main]
|
||||
@ -16,7 +16,7 @@ async fn main() -> std::io::Result<()> {
|
||||
.service(index)
|
||||
.external_resource("youtube", "https://youtube.com/watch/{video_id}")
|
||||
})
|
||||
.bind("127.0.0.1:8080")?
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -6,7 +6,7 @@ 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())
|
||||
.insert_header((header::LOCATION, url.as_str()))
|
||||
.finish())
|
||||
}
|
||||
|
||||
@ -20,11 +20,11 @@ async fn main() -> std::io::Result<()> {
|
||||
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()),
|
||||
.to(HttpResponse::Ok),
|
||||
)
|
||||
.service(index)
|
||||
})
|
||||
.bind("127.0.0.1:8080")?
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
Reference in New Issue
Block a user