1
0
mirror of https://github.com/actix/actix-website synced 2025-06-27 07:29:02 +02:00
This commit is contained in:
Rob Ede
2020-09-12 16:21:54 +01:00
committed by GitHub
parent a0ce9f28e2
commit 4d8d53cea5
145 changed files with 1011 additions and 1461 deletions

View File

@ -5,9 +5,8 @@ edition = "2018"
workspace = "../"
[dependencies]
actix = "0.9"
actix-rt = "1.0"
actix-web = "2.0"
actix = "0.10"
actix-web = "3"
futures = "0.3.1"
openssl = "0.10"
serde = "1.0"

View File

@ -1,7 +1,7 @@
use actix_web::{guard, web, App, HttpResponse};
#[rustfmt::skip]
#[actix_rt::main]
#[actix_web::main]
async fn main() -> std::io::Result<()> {
use actix_web::HttpServer;
@ -17,7 +17,7 @@ App::new().service(
)
// </cfg>
})
.bind("127.0.0.1:8088")?
.bind("127.0.0.1:8080")?
.run()
.await
}

View File

@ -1,11 +1,12 @@
use actix_web::{guard, web, App, HttpRequest, HttpResponse, HttpServer, Responder};
#[allow(dead_code)]
async fn index(_req: HttpRequest) -> impl Responder {
"Welcome!"
}
// <default>
#[actix_rt::main]
#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| {
App::new()
@ -16,7 +17,7 @@ async fn main() -> std::io::Result<()> {
.to(|| HttpResponse::MethodNotAllowed()),
)
})
.bind("127.0.0.1:8088")?
.bind("127.0.0.1:8080")?
.run()
.await
}

View File

@ -9,7 +9,7 @@ impl Guard for ContentTypeHeader {
}
}
#[actix_rt::main]
#[actix_web::main]
async fn main() -> std::io::Result<()> {
use actix_web::{web, App, HttpServer};
@ -21,7 +21,7 @@ async fn main() -> std::io::Result<()> {
.to(|| HttpResponse::Ok()),
)
})
.bind("127.0.0.1:8088")?
.bind("127.0.0.1:8080")?
.run()
.await
}

View File

@ -1,7 +1,7 @@
// <guard2>
use actix_web::{guard, web, App, HttpResponse, HttpServer};
#[actix_rt::main]
#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| {
App::new().route(
@ -11,7 +11,7 @@ async fn main() -> std::io::Result<()> {
.to(|| HttpResponse::MethodNotAllowed()),
)
})
.bind("127.0.0.1:8088")?
.bind("127.0.0.1:8080")?
.run()
.await
}

View File

@ -20,14 +20,14 @@ async fn index() -> HttpResponse {
HttpResponse::Ok().body("Hello")
}
#[actix_rt::main]
#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| {
App::new()
.route("/", web::get().to(index))
.route("/user", web::post().to(index))
})
.bind("127.0.0.1:8088")?
.bind("127.0.0.1:8080")?
.run()
.await
}

View File

@ -1,6 +1,7 @@
// <minfo>
use actix_web::{HttpRequest, HttpResponse, Result};
use actix_web::{get, App, HttpRequest, HttpServer, Result};
#[get("/a/{v1}/{v2}/")]
async fn index(req: HttpRequest) -> Result<String> {
let v1: u8 = req.match_info().get("v1").unwrap().parse().unwrap();
let v2: u8 = req.match_info().query("v2").parse().unwrap();
@ -8,17 +9,11 @@ async fn index(req: HttpRequest) -> Result<String> {
Ok(format!("Values {} {} {} {}", v1, v2, v3, v4))
}
#[actix_rt::main]
#[actix_web::main]
async fn main() -> std::io::Result<()> {
use actix_web::{web, App, HttpServer};
HttpServer::new(|| {
App::new()
.route("/a/{v1}/{v2}/", web::get().to(index))
.route("", web::get().to(|| HttpResponse::Ok()))
})
.bind("127.0.0.1:8088")?
.run()
.await
HttpServer::new(|| App::new().service(index))
.bind("127.0.0.1:8080")?
.run()
.await
}
// </minfo>

View File

@ -1,3 +1,5 @@
#![allow(dead_code)]
// <norm>
use actix_web::{middleware, HttpResponse};
@ -5,16 +7,16 @@ async fn index() -> HttpResponse {
HttpResponse::Ok().body("Hello")
}
#[actix_rt::main]
#[actix_web::main]
async fn main() -> std::io::Result<()> {
use actix_web::{web, App, HttpServer};
HttpServer::new(|| {
App::new()
.wrap(middleware::NormalizePath)
.wrap(middleware::NormalizePath::default())
.route("/resource/", web::to(index))
})
.bind("127.0.0.1:8088")?
.bind("127.0.0.1:8080")?
.run()
.await
}

View File

@ -1,21 +1,22 @@
use actix_web::HttpResponse;
#[get("/resource/")]
fn index() -> HttpResponse {
HttpResponse::Ok().body("Hello")
}
// <norm>
use actix_web::{http::Method, middleware, web, App, HttpServer};
use actix_web::{get, http::Method, middleware, web, App, HttpServer};
#[actix_rt::main]
#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| {
App::new()
.wrap(middleware::NormalizePath)
.route("/resource/", web::get().to(index))
.wrap(middleware::NormalizePath::default())
.service(index)
.default_service(web::route().method(Method::GET))
})
.bind("127.0.0.1:8088")?
.bind("127.0.0.1:8080")?
.run()
.await
}

