mirror of
https://github.com/actix/examples
synced 2025-06-26 17:17:42 +02:00
Restructure folders (#411)
This commit is contained in:
committed by
GitHub
parent
9db98162b2
commit
c3407627d0
36
basics/nested-routing/src/appconfig.rs
Normal file
36
basics/nested-routing/src/appconfig.rs
Normal file
@ -0,0 +1,36 @@
|
||||
use actix_web::web;
|
||||
|
||||
use crate::handlers::{parts, products};
|
||||
|
||||
pub fn config_app(cfg: &mut web::ServiceConfig) {
|
||||
// domain includes: /products/{product_id}/parts/{part_id}
|
||||
cfg.service(
|
||||
web::scope("/products")
|
||||
.service(
|
||||
web::resource("")
|
||||
.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(products::get_product_detail))
|
||||
.route(web::delete().to(products::remove_product)),
|
||||
)
|
||||
.service(
|
||||
web::scope("/parts")
|
||||
.service(
|
||||
web::resource("")
|
||||
.route(web::get().to(parts::get_parts))
|
||||
.route(web::post().to(parts::add_part)),
|
||||
)
|
||||
.service(
|
||||
web::resource("/{part_id}")
|
||||
.route(web::get().to(parts::get_part_detail))
|
||||
.route(web::delete().to(parts::remove_part)),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
18
basics/nested-routing/src/bin/main.rs
Normal file
18
basics/nested-routing/src/bin/main.rs
Normal file
@ -0,0 +1,18 @@
|
||||
use actix_web::{middleware, App, HttpServer};
|
||||
|
||||
use async_ex2::appconfig::config_app;
|
||||
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
std::env::set_var("RUST_LOG", "actix_server=info,actix_web=info");
|
||||
env_logger::init();
|
||||
|
||||
HttpServer::new(|| {
|
||||
App::new()
|
||||
.configure(config_app)
|
||||
.wrap(middleware::Logger::default())
|
||||
})
|
||||
.bind("127.0.0.1:8080")?
|
||||
.run()
|
||||
.await
|
||||
}
|
15
basics/nested-routing/src/common.rs
Normal file
15
basics/nested-routing/src/common.rs
Normal file
@ -0,0 +1,15 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Deserialize, Serialize)]
|
||||
pub struct Product {
|
||||
id: Option<i64>,
|
||||
product_type: Option<String>,
|
||||
name: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Serialize)]
|
||||
pub struct Part {
|
||||
id: Option<i64>,
|
||||
part_type: Option<String>,
|
||||
name: Option<String>,
|
||||
}
|
2
basics/nested-routing/src/handlers/mod.rs
Normal file
2
basics/nested-routing/src/handlers/mod.rs
Normal file
@ -0,0 +1,2 @@
|
||||
pub mod parts;
|
||||
pub mod products;
|
19
basics/nested-routing/src/handlers/parts.rs
Normal file
19
basics/nested-routing/src/handlers/parts.rs
Normal file
@ -0,0 +1,19 @@
|
||||
use actix_web::{web, Error, HttpResponse};
|
||||
|
||||
use crate::common::{Part, Product};
|
||||
|
||||
pub async fn get_parts(_query: web::Query<Option<Part>>) -> Result<HttpResponse, Error> {
|
||||
Ok(HttpResponse::Ok().finish())
|
||||
}
|
||||
|
||||
pub async fn add_part(_new_part: web::Json<Product>) -> Result<HttpResponse, Error> {
|
||||
Ok(HttpResponse::Ok().finish())
|
||||
}
|
||||
|
||||
pub async fn get_part_detail(_id: web::Path<String>) -> Result<HttpResponse, Error> {
|
||||
Ok(HttpResponse::Ok().finish())
|
||||
}
|
||||
|
||||
pub async fn remove_part(_id: web::Path<String>) -> Result<HttpResponse, Error> {
|
||||
Ok(HttpResponse::Ok().finish())
|
||||
}
|
50
basics/nested-routing/src/handlers/products.rs
Normal file
50
basics/nested-routing/src/handlers/products.rs
Normal file
@ -0,0 +1,50 @@
|
||||
use actix_web::{web, Error, HttpResponse};
|
||||
|
||||
use crate::common::{Part, Product};
|
||||
|
||||
pub async fn get_products(
|
||||
_query: web::Query<Option<Part>>,
|
||||
) -> Result<HttpResponse, Error> {
|
||||
Ok(HttpResponse::Ok().finish())
|
||||
}
|
||||
|
||||
pub async fn add_product(
|
||||
_new_product: web::Json<Product>,
|
||||
) -> Result<HttpResponse, Error> {
|
||||
Ok(HttpResponse::Ok().finish())
|
||||
}
|
||||
|
||||
pub async fn get_product_detail(_id: web::Path<String>) -> Result<HttpResponse, Error> {
|
||||
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 crate::appconfig::config_app;
|
||||
use actix_service::Service;
|
||||
use actix_web::{
|
||||
http::{header, StatusCode},
|
||||
test, 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();
|
||||
|
||||
let req = test::TestRequest::post()
|
||||
.uri("/products")
|
||||
.header(header::CONTENT_TYPE, "application/json")
|
||||
.set_payload(payload)
|
||||
.to_request();
|
||||
|
||||
let resp = app.call(req).await.unwrap();
|
||||
|
||||
assert_eq!(resp.status(), StatusCode::OK);
|
||||
}
|
||||
}
|
3
basics/nested-routing/src/lib.rs
Normal file
3
basics/nested-routing/src/lib.rs
Normal file
@ -0,0 +1,3 @@
|
||||
pub mod appconfig;
|
||||
pub mod common;
|
||||
pub mod handlers;
|
Reference in New Issue
Block a user