mirror of
https://github.com/actix/actix-website
synced 2024-11-23 16:31:08 +01:00
Quick pass at url-dispatch
This commit is contained in:
parent
a313315a92
commit
0c268d18c1
@ -1,15 +1,14 @@
|
||||
// <cfg>
|
||||
use actix_web::{pred, App, HttpResponse};
|
||||
use actix_web::{guard, web, App, HttpResponse};
|
||||
|
||||
fn main() {
|
||||
App::new()
|
||||
.resource("/path", |resource| {
|
||||
resource
|
||||
.route()
|
||||
.filter(pred::Get())
|
||||
.filter(pred::Header("content-type", "text/plain"))
|
||||
.f(|req| HttpResponse::Ok())
|
||||
})
|
||||
.finish();
|
||||
pub fn main() {
|
||||
App::new().service(
|
||||
web::resource("/").route(
|
||||
web::route()
|
||||
.guard(guard::Get())
|
||||
.guard(guard::Header("content-type", "text/plain"))
|
||||
.to(|| HttpResponse::Ok()),
|
||||
),
|
||||
);
|
||||
}
|
||||
// </cfg>
|
||||
|
@ -1,14 +1,17 @@
|
||||
// <default>
|
||||
use actix_web::{http::Method, pred, App, HttpResponse};
|
||||
use actix_web::{guard, web, App, HttpRequest, HttpResponse, Responder};
|
||||
|
||||
fn index(_req: HttpRequest) -> impl Responder {
|
||||
"Welcome!"
|
||||
}
|
||||
|
||||
// <default>
|
||||
fn main() {
|
||||
App::new()
|
||||
.default_resource(|r| {
|
||||
r.method(Method::GET).f(|req| HttpResponse::NotFound());
|
||||
r.route()
|
||||
.filter(pred::Not(pred::Get()))
|
||||
.f(|req| HttpResponse::MethodNotAllowed());
|
||||
})
|
||||
.finish();
|
||||
.service(web::resource("/").route(web::get().to(index)))
|
||||
.default_service(
|
||||
web::route()
|
||||
.guard(guard::Not(guard::Get()))
|
||||
.to(|| HttpResponse::MethodNotAllowed()),
|
||||
);
|
||||
}
|
||||
// </default>
|
||||
|
@ -16,14 +16,13 @@ mod urls;
|
||||
// <main>
|
||||
use actix_web::{web, App, HttpRequest, HttpResponse};
|
||||
|
||||
fn index(req: HttpRequest) -> HttpResponse {
|
||||
fn index(_req: HttpRequest) -> HttpResponse {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn main() {
|
||||
App::new()
|
||||
.route("/user/{name}", web::get().to(index))
|
||||
.route("/user/{name}", web::get().to(index))
|
||||
.finish();
|
||||
.route("/user/{name}", web::get().to(index));
|
||||
}
|
||||
// </main>
|
||||
|
@ -1,15 +1,13 @@
|
||||
// <minfo>
|
||||
use actix_web::{App, HttpRequest, Result};
|
||||
use actix_web::{web, App, HttpRequest, Result};
|
||||
|
||||
fn index(req: &HttpRequest) -> Result<String> {
|
||||
let v1: u8 = req.match_info().query("v1")?;
|
||||
let v2: u8 = req.match_info().query("v2")?;
|
||||
fn index(req: HttpRequest) -> Result<String> {
|
||||
let v1: u8 = req.match_info().query("v1").parse().unwrap();
|
||||
let v2: u8 = req.match_info().query("v2").parse().unwrap();
|
||||
Ok(format!("Values {} {}", v1, v2))
|
||||
}
|
||||
|
||||
fn main() {
|
||||
App::new()
|
||||
.resource(r"/a/{v1}/{v2}/", |r| r.f(index))
|
||||
.finish();
|
||||
App::new().route(r"/a/{v1}/{v2}/", web::get().to(index));
|
||||
}
|
||||
// </minfo>
|
||||
|
@ -1,15 +1,14 @@
|
||||
// <norm>
|
||||
use actix_web::{http::NormalizePath, App};
|
||||
use actix_web::{middleware, web, App, HttpResponse};
|
||||
|
||||
fn main() {
|
||||
let app = App::new()
|
||||
.resource("/resource/", |r| r.f(index))
|
||||
.default_resource(|r| r.h(NormalizePath::default()))
|
||||
.finish();
|
||||
App::new()
|
||||
.wrap(middleware::NormalizePath)
|
||||
.route("/", web::get().to(|| HttpResponse::Ok()));
|
||||
}
|
||||
// </norm>
|
||||
|
||||
use actix_web::HttpRequest;
|
||||
fn index(req: &HttpRequest) -> String {
|
||||
fn index(_req: HttpRequest) -> String {
|
||||
unimplemented!()
|
||||
}
|
||||
|
@ -1,16 +1,16 @@
|
||||
// <norm>
|
||||
use actix_web::{http::Method, http::NormalizePath, App};
|
||||
use actix_web::{http::Method, middleware, web, App};
|
||||
|
||||
fn main() {
|
||||
let app = App::new()
|
||||
.resource("/resource/", |r| r.f(index))
|
||||
.default_resource(|r| r.method(Method::GET).h(NormalizePath::default()))
|
||||
.finish();
|
||||
App::new()
|
||||
.wrap(middleware::NormalizePath)
|
||||
.route("/resource/", web::get().to(index))
|
||||
.default_service(web::route().method(Method::GET));
|
||||
}
|
||||
// </norm>
|
||||
|
||||
use actix_web::HttpRequest;
|
||||
|
||||
fn index(req: &HttpRequest) -> String {
|
||||
fn index(_req: HttpRequest) -> String {
|
||||
unimplemented!()
|
||||
}
|
||||
|
@ -1,15 +1,15 @@
|
||||
// <path>
|
||||
use actix_web::{http::Method, App, Path, Result};
|
||||
use actix_web::{web, App, Result};
|
||||
|
||||
// extract path info using serde
|
||||
fn index(info: Path<(String, u32)>) -> Result<String> {
|
||||
fn index(info: web::Path<(String, u32)>) -> Result<String> {
|
||||
Ok(format!("Welcome {}! id: {}", info.0, info.1))
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let app = App::new().resource(
|
||||
App::new().route(
|
||||
"/{username}/{id}/index.html", // <- define path parameters
|
||||
|r| r.method(Method::GET).with(index),
|
||||
web::get().to(index),
|
||||
);
|
||||
}
|
||||
// </path>
|
||||
|
@ -1,5 +1,5 @@
|
||||
// <path>
|
||||
use actix_web::{http::Method, web, App, Result};
|
||||
use actix_web::{web, App, Result};
|
||||
use serde::Deserialize;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
@ -13,9 +13,9 @@ fn index(info: web::Path<Info>) -> Result<String> {
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let app = App::new().resource(
|
||||
App::new().route(
|
||||
"/{username}/index.html", // <- define path parameters
|
||||
|r| r.method(Method::GET).with(index),
|
||||
web::get().to(index),
|
||||
);
|
||||
}
|
||||
// </path>
|
||||
|
@ -1,15 +1,13 @@
|
||||
// <pbuf>
|
||||
use actix_web::{http::Method, App, HttpRequest, Result};
|
||||
use actix_web::{web, App, HttpRequest, Result};
|
||||
use std::path::PathBuf;
|
||||
|
||||
fn index(req: &HttpRequest) -> Result<String> {
|
||||
let path: PathBuf = req.match_info().query("tail")?;
|
||||
fn index(req: HttpRequest) -> Result<String> {
|
||||
let path: PathBuf = req.match_info().query("tail").parse().unwrap();
|
||||
Ok(format!("Path {:?}", path))
|
||||
}
|
||||
|
||||
fn main() {
|
||||
App::new()
|
||||
.resource(r"/a/{tail:.*}", |r| r.method(Method::GET).f(index))
|
||||
.finish();
|
||||
App::new().route(r"/a/{tail:.*}", web::get().to(index));
|
||||
}
|
||||
// </pbuf>
|
||||
|
@ -1,19 +1,20 @@
|
||||
// <pred>
|
||||
use actix_web::{http, server::Request, pred::Predicate, App, HttpResponse};
|
||||
use actix_web::{dev::RequestHead, guard::Guard, http, web, App, HttpResponse};
|
||||
|
||||
struct ContentTypeHeader;
|
||||
|
||||
impl<S: 'static> Predicate<S> for ContentTypeHeader {
|
||||
fn check(&self, req: &Request, state: &S) -> bool {
|
||||
impl Guard for ContentTypeHeader {
|
||||
fn check(&self, req: &RequestHead) -> bool {
|
||||
req.headers().contains_key(http::header::CONTENT_TYPE)
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
App::new().resource("/index.html", |r| {
|
||||
r.route()
|
||||
.filter(ContentTypeHeader)
|
||||
.f(|_| HttpResponse::Ok())
|
||||
});
|
||||
App::new().route(
|
||||
"",
|
||||
web::route()
|
||||
.guard(ContentTypeHeader)
|
||||
.to(|| HttpResponse::Ok()),
|
||||
);
|
||||
}
|
||||
// </pred>
|
||||
|
@ -1,13 +1,12 @@
|
||||
// <pred>
|
||||
use actix_web::{pred, App, HttpResponse};
|
||||
use actix_web::{guard, web, App, HttpResponse};
|
||||
|
||||
fn main() {
|
||||
App::new()
|
||||
.resource("/index.html", |r| {
|
||||
r.route()
|
||||
.filter(pred::Not(pred::Get()))
|
||||
.f(|req| HttpResponse::MethodNotAllowed())
|
||||
})
|
||||
.finish();
|
||||
App::new().route(
|
||||
"/",
|
||||
web::route()
|
||||
.guard(guard::Not(guard::Get()))
|
||||
.to(|| HttpResponse::MethodNotAllowed()),
|
||||
);
|
||||
}
|
||||
// </pred>
|
||||
|
@ -1,16 +1,15 @@
|
||||
// <resource>
|
||||
use actix_web::{http::Method, App, HttpRequest, HttpResponse};
|
||||
use actix_web::{http::Method, web, App, HttpRequest, HttpResponse};
|
||||
|
||||
fn index(req: &HttpRequest) -> HttpResponse {
|
||||
fn index(_req: HttpRequest) -> HttpResponse {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn main() {
|
||||
App::new()
|
||||
.resource("/prefix", |r| r.f(index))
|
||||
.resource("/user/{name}", |r| {
|
||||
r.method(Method::GET).f(|req| HttpResponse::Ok())
|
||||
})
|
||||
.finish();
|
||||
.service(web::resource("/prefix").route(web::get().to(index)))
|
||||
.service(
|
||||
web::resource("/user/{name}").route(web::get().to(|| HttpResponse::Ok())),
|
||||
);
|
||||
}
|
||||
// </resource>
|
||||
|
@ -1,4 +1,4 @@
|
||||
use actix_web::{App, HttpRequest, HttpResponse};
|
||||
use actix_web::{web, App, HttpRequest, HttpResponse};
|
||||
|
||||
// <scope>
|
||||
fn show_users(_req: HttpRequest) -> HttpResponse {
|
||||
@ -7,7 +7,8 @@ fn show_users(_req: HttpRequest) -> HttpResponse {
|
||||
|
||||
#[rustfmt::skip]
|
||||
fn main() {
|
||||
App::new().service(
|
||||
web::scope("/users") .route("/show", web::to(show_users)))
|
||||
App::new()
|
||||
.service(web::scope("/users")
|
||||
.route("/show", web::get().to(show_users)));
|
||||
}
|
||||
// </scope>
|
||||
|
@ -1,16 +1,15 @@
|
||||
// <ext>
|
||||
use actix_web::{App, Error, HttpRequest, HttpResponse};
|
||||
use actix_web::{web, App, Error, HttpRequest, HttpResponse};
|
||||
|
||||
fn index(req: &HttpRequest) -> Result<HttpResponse, Error> {
|
||||
fn index(req: HttpRequest) -> Result<HttpResponse, Error> {
|
||||
let url = req.url_for("youtube", &["oHg5SJYRHA0"])?;
|
||||
assert_eq!(url.as_str(), "https://youtube.com/watch/oHg5SJYRHA0");
|
||||
Ok(HttpResponse::Ok().into())
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let app = App::new()
|
||||
.resource("/index.html", |r| r.f(index))
|
||||
.external_resource("youtube", "https://youtube.com/watch/{video_id}")
|
||||
.finish();
|
||||
App::new()
|
||||
.service(web::resource("/index.html").route(web::get().to(index)))
|
||||
.external_resource("youtube", "https://youtube.com/watch/{video_id}");
|
||||
}
|
||||
// </ext>
|
||||
|
@ -1,5 +1,7 @@
|
||||
// <url>
|
||||
use actix_web::{http::header, http::Method, App, HttpRequest, HttpResponse, Result};
|
||||
use actix_web::{
|
||||
guard, http::header, http::Method, 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
|
||||
@ -9,12 +11,13 @@ fn index(req: HttpRequest) -> Result<HttpResponse> {
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let app = App::new()
|
||||
.resource("/test/{a}/{b}/{c}", |r| {
|
||||
r.name("foo"); // <- set resource name, then it could be used in `url_for`
|
||||
r.method(Method::GET).f(|_| HttpResponse::Ok());
|
||||
})
|
||||
.route("/test/", Method::GET, index)
|
||||
.finish();
|
||||
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));
|
||||
}
|
||||
// </url>
|
||||
|
Loading…
Reference in New Issue
Block a user