View File

@ -1,22 +1,17 @@
// <path>
use actix_web::{web, Result};
use actix_web::{get, web, App, HttpServer, Result};
#[get("/{username}/{id}/index.html")] // <- define path parameters
async fn index(info: web::Path<(String, u32)>) -> Result<String> {
let info = info.into_inner();
Ok(format!("Welcome {}! id: {}", info.0, info.1))
}
#[actix_rt::main]
#[actix_web::main]
async fn main() -> std::io::Result<()> {
use actix_web::{App, HttpServer};
HttpServer::new(|| {
App::new().route(
"/{username}/{id}/index.html", // <- define path parameters
web::get().to(index),
)
})
.bind("127.0.0.1:8088")?
.run()
.await
HttpServer::new(|| App::new().service(index))
.bind("127.0.0.1:8080")?
.run()
.await
}
// </path>

View File

@ -1,5 +1,5 @@
// <path>
use actix_web::{web, Result};
use actix_web::{get, web, App, HttpServer, Result};
use serde::Deserialize;
#[derive(Deserialize)]
@ -8,22 +8,16 @@ struct Info {
}
// extract path info using serde
#[get("/{username}/index.html")] // <- define path parameters
async fn index(info: web::Path<Info>) -> Result<String> {
Ok(format!("Welcome {}!", info.username))
}
#[actix_rt::main]
#[actix_web::main]
async fn main() -> std::io::Result<()> {
use actix_web::{App, HttpServer};
HttpServer::new(|| {
App::new().route(
"/{username}/index.html", // <- define path parameters
web::get().to(index),
)
})
.bind("127.0.0.1:8088")?
.run()
.await
HttpServer::new(|| App::new().service(index))
.bind("127.0.0.1:8080")?
.run()
.await
}
// </path>

View File

@ -1,18 +1,17 @@
// <pbuf>
use actix_web::{HttpRequest, Result};
use actix_web::{get, App, HttpRequest, HttpServer, Result};
use std::path::PathBuf;
#[get("/a/{tail:.*}")]
async fn index(req: HttpRequest) -> Result<String> {
let path: PathBuf = req.match_info().query("tail").parse().unwrap();
Ok(format!("Path {:?}", path))
}
#[actix_rt::main]
#[actix_web::main]
async fn main() -> std::io::Result<()> {
use actix_web::{web, App, HttpServer};
HttpServer::new(|| App::new().route(r"/a/{tail:.*}", web::get().to(index)))
.bind("127.0.0.1:8088")?
HttpServer::new(|| App::new().service(index))
.bind("127.0.0.1:8080")?
.run()
.await
}

View File

@ -1,24 +1,26 @@
use actix_web::{web, App, HttpResponse, HttpServer};
use actix_web::{get, web, App, HttpResponse, HttpServer};
// <scope>
#[get("/show")]
async fn show_users() -> HttpResponse {
HttpResponse::Ok().body("Show users")
}
#[get("/show/{id}")]
async fn user_detail(path: web::Path<(u32,)>) -> HttpResponse {
HttpResponse::Ok().body(format!("User detail: {}", path.0))
HttpResponse::Ok().body(format!("User detail: {}", path.into_inner().0))
}
#[actix_rt::main]
#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| {
App::new().service(
web::scope("/users")
.route("/show", web::get().to(show_users))
.route("/show/{id}", web::get().to(user_detail)),
.service(show_users)
.service(user_detail),
)
})
.bind("127.0.0.1:8088")?
.bind("127.0.0.1:8080")?
.run()
.await
}

View File

@ -1,6 +1,7 @@
// <ext>
use actix_web::{HttpRequest, Responder};
use actix_web::{get, App, HttpRequest, HttpServer, Responder};
#[get("/")]
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");
@ -8,17 +9,14 @@ async fn index(req: HttpRequest) -> impl Responder {
url.into_string()
}
#[actix_rt::main]
#[actix_web::main]
async fn main() -> std::io::Result<()> {
use actix_web::{web, App, HttpServer};
HttpServer::new(|| {
App::new()
.route("/", web::get().to(index))
.service(index)
.external_resource("youtube", "https://youtube.com/watch/{video_id}")
.route("/", actix_web::web::get().to(index))
})
.bind("127.0.0.1:8088")?
.bind("127.0.0.1:8080")?
.run()
.await
}

View File

@ -1,6 +1,7 @@
// <url>
use actix_web::{guard, http::header, HttpRequest, HttpResponse, Result};
use actix_web::{get, guard, http::header, HttpRequest, HttpResponse, Result};
#[get("/test/")]
async fn index(req: HttpRequest) -> Result<HttpResponse> {
let url = req.url_for("foo", &["1", "2", "3"])?; // <- generate url for "foo" resource
@ -9,7 +10,7 @@ async fn index(req: HttpRequest) -> Result<HttpResponse> {
.finish())
}
#[actix_rt::main]
#[actix_web::main]
async fn main() -> std::io::Result<()> {
use actix_web::{web, App, HttpServer};
@ -21,9 +22,9 @@ async fn main() -> std::io::Result<()> {
.guard(guard::Get())
.to(|| HttpResponse::Ok()),
)
.route("/test/", web::get().to(index))
.service(index)
})
.bind("127.0.0.1:8088")?
.bind("127.0.0.1:8080")?
.run()
.await
}