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