mirror of
https://github.com/actix/examples
synced 2025-06-28 09:50:36 +02:00
upgrade to 2.0 alpha.3
This commit is contained in:
@ -6,13 +6,13 @@ edition = "2018"
|
||||
workspace = ".."
|
||||
|
||||
[dependencies]
|
||||
actix-rt = "0.2.2"
|
||||
actix-web = { version="1.0.0", features=["ssl"] }
|
||||
actix-multipart = "0.1.1"
|
||||
actix-service = "0.4.1"
|
||||
bytes = "0.4.12"
|
||||
actix-rt = "1.0.0-alpha.3"
|
||||
actix-web = { version="2.0.0-alpha.3", features=["openssl"] }
|
||||
actix-multipart = "0.2.0-alpha.2"
|
||||
actix-service = "1.0.0-alpha.3"
|
||||
bytes = "0.5.2"
|
||||
env_logger = "0.6.1"
|
||||
futures = "0.1"
|
||||
futures = "0.3.1"
|
||||
serde = { version = "^1.0", features = ["derive"] }
|
||||
serde_derive = "1.0.90"
|
||||
serde_json = "1.0.39"
|
||||
|
@ -8,27 +8,27 @@ pub fn config_app(cfg: &mut web::ServiceConfig) {
|
||||
web::scope("/products")
|
||||
.service(
|
||||
web::resource("")
|
||||
.route(web::get().to_async(products::get_products))
|
||||
.route(web::post().to_async(products::add_product)),
|
||||
.route(web::get().to(products::get_products))
|
||||
.route(web::post().to(products::add_product)),
|
||||
)
|
||||
.service(
|
||||
web::scope("/{product_id}")
|
||||
.service(
|
||||
web::resource("")
|
||||
.route(web::get().to_async(products::get_product_detail))
|
||||
.route(web::delete().to_async(products::remove_product)),
|
||||
.route(web::get().to(products::get_product_detail))
|
||||
.route(web::delete().to(products::remove_product)),
|
||||
)
|
||||
.service(
|
||||
web::scope("/parts")
|
||||
.service(
|
||||
web::resource("")
|
||||
.route(web::get().to_async(parts::get_parts))
|
||||
.route(web::post().to_async(parts::add_part)),
|
||||
.route(web::get().to(parts::get_parts))
|
||||
.route(web::post().to(parts::add_part)),
|
||||
)
|
||||
.service(
|
||||
web::resource("/{part_id}")
|
||||
.route(web::get().to_async(parts::get_part_detail))
|
||||
.route(web::delete().to_async(parts::remove_part)),
|
||||
.route(web::get().to(parts::get_part_detail))
|
||||
.route(web::delete().to(parts::remove_part)),
|
||||
),
|
||||
),
|
||||
),
|
||||
|
@ -2,7 +2,8 @@ use actix_web::{middleware, App, HttpServer};
|
||||
|
||||
use async_ex2::appconfig::config_app;
|
||||
|
||||
fn main() -> std::io::Result<()> {
|
||||
#[actix_rt::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
std::env::set_var("RUST_LOG", "actix_server=info,actix_web=info");
|
||||
env_logger::init();
|
||||
|
||||
@ -12,5 +13,6 @@ fn main() -> std::io::Result<()> {
|
||||
.wrap(middleware::Logger::default())
|
||||
})
|
||||
.bind("127.0.0.1:8080")?
|
||||
.run()
|
||||
.start()
|
||||
.await
|
||||
}
|
||||
|
@ -1,28 +1,19 @@
|
||||
use actix_web::{web, Error, HttpResponse};
|
||||
use futures::{future::ok as fut_ok, Future};
|
||||
|
||||
use crate::common::{Part, Product};
|
||||
|
||||
pub fn get_parts(
|
||||
query: web::Query<Option<Part>>,
|
||||
) -> impl Future<Item = HttpResponse, Error = Error> {
|
||||
fut_ok(HttpResponse::Ok().finish())
|
||||
pub async fn get_parts(query: web::Query<Option<Part>>) -> Result<HttpResponse, Error> {
|
||||
Ok(HttpResponse::Ok().finish())
|
||||
}
|
||||
|
||||
pub fn add_part(
|
||||
new_part: web::Json<Product>,
|
||||
) -> impl Future<Item = HttpResponse, Error = Error> {
|
||||
fut_ok(HttpResponse::Ok().finish())
|
||||
pub async fn add_part(new_part: web::Json<Product>) -> Result<HttpResponse, Error> {
|
||||
Ok(HttpResponse::Ok().finish())
|
||||
}
|
||||
|
||||
pub fn get_part_detail(
|
||||
id: web::Path<String>,
|
||||
) -> impl Future<Item = HttpResponse, Error = Error> {
|
||||
fut_ok(HttpResponse::Ok().finish())
|
||||
pub async fn get_part_detail(id: web::Path<String>) -> Result<HttpResponse, Error> {
|
||||
Ok(HttpResponse::Ok().finish())
|
||||
}
|
||||
|
||||
pub fn remove_part(
|
||||
id: web::Path<String>,
|
||||
) -> impl Future<Item = HttpResponse, Error = Error> {
|
||||
fut_ok(HttpResponse::Ok().finish())
|
||||
pub async fn remove_part(id: web::Path<String>) -> Result<HttpResponse, Error> {
|
||||
Ok(HttpResponse::Ok().finish())
|
||||
}
|
||||
|
@ -1,45 +1,39 @@
|
||||
use actix_web::{web, Error, HttpResponse};
|
||||
use futures::{future::ok as fut_ok, Future};
|
||||
|
||||
use crate::common::{Part, Product};
|
||||
|
||||
pub fn get_products(
|
||||
pub async fn get_products(
|
||||
query: web::Query<Option<Part>>,
|
||||
) -> impl Future<Item = HttpResponse, Error = Error> {
|
||||
fut_ok(HttpResponse::Ok().finish())
|
||||
) -> Result<HttpResponse, Error> {
|
||||
Ok(HttpResponse::Ok().finish())
|
||||
}
|
||||
|
||||
pub fn add_product(
|
||||
pub async fn add_product(
|
||||
new_product: web::Json<Product>,
|
||||
) -> impl Future<Item = HttpResponse, Error = Error> {
|
||||
fut_ok(HttpResponse::Ok().finish())
|
||||
) -> Result<HttpResponse, Error> {
|
||||
Ok(HttpResponse::Ok().finish())
|
||||
}
|
||||
|
||||
pub fn get_product_detail(
|
||||
id: web::Path<String>,
|
||||
) -> impl Future<Item = HttpResponse, Error = Error> {
|
||||
fut_ok(HttpResponse::Ok().finish())
|
||||
pub async fn get_product_detail(id: web::Path<String>) -> Result<HttpResponse, Error> {
|
||||
Ok(HttpResponse::Ok().finish())
|
||||
}
|
||||
|
||||
pub fn remove_product(
|
||||
id: web::Path<String>,
|
||||
) -> impl Future<Item = HttpResponse, Error = Error> {
|
||||
fut_ok(HttpResponse::Ok().finish())
|
||||
pub async fn remove_product(id: web::Path<String>) -> Result<HttpResponse, Error> {
|
||||
Ok(HttpResponse::Ok().finish())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::appconfig::config_app;
|
||||
use actix_service::Service;
|
||||
use actix_web::{
|
||||
http::{header, StatusCode},
|
||||
test, web, App, HttpRequest, HttpResponse,
|
||||
test, App,
|
||||
};
|
||||
|
||||
#[test]
|
||||
fn test_add_product() {
|
||||
let mut app = test::init_service(App::new().configure(config_app));
|
||||
#[actix_rt::test]
|
||||
async fn test_add_product() {
|
||||
let mut app = test::init_service(App::new().configure(config_app)).await;
|
||||
|
||||
let payload = r#"{"id":12345,"product_type":"fancy","name":"test"}"#.as_bytes();
|
||||
|
||||
@ -49,7 +43,7 @@ mod tests {
|
||||
.set_payload(payload)
|
||||
.to_request();
|
||||
|
||||
let resp = test::block_on(app.call(req)).unwrap();
|
||||
let resp = app.call(req).await.unwrap();
|
||||
|
||||
assert_eq!(resp.status(), StatusCode::OK);
|
||||
}
|
||||
|
Reference in New Issue
Block a user