mirror of
https://github.com/actix/actix-website
synced 2025-06-27 15:39:02 +02:00
URL-Dispatch is done-ish.
This commit is contained in:
@ -1,14 +1,23 @@
|
||||
// <cfg>
|
||||
use actix_web::{guard, web, App, HttpResponse};
|
||||
|
||||
#[rustfmt::skip]
|
||||
pub fn main() {
|
||||
App::new().service(
|
||||
web::resource("/path").route(
|
||||
web::route()
|
||||
.guard(guard::Get())
|
||||
.guard(guard::Header("content-type", "text/plain"))
|
||||
.to(|| HttpResponse::Ok()),
|
||||
),
|
||||
);
|
||||
}
|
||||
use actix_web::HttpServer;
|
||||
|
||||
HttpServer::new(|| {
|
||||
// <cfg>
|
||||
App::new().service(
|
||||
web::resource("/path").route(
|
||||
web::route()
|
||||
.guard(guard::Get())
|
||||
.guard(guard::Header("content-type", "text/plain"))
|
||||
.to(|| HttpResponse::Ok()),
|
||||
),
|
||||
)
|
||||
// </cfg>
|
||||
})
|
||||
.bind("127.0.0.1:8088")
|
||||
.unwrap()
|
||||
.run()
|
||||
.unwrap();
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
use actix_web::{guard, web, App, HttpRequest, HttpResponse, Responder};
|
||||
use actix_web::{guard, web, App, HttpRequest, HttpResponse, HttpServer, Responder};
|
||||
|
||||
fn index(_req: HttpRequest) -> impl Responder {
|
||||
"Welcome!"
|
||||
@ -6,12 +6,18 @@ fn index(_req: HttpRequest) -> impl Responder {
|
||||
|
||||
// <default>
|
||||
pub fn main() {
|
||||
App::new()
|
||||
.service(web::resource("/").route(web::get().to(index)))
|
||||
.default_service(
|
||||
web::route()
|
||||
.guard(guard::Not(guard::Get()))
|
||||
.to(|| HttpResponse::MethodNotAllowed()),
|
||||
);
|
||||
HttpServer::new(|| {
|
||||
App::new()
|
||||
.service(web::resource("/").route(web::get().to(index)))
|
||||
.default_service(
|
||||
web::route()
|
||||
.guard(guard::Not(guard::Get()))
|
||||
.to(|| HttpResponse::MethodNotAllowed()),
|
||||
)
|
||||
})
|
||||
.bind("127.0.0.1:8088")
|
||||
.unwrap()
|
||||
.run()
|
||||
.unwrap();
|
||||
}
|
||||
// </default>
|
||||
|
28
examples/url-dispatch/src/guard.rs
Normal file
28
examples/url-dispatch/src/guard.rs
Normal file
@ -0,0 +1,28 @@
|
||||
// <guard>
|
||||
use actix_web::{
|
||||
dev::RequestHead, guard::Guard, http, web, App, HttpResponse, HttpServer,
|
||||
};
|
||||
|
||||
struct ContentTypeHeader;
|
||||
|
||||
impl Guard for ContentTypeHeader {
|
||||
fn check(&self, req: &RequestHead) -> bool {
|
||||
req.headers().contains_key(http::header::CONTENT_TYPE)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn main() {
|
||||
HttpServer::new(|| {
|
||||
App::new().route(
|
||||
"/",
|
||||
web::route()
|
||||
.guard(ContentTypeHeader)
|
||||
.to(|| HttpResponse::Ok()),
|
||||
)
|
||||
})
|
||||
.bind("127.0.0.1:8088")
|
||||
.unwrap()
|
||||
.run()
|
||||
.unwrap();
|
||||
}
|
||||
// </guard>
|
18
examples/url-dispatch/src/guard2.rs
Normal file
18
examples/url-dispatch/src/guard2.rs
Normal file
@ -0,0 +1,18 @@
|
||||
// <guard2>
|
||||
use actix_web::{guard, web, App, HttpResponse, HttpServer};
|
||||
|
||||
pub fn main() {
|
||||
HttpServer::new(|| {
|
||||
App::new().route(
|
||||
"/",
|
||||
web::route()
|
||||
.guard(guard::Not(guard::Get()))
|
||||
.to(|| HttpResponse::MethodNotAllowed()),
|
||||
)
|
||||
})
|
||||
.bind("127.0.0.1:8088")
|
||||
.unwrap()
|
||||
.run()
|
||||
.unwrap();
|
||||
}
|
||||
// </guard2>
|
@ -1,28 +1,34 @@
|
||||
pub mod cfg;
|
||||
pub mod dhandler;
|
||||
pub mod guard;
|
||||
pub mod guard2;
|
||||
pub mod minfo;
|
||||
pub mod norm;
|
||||
pub mod norm2;
|
||||
pub mod path;
|
||||
pub mod path2;
|
||||
pub mod pbuf;
|
||||
pub mod pred;
|
||||
pub mod pred2;
|
||||
pub mod resource;
|
||||
pub mod scope;
|
||||
pub mod url_ext;
|
||||
pub mod urls;
|
||||
|
||||
// <main>
|
||||
use actix_web::{web, App, HttpRequest, HttpResponse};
|
||||
use actix_web::{web, App, HttpResponse, HttpServer};
|
||||
|
||||
fn index(_req: HttpRequest) -> HttpResponse {
|
||||
unimplemented!()
|
||||
fn index() -> HttpResponse {
|
||||
HttpResponse::Ok().body("Hello")
|
||||
}
|
||||
|
||||
fn main() {
|
||||
App::new()
|
||||
.route("/user/{name}", web::get().to(index))
|
||||
.route("/user/{name}", web::post().to(index));
|
||||
HttpServer::new(|| {
|
||||
App::new()
|
||||
.route("/", web::get().to(index))
|
||||
.route("/user", web::post().to(index))
|
||||
})
|
||||
.bind("127.0.0.1:8088")
|
||||
.unwrap()
|
||||
.run()
|
||||
.unwrap();
|
||||
}
|
||||
// </main>
|
||||
|
@ -9,8 +9,16 @@ fn index(req: HttpRequest) -> Result<String> {
|
||||
}
|
||||
|
||||
pub fn main() {
|
||||
App::new()
|
||||
.route("/a/{v1}/{v2}/", web::get().to(index))
|
||||
.route("", web::get().to(|| actix_web::HttpResponse::Ok()));
|
||||
use actix_web::HttpServer;
|
||||
|
||||
HttpServer::new(|| {
|
||||
App::new()
|
||||
.route("/a/{v1}/{v2}/", web::get().to(index))
|
||||
.route("", web::get().to(|| actix_web::HttpResponse::Ok()))
|
||||
})
|
||||
.bind("127.0.0.1:8088")
|
||||
.unwrap()
|
||||
.run()
|
||||
.unwrap();
|
||||
}
|
||||
// </minfo>
|
||||
|
@ -1,14 +1,21 @@
|
||||
// <norm>
|
||||
use actix_web::{middleware, web, App};
|
||||
use actix_web::{middleware, web, App, HttpResponse};
|
||||
|
||||
fn index() -> HttpResponse {
|
||||
HttpResponse::Ok().body("Hello")
|
||||
}
|
||||
|
||||
pub fn main() {
|
||||
App::new()
|
||||
.wrap(middleware::NormalizePath)
|
||||
.route("/", web::get().to(index));
|
||||
use actix_web::HttpServer;
|
||||
|
||||
HttpServer::new(|| {
|
||||
App::new()
|
||||
.wrap(middleware::NormalizePath)
|
||||
.route("/resource/", web::to(index))
|
||||
})
|
||||
.bind("127.0.0.1:8088")
|
||||
.unwrap()
|
||||
.run()
|
||||
.unwrap();
|
||||
}
|
||||
// </norm>
|
||||
|
||||
use actix_web::HttpRequest;
|
||||
fn index(_req: HttpRequest) -> String {
|
||||
unimplemented!()
|
||||
}
|
||||
|
@ -1,16 +1,22 @@
|
||||
// <norm>
|
||||
use actix_web::{http::Method, middleware, web, App};
|
||||
use actix_web::{http::Method, middleware, web, App, HttpServer};
|
||||
|
||||
pub fn main() {
|
||||
App::new()
|
||||
.wrap(middleware::NormalizePath)
|
||||
.route("/resource/", web::get().to(index))
|
||||
.default_service(web::route().method(Method::GET));
|
||||
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()
|
||||
.run()
|
||||
.unwrap();
|
||||
}
|
||||
// </norm>
|
||||
|
||||
use actix_web::HttpRequest;
|
||||
use actix_web::HttpResponse;
|
||||
|
||||
fn index(_req: HttpRequest) -> String {
|
||||
unimplemented!()
|
||||
fn index() -> HttpResponse {
|
||||
HttpResponse::Ok().body("Hello")
|
||||
}
|
||||
|
@ -1,15 +1,22 @@
|
||||
// <path>
|
||||
use actix_web::{web, App, Result};
|
||||
|
||||
// extract path info using serde
|
||||
fn index(info: web::Path<(String, u32)>) -> Result<String> {
|
||||
Ok(format!("Welcome {}! id: {}", info.0, info.1))
|
||||
}
|
||||
|
||||
pub fn main() {
|
||||
App::new().route(
|
||||
"/{username}/{id}/index.html", // <- define path parameters
|
||||
web::get().to(index),
|
||||
);
|
||||
use actix_web::HttpServer;
|
||||
|
||||
HttpServer::new(|| {
|
||||
App::new().route(
|
||||
"/{username}/{id}/index.html", // <- define path parameters
|
||||
web::get().to(index),
|
||||
)
|
||||
})
|
||||
.bind("127.0.0.1:8088")
|
||||
.unwrap()
|
||||
.run()
|
||||
.unwrap();
|
||||
}
|
||||
// </path>
|
||||
|
@ -13,9 +13,17 @@ fn index(info: web::Path<Info>) -> Result<String> {
|
||||
}
|
||||
|
||||
pub fn main() {
|
||||
App::new().route(
|
||||
"/{username}/index.html", // <- define path parameters
|
||||
web::get().to(index),
|
||||
);
|
||||
use actix_web::HttpServer;
|
||||
|
||||
HttpServer::new(|| {
|
||||
App::new().route(
|
||||
"/{username}/index.html", // <- define path parameters
|
||||
web::get().to(index),
|
||||
)
|
||||
})
|
||||
.bind("127.0.0.1:8088")
|
||||
.unwrap()
|
||||
.run()
|
||||
.unwrap();
|
||||
}
|
||||
// </path>
|
||||
|
@ -8,6 +8,12 @@ fn index(req: HttpRequest) -> Result<String> {
|
||||
}
|
||||
|
||||
pub fn main() {
|
||||
App::new().route(r"/a/{tail:.*}", web::get().to(index));
|
||||
use actix_web::HttpServer;
|
||||
|
||||
HttpServer::new(|| App::new().route(r"/a/{tail:.*}", web::get().to(index)))
|
||||
.bind("127.0.0.1:8088")
|
||||
.unwrap()
|
||||
.run()
|
||||
.unwrap();
|
||||
}
|
||||
// </pbuf>
|
||||
|
@ -1,20 +0,0 @@
|
||||
// <pred>
|
||||
use actix_web::{dev::RequestHead, guard::Guard, http, web, App, HttpResponse};
|
||||
|
||||
struct ContentTypeHeader;
|
||||
|
||||
impl Guard for ContentTypeHeader {
|
||||
fn check(&self, req: &RequestHead) -> bool {
|
||||
req.headers().contains_key(http::header::CONTENT_TYPE)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn main() {
|
||||
App::new().route(
|
||||
"",
|
||||
web::route()
|
||||
.guard(ContentTypeHeader)
|
||||
.to(|| HttpResponse::Ok()),
|
||||
);
|
||||
}
|
||||
// </pred>
|
@ -1,12 +0,0 @@
|
||||
// <pred>
|
||||
use actix_web::{guard, web, App, HttpResponse};
|
||||
|
||||
pub fn main() {
|
||||
App::new().route(
|
||||
"/",
|
||||
web::route()
|
||||
.guard(guard::Not(guard::Get()))
|
||||
.to(|| HttpResponse::MethodNotAllowed()),
|
||||
);
|
||||
}
|
||||
// </pred>
|
@ -1,15 +1,19 @@
|
||||
// <resource>
|
||||
use actix_web::{web, App, HttpRequest, HttpResponse};
|
||||
use actix_web::{guard, web, App, HttpResponse};
|
||||
|
||||
fn index(_req: HttpRequest) -> HttpResponse {
|
||||
unimplemented!()
|
||||
fn index() -> HttpResponse {
|
||||
HttpResponse::Ok().body("Hello")
|
||||
}
|
||||
|
||||
pub fn main() {
|
||||
App::new()
|
||||
.service(web::resource("/prefix").to(index))
|
||||
.service(
|
||||
web::resource("/user/{name}").route(web::get().to(|| HttpResponse::Ok())),
|
||||
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())),
|
||||
);
|
||||
}
|
||||
// </resource>
|
||||
|
@ -1,11 +1,25 @@
|
||||
use actix_web::{web, App, HttpRequest, HttpResponse};
|
||||
use actix_web::{web, App, HttpResponse, HttpServer};
|
||||
|
||||
// <scope>
|
||||
fn show_users(_req: HttpRequest) -> HttpResponse {
|
||||
unimplemented!()
|
||||
fn show_users() -> HttpResponse {
|
||||
HttpResponse::Ok().body("Show users")
|
||||
}
|
||||
|
||||
fn user_detail(_path: web::Path<(u32,)>) -> HttpResponse {
|
||||
HttpResponse::Ok().body("User detail")
|
||||
}
|
||||
|
||||
pub fn main() {
|
||||
App::new().service(web::scope("/users").route("/show", web::get().to(show_users)));
|
||||
HttpServer::new(|| {
|
||||
App::new().service(
|
||||
web::scope("/users")
|
||||
.route("/show", web::get().to(show_users))
|
||||
.route("/show/{id}", web::get().to(user_detail)),
|
||||
)
|
||||
})
|
||||
.bind("127.0.0.1:8088")
|
||||
.unwrap()
|
||||
.run()
|
||||
.unwrap();
|
||||
}
|
||||
// </scope>
|
||||
|
@ -9,9 +9,17 @@ fn index(req: HttpRequest) -> impl Responder {
|
||||
}
|
||||
|
||||
pub fn main() {
|
||||
App::new()
|
||||
.route("/index.html", web::get().to(index))
|
||||
.external_resource("youtube", "https://youtube.com/watch/{video_id}")
|
||||
.route("/", actix_web::web::get().to(index));
|
||||
use actix_web::HttpServer;
|
||||
|
||||
HttpServer::new(|| {
|
||||
App::new()
|
||||
.route("/", web::get().to(index))
|
||||
.external_resource("youtube", "https://youtube.com/watch/{video_id}")
|
||||
.route("/", actix_web::web::get().to(index))
|
||||
})
|
||||
.bind("127.0.0.1:8088")
|
||||
.unwrap()
|
||||
.run()
|
||||
.unwrap();
|
||||
}
|
||||
// </ext>
|
||||
|
@ -3,19 +3,28 @@ use actix_web::{guard, http::header, web, App, HttpRequest, HttpResponse, Result
|
||||
|
||||
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())
|
||||
.finish())
|
||||
}
|
||||
|
||||
pub fn main() {
|
||||
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()),
|
||||
)
|
||||
.route("/test/", web::get().to(index));
|
||||
use actix_web::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()),
|
||||
)
|
||||
.route("/test/", web::get().to(index))
|
||||
})
|
||||
.bind("127.0.0.1:8088")
|
||||
.unwrap()
|
||||
.run()
|
||||
.unwrap();
|
||||
}
|
||||
// </url>
|
||||
|
Reference in New Issue
Block a user