mirror of
https://github.com/actix/examples
synced 2025-06-28 18:00:37 +02:00
upgrade to alpha.6
This commit is contained in:
@ -1,47 +1,36 @@
|
||||
use actix_web::{error, web};
|
||||
use bytes::Bytes;
|
||||
use futures::Stream;
|
||||
|
||||
use crate::{
|
||||
handlers::{
|
||||
products,
|
||||
parts
|
||||
},
|
||||
};
|
||||
use crate::handlers::{parts, products};
|
||||
|
||||
|
||||
pub fn config_app<P>(cfg: &mut web::RouterConfig<P>)
|
||||
where P: Stream<Item = Bytes, Error = error::PayloadError>
|
||||
+ 'static
|
||||
{
|
||||
pub fn config_app(cfg: &mut web::RouterConfig) {
|
||||
// domain includes: /products/{product_id}/parts/{part_id}
|
||||
cfg.service(
|
||||
web::scope("/products")
|
||||
.service(
|
||||
web::resource("")
|
||||
.route(web::get().to_async(products::get_products))
|
||||
.route(web::post().to_async(products::add_product))
|
||||
.route(web::post().to_async(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::delete().to_async(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::post().to_async(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::delete().to_async(parts::remove_part)),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -1,19 +1,16 @@
|
||||
use actix_web::{middleware, App, HttpServer};
|
||||
|
||||
use async_ex2::{
|
||||
appconfig::config_app,
|
||||
};
|
||||
|
||||
use async_ex2::appconfig::config_app;
|
||||
|
||||
fn main() -> std::io::Result<()> {
|
||||
std::env::set_var("RUST_LOG", "actix_server=info,actix_web=info");
|
||||
env_logger::init();
|
||||
|
||||
HttpServer::new(||
|
||||
HttpServer::new(|| {
|
||||
App::new()
|
||||
.configure(config_app)
|
||||
.wrap(middleware::Logger::default())
|
||||
)
|
||||
})
|
||||
.bind("127.0.0.1:8080")?
|
||||
.run()
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,5 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
|
||||
#[derive(Deserialize, Serialize)]
|
||||
pub struct Product {
|
||||
id: Option<i64>,
|
||||
@ -8,10 +7,9 @@ pub struct Product {
|
||||
name: Option<String>,
|
||||
}
|
||||
|
||||
|
||||
#[derive(Deserialize, Serialize)]
|
||||
pub struct Part {
|
||||
id: Option<i64>,
|
||||
part_type: Option<String>,
|
||||
name: Option<String>,
|
||||
}
|
||||
}
|
||||
|
@ -1,3 +1,2 @@
|
||||
pub mod products;
|
||||
pub mod parts;
|
||||
|
||||
pub mod products;
|
||||
|
@ -1,26 +1,32 @@
|
||||
use actix_multipart::{Field, Item, Multipart, MultipartError};
|
||||
use actix_web::{HttpResponse, web, error, Error};
|
||||
use futures::{future::{ok as fut_ok, err as fut_err, Either}, Future, Stream};
|
||||
use actix_multipart::{Field, Multipart, MultipartError};
|
||||
use actix_web::{error, web, Error, HttpResponse};
|
||||
use futures::{
|
||||
future::{err as fut_err, ok as fut_ok, Either},
|
||||
Future, Stream,
|
||||
};
|
||||
|
||||
use crate::common::{Part, Product};
|
||||
|
||||
|
||||
pub fn get_parts(query: web::Query<Option<Part>>)
|
||||
-> impl Future<Item = HttpResponse, Error = Error> {
|
||||
pub fn get_parts(
|
||||
query: web::Query<Option<Part>>,
|
||||
) -> impl Future<Item = HttpResponse, Error = Error> {
|
||||
fut_ok(HttpResponse::Ok().finish())
|
||||
}
|
||||
|
||||
pub fn add_part(new_part: web::Json<Product>)
|
||||
-> impl Future<Item = HttpResponse, Error = Error> {
|
||||
pub fn add_part(
|
||||
new_part: web::Json<Product>,
|
||||
) -> impl Future<Item = HttpResponse, Error = Error> {
|
||||
fut_ok(HttpResponse::Ok().finish())
|
||||
}
|
||||
|
||||
pub fn get_part_detail(id: web::Path<String>)
|
||||
-> impl Future<Item = HttpResponse, Error = Error> {
|
||||
pub fn get_part_detail(
|
||||
id: web::Path<String>,
|
||||
) -> impl Future<Item = HttpResponse, Error = Error> {
|
||||
fut_ok(HttpResponse::Ok().finish())
|
||||
}
|
||||
|
||||
pub fn remove_part(id: web::Path<String>)
|
||||
-> impl Future<Item = HttpResponse, Error = Error> {
|
||||
pub fn remove_part(
|
||||
id: web::Path<String>,
|
||||
) -> impl Future<Item = HttpResponse, Error = Error> {
|
||||
fut_ok(HttpResponse::Ok().finish())
|
||||
}
|
||||
}
|
||||
|
@ -1,54 +1,57 @@
|
||||
use actix_multipart::{Field, Item, Multipart, MultipartError};
|
||||
use actix_web::{HttpResponse, web, error, Error};
|
||||
use futures::{future::{ok as fut_ok, err as fut_err, Either}, Future, Stream};
|
||||
use actix_multipart::{Field, Multipart, MultipartError};
|
||||
use actix_web::{error, web, Error, HttpResponse};
|
||||
use futures::{
|
||||
future::{err as fut_err, ok as fut_ok, Either},
|
||||
Future, Stream,
|
||||
};
|
||||
|
||||
use crate::common::{Part, Product};
|
||||
|
||||
|
||||
pub fn get_products(query: web::Query<Option<Part>>)
|
||||
-> impl Future<Item = HttpResponse, Error = Error> {
|
||||
pub fn get_products(
|
||||
query: web::Query<Option<Part>>,
|
||||
) -> impl Future<Item = HttpResponse, Error = Error> {
|
||||
fut_ok(HttpResponse::Ok().finish())
|
||||
}
|
||||
|
||||
pub fn add_product(new_product: web::Json<Product>)
|
||||
-> impl Future<Item = HttpResponse, Error = Error> {
|
||||
pub fn add_product(
|
||||
new_product: web::Json<Product>,
|
||||
) -> impl Future<Item = HttpResponse, Error = Error> {
|
||||
fut_ok(HttpResponse::Ok().finish())
|
||||
}
|
||||
|
||||
pub fn get_product_detail(id: web::Path<String>)
|
||||
-> impl Future<Item = HttpResponse, Error = Error> {
|
||||
pub fn get_product_detail(
|
||||
id: web::Path<String>,
|
||||
) -> impl Future<Item = HttpResponse, Error = Error> {
|
||||
fut_ok(HttpResponse::Ok().finish())
|
||||
}
|
||||
|
||||
pub fn remove_product(id: web::Path<String>)
|
||||
-> impl Future<Item = HttpResponse, Error = Error> {
|
||||
pub fn remove_product(
|
||||
id: web::Path<String>,
|
||||
) -> impl Future<Item = HttpResponse, Error = Error> {
|
||||
fut_ok(HttpResponse::Ok().finish())
|
||||
}
|
||||
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use actix_service::Service;
|
||||
use actix_web::{test, HttpResponse, HttpRequest, App,
|
||||
http::{header, StatusCode}, web};
|
||||
use crate::appconfig::config_app;
|
||||
|
||||
use actix_service::Service;
|
||||
use actix_web::{
|
||||
http::{header, StatusCode},
|
||||
test, web, App, HttpRequest, HttpResponse,
|
||||
};
|
||||
|
||||
#[test]
|
||||
fn test_add_product() {
|
||||
let mut app = test::init_service(
|
||||
App::new()
|
||||
.configure(config_app)
|
||||
);
|
||||
let mut app = test::init_service(App::new().configure(config_app));
|
||||
|
||||
let payload = r#"{"id":12345,"product_type":"fancy","name":"test"}"#.as_bytes();
|
||||
|
||||
let req = test::TestRequest::post()
|
||||
.uri("/products")
|
||||
.header(header::CONTENT_TYPE, "application/json")
|
||||
.set_payload(payload)
|
||||
.to_request();
|
||||
.uri("/products")
|
||||
.header(header::CONTENT_TYPE, "application/json")
|
||||
.set_payload(payload)
|
||||
.to_request();
|
||||
|
||||
let resp = test::block_on(app.call(req)).unwrap();
|
||||
|
||||
|
Reference in New Issue
Block a user