From d29f113ffc823046c2b9e0554d4e166c15413be4 Mon Sep 17 00:00:00 2001 From: dowwie Date: Tue, 9 Apr 2019 14:12:07 -0400 Subject: [PATCH] added async_ex2 --- .gitignore | 2 ++ Cargo.toml | 1 + async_ex2/Cargo.toml | 20 +++++++++++++ async_ex2/src/README.md | 3 ++ async_ex2/src/appconfig.rs | 47 ++++++++++++++++++++++++++++++ async_ex2/src/bin/main.rs | 19 ++++++++++++ async_ex2/src/common.rs | 17 +++++++++++ async_ex2/src/handlers/mod.rs | 3 ++ async_ex2/src/handlers/parts.rs | 26 +++++++++++++++++ async_ex2/src/handlers/products.rs | 26 +++++++++++++++++ async_ex2/src/lib.rs | 3 ++ 11 files changed, 167 insertions(+) create mode 100644 async_ex2/Cargo.toml create mode 100644 async_ex2/src/README.md create mode 100644 async_ex2/src/appconfig.rs create mode 100644 async_ex2/src/bin/main.rs create mode 100644 async_ex2/src/common.rs create mode 100644 async_ex2/src/handlers/mod.rs create mode 100644 async_ex2/src/handlers/parts.rs create mode 100644 async_ex2/src/handlers/products.rs create mode 100644 async_ex2/src/lib.rs diff --git a/.gitignore b/.gitignore index 4a903f99..532edaeb 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,5 @@ Cargo.lock # intellij files .idea/** + +.history/ diff --git a/Cargo.toml b/Cargo.toml index 9ff86387..d6285920 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,6 +5,7 @@ members = [ "actix_todo", "async_db", "async_ex1", + "async_ex2", "basics", "cookie-auth", "cookie-session", diff --git a/async_ex2/Cargo.toml b/async_ex2/Cargo.toml new file mode 100644 index 00000000..d8195f6c --- /dev/null +++ b/async_ex2/Cargo.toml @@ -0,0 +1,20 @@ +[package] +name = "async_ex2" +version = "0.1.0" +authors = ["dowwie "] +edition = "2018" +workspace = ".." + +[dependencies] +actix-rt = "0.2.2" +actix-web = { version="1.0.0-alpha.4", features=["ssl"] } +actix-multipart = { git="https://github.com/actix/actix-web.git" } +bytes = "0.4.12" +env_logger = "0.6.1" +futures = "0.1" +serde = { version = "^1.0", features = ["derive"] } +serde_derive = "1.0.90" +serde_json = "1.0.39" +time = "0.1.42" +validator = "0.8.0" +validator_derive = "0.8.0" diff --git a/async_ex2/src/README.md b/async_ex2/src/README.md new file mode 100644 index 00000000..68374f9a --- /dev/null +++ b/async_ex2/src/README.md @@ -0,0 +1,3 @@ +This example illustrates how to use nested resource registration through application-level configuration. +The endpoints do nothing. + diff --git a/async_ex2/src/appconfig.rs b/async_ex2/src/appconfig.rs new file mode 100644 index 00000000..ae23b09c --- /dev/null +++ b/async_ex2/src/appconfig.rs @@ -0,0 +1,47 @@ +use actix_web::{error, web}; +use bytes::Bytes; +use futures::Stream; + +use crate::{ + handlers::{ + products, + parts + }, +}; + + +pub fn config_app

(cfg: &mut web::RouterConfig

) + where P: Stream + + 'static +{ + // 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)) + ) + .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)) + ) + .service( + web::scope("/parts") + .service( + web::resource("") + .route(web::get().to_async(parts::get_parts)) + .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)) + ) + ) + ) + ); +} \ No newline at end of file diff --git a/async_ex2/src/bin/main.rs b/async_ex2/src/bin/main.rs new file mode 100644 index 00000000..44c9c7e7 --- /dev/null +++ b/async_ex2/src/bin/main.rs @@ -0,0 +1,19 @@ +use actix_web::{middleware, App, HttpServer}; + +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(|| + App::new() + .configure(config_app) + .wrap(middleware::Logger::default()) + ) + .bind("127.0.0.1:8080")? + .run() +} \ No newline at end of file diff --git a/async_ex2/src/common.rs b/async_ex2/src/common.rs new file mode 100644 index 00000000..4359c2ec --- /dev/null +++ b/async_ex2/src/common.rs @@ -0,0 +1,17 @@ +use serde::{Deserialize, Serialize}; + + +#[derive(Deserialize, Serialize)] +pub struct Product { + id: Option, + product_type: Option, + name: Option, +} + + +#[derive(Deserialize, Serialize)] +pub struct Part { + id: Option, + part_type: Option, + name: Option, +} \ No newline at end of file diff --git a/async_ex2/src/handlers/mod.rs b/async_ex2/src/handlers/mod.rs new file mode 100644 index 00000000..17f9e2c2 --- /dev/null +++ b/async_ex2/src/handlers/mod.rs @@ -0,0 +1,3 @@ +pub mod products; +pub mod parts; + diff --git a/async_ex2/src/handlers/parts.rs b/async_ex2/src/handlers/parts.rs new file mode 100644 index 00000000..22bb017a --- /dev/null +++ b/async_ex2/src/handlers/parts.rs @@ -0,0 +1,26 @@ +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 crate::common::{Part, Product}; + + +pub fn get_parts(query: web::Query>) + -> impl Future { + fut_ok(HttpResponse::Ok().finish()) +} + +pub fn add_part(new_part: web::Json) + -> impl Future { + fut_ok(HttpResponse::Ok().finish()) +} + +pub fn get_part_detail(id: web::Path) + -> impl Future { + fut_ok(HttpResponse::Ok().finish()) +} + +pub fn remove_part(id: web::Path) + -> impl Future { + fut_ok(HttpResponse::Ok().finish()) +} \ No newline at end of file diff --git a/async_ex2/src/handlers/products.rs b/async_ex2/src/handlers/products.rs new file mode 100644 index 00000000..5d0cd2a9 --- /dev/null +++ b/async_ex2/src/handlers/products.rs @@ -0,0 +1,26 @@ +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 crate::common::{Part, Product}; + + +pub fn get_products(query: web::Query>) + -> impl Future { + fut_ok(HttpResponse::Ok().finish()) +} + +pub fn add_product(new_product: web::Json) + -> impl Future { + fut_ok(HttpResponse::Ok().finish()) +} + +pub fn get_product_detail(id: web::Path) + -> impl Future { + fut_ok(HttpResponse::Ok().finish()) +} + +pub fn remove_product(id: web::Path) + -> impl Future { + fut_ok(HttpResponse::Ok().finish()) +} \ No newline at end of file diff --git a/async_ex2/src/lib.rs b/async_ex2/src/lib.rs new file mode 100644 index 00000000..1e9c00b3 --- /dev/null +++ b/async_ex2/src/lib.rs @@ -0,0 +1,3 @@ +pub mod appconfig; +pub mod common; +pub mod handlers;