mirror of
https://github.com/actix/examples
synced 2024-11-23 14:31:07 +01:00
added async_ex2
This commit is contained in:
parent
4b62438e05
commit
d29f113ffc
2
.gitignore
vendored
2
.gitignore
vendored
@ -13,3 +13,5 @@ Cargo.lock
|
||||
|
||||
# intellij files
|
||||
.idea/**
|
||||
|
||||
.history/
|
||||
|
@ -5,6 +5,7 @@ members = [
|
||||
"actix_todo",
|
||||
"async_db",
|
||||
"async_ex1",
|
||||
"async_ex2",
|
||||
"basics",
|
||||
"cookie-auth",
|
||||
"cookie-session",
|
||||
|
20
async_ex2/Cargo.toml
Normal file
20
async_ex2/Cargo.toml
Normal file
@ -0,0 +1,20 @@
|
||||
[package]
|
||||
name = "async_ex2"
|
||||
version = "0.1.0"
|
||||
authors = ["dowwie <dkcdkg@gmail.com>"]
|
||||
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"
|
3
async_ex2/src/README.md
Normal file
3
async_ex2/src/README.md
Normal file
@ -0,0 +1,3 @@
|
||||
This example illustrates how to use nested resource registration through application-level configuration.
|
||||
The endpoints do nothing.
|
||||
|
47
async_ex2/src/appconfig.rs
Normal file
47
async_ex2/src/appconfig.rs
Normal file
@ -0,0 +1,47 @@
|
||||
use actix_web::{error, web};
|
||||
use bytes::Bytes;
|
||||
use futures::Stream;
|
||||
|
||||
use crate::{
|
||||
handlers::{
|
||||
products,
|
||||
parts
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
pub fn config_app<P>(cfg: &mut web::RouterConfig<P>)
|
||||
where P: Stream<Item = Bytes, Error = error::PayloadError>
|
||||
+ '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))
|
||||
)
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
19
async_ex2/src/bin/main.rs
Normal file
19
async_ex2/src/bin/main.rs
Normal file
@ -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()
|
||||
}
|
17
async_ex2/src/common.rs
Normal file
17
async_ex2/src/common.rs
Normal file
@ -0,0 +1,17 @@
|
||||
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>,
|
||||
}
|
3
async_ex2/src/handlers/mod.rs
Normal file
3
async_ex2/src/handlers/mod.rs
Normal file
@ -0,0 +1,3 @@
|
||||
pub mod products;
|
||||
pub mod parts;
|
||||
|
26
async_ex2/src/handlers/parts.rs
Normal file
26
async_ex2/src/handlers/parts.rs
Normal file
@ -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<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> {
|
||||
fut_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 fn remove_part(id: web::Path<String>)
|
||||
-> impl Future<Item = HttpResponse, Error = Error> {
|
||||
fut_ok(HttpResponse::Ok().finish())
|
||||
}
|
26
async_ex2/src/handlers/products.rs
Normal file
26
async_ex2/src/handlers/products.rs
Normal file
@ -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<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> {
|
||||
fut_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 fn remove_product(id: web::Path<String>)
|
||||
-> impl Future<Item = HttpResponse, Error = Error> {
|
||||
fut_ok(HttpResponse::Ok().finish())
|
||||
}
|
3
async_ex2/src/lib.rs
Normal file
3
async_ex2/src/lib.rs
Normal file
@ -0,0 +1,3 @@
|
||||
pub mod appconfig;
|
||||
pub mod common;
|
||||
pub mod handlers;
|
Loading…
Reference in New Issue
Block a user