mirror of
https://github.com/actix/actix-website
synced 2024-11-23 16:31:08 +01:00
Merge branch 'update1.0-url' into update1.0
This commit is contained in:
commit
35156b0a75
@ -1,6 +1,7 @@
|
||||
[package]
|
||||
name = "url-dispatch"
|
||||
version = "0.7.0"
|
||||
edition = "2018"
|
||||
workspace = "../"
|
||||
|
||||
[dependencies]
|
||||
|
@ -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>
|
||||
|
@ -1,10 +1,3 @@
|
||||
extern crate actix;
|
||||
extern crate actix_web;
|
||||
extern crate futures;
|
||||
extern crate openssl;
|
||||
#[macro_use]
|
||||
extern crate serde;
|
||||
|
||||
mod cfg;
|
||||
mod dhandler;
|
||||
mod minfo;
|
||||
@ -17,21 +10,19 @@ mod pred;
|
||||
mod pred2;
|
||||
mod resource;
|
||||
mod scope;
|
||||
mod scope;
|
||||
mod url_ext;
|
||||
mod urls;
|
||||
|
||||
// <main>
|
||||
use actix_web::{http::Method, App, HttpRequest, HttpResponse};
|
||||
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}", Method::GET, index)
|
||||
.route("/user/{name}", Method::POST, index)
|
||||
.finish();
|
||||
.route("/user/{name}", web::get().to(index))
|
||||
.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,6 +1,6 @@
|
||||
// <path>
|
||||
extern crate serde_derive;
|
||||
use actix_web::{http::Method, App, Path, Result};
|
||||
use actix_web::{web, App, Result};
|
||||
use serde::Deserialize;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct Info {
|
||||
@ -8,14 +8,14 @@ struct Info {
|
||||
}
|
||||
|
||||
// extract path info using serde
|
||||
fn index(info: Path<Info>) -> Result<String> {
|
||||
fn index(info: web::Path<Info>) -> Result<String> {
|
||||
Ok(format!("Welcome {}!", info.username))
|
||||
}
|
||||
|
||||
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