From 7857ac65f821b761a1b5fc5677bf5df1fdd0c656 Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Thu, 17 Feb 2022 20:22:36 +0000 Subject: [PATCH] update protobuf example --- Cargo.lock | 37 ++++++++--------------- basics/hello-world/src/main.rs | 2 +- basics/middleware/src/main.rs | 2 +- basics/nested-routing/src/bin/main.rs | 2 +- basics/shutdown-server/src/main.rs | 2 +- basics/state/src/main.rs | 2 +- basics/static_index/src/main.rs | 2 +- database_interactions/diesel/src/main.rs | 2 +- database_interactions/mongodb/src/main.rs | 2 +- forms/form/src/main.rs | 2 +- graphql/juniper-advanced/src/main.rs | 2 +- graphql/juniper/src/main.rs | 2 +- json/json/src/main.rs | 2 +- json/jsonrpc/src/main.rs | 2 +- other/data_factory/src/main.rs | 2 +- other/protobuf/.gitignore | 3 ++ other/protobuf/Cargo.toml | 13 ++++---- other/protobuf/README.md | 5 +-- other/protobuf/client.py | 2 +- other/protobuf/src/main.rs | 13 ++++---- security/casbin/src/main.rs | 2 +- session/cookie-auth/src/main.rs | 2 +- session/cookie-session/src/main.rs | 2 +- session/redis-session/src/main.rs | 2 +- template_engines/askama/src/main.rs | 2 +- template_engines/handlebars/src/main.rs | 2 +- template_engines/tera/src/main.rs | 2 +- template_engines/tinytemplate/src/main.rs | 2 +- template_engines/yarte/src/main.rs | 2 +- websockets/chat-broker/src/main.rs | 2 +- websockets/chat/src/main.rs | 2 +- websockets/websocket/src/main.rs | 2 +- 32 files changed, 57 insertions(+), 68 deletions(-) create mode 100644 other/protobuf/.gitignore diff --git a/Cargo.lock b/Cargo.lock index 978a7f7..df27a86 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -426,13 +426,11 @@ dependencies = [ [[package]] name = "actix-protobuf" -version = "0.6.0" +version = "0.7.0-beta.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "af362615b2f93a278580d218b6485cb2afa4e7a22cbc9ab36e44ba3d79c66967" +checksum = "c142e6182e45703d7197aaeb1e0096b21e9abe9626836b50eab6899d2f753bac" dependencies = [ - "actix-rt 1.1.1", - "actix-web 3.3.3", - "bytes 0.5.6", + "actix-web 4.0.0-rc.3", "derive_more", "futures-util", "prost", @@ -3207,15 +3205,6 @@ version = "2.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "68f2d64f2edebec4ce84ad108148e67e1064789bee435edc5b60ad398714a3a9" -[[package]] -name = "itertools" -version = "0.8.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f56a2d0bc861f9165be4eb3442afd3c236d8a98afd426f65d92324ae1091a484" -dependencies = [ - "either", -] - [[package]] name = "itertools" version = "0.10.3" @@ -4547,22 +4536,22 @@ dependencies = [ [[package]] name = "prost" -version = "0.6.1" +version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ce49aefe0a6144a45de32927c77bd2859a5f7677b55f220ae5b744e87389c212" +checksum = "444879275cb4fd84958b1a1d5420d15e6fcf7c235fe47f053c9c2a80aceb6001" dependencies = [ - "bytes 0.5.6", + "bytes 1.1.0", "prost-derive", ] [[package]] name = "prost-derive" -version = "0.6.1" +version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "537aa19b95acde10a12fec4301466386f757403de4cd4e5b4fa78fb5ecb18f72" +checksum = "f9cc1a3263e07e0bf68e96268f37665207b49560d98739662cdfaae215c720fe" dependencies = [ "anyhow", - "itertools 0.8.2", + "itertools", "proc-macro2", "quote", "syn", @@ -4572,11 +4561,11 @@ dependencies = [ name = "protobuf-example" version = "1.0.0" dependencies = [ - "actix 0.10.0", + "actix 0.12.0", "actix-protobuf", - "actix-web 3.3.3", - "bytes 0.5.6", + "actix-web 4.0.0-rc.3", "env_logger 0.9.0", + "log", "prost", "prost-derive", ] @@ -5684,7 +5673,7 @@ version = "0.1.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b4b7922be017ee70900be125523f38bdd644f4f06a1b16e8fa5a8ee8c34bffd4" dependencies = [ - "itertools 0.10.3", + "itertools", "nom 7.1.0", "unicode_categories", ] diff --git a/basics/hello-world/src/main.rs b/basics/hello-world/src/main.rs index 45df7f3..9f6ebe2 100644 --- a/basics/hello-world/src/main.rs +++ b/basics/hello-world/src/main.rs @@ -17,7 +17,7 @@ async fn main() -> std::io::Result<()> { .service(web::resource("/index.html").to(|| async { "Hello world!" })) .service(web::resource("/").to(index)) }) - .bind("127.0.0.1:8080")? + .bind(("127.0.0.1", 8080))? .run() .await } diff --git a/basics/middleware/src/main.rs b/basics/middleware/src/main.rs index 1a985cd..8f8dd94 100644 --- a/basics/middleware/src/main.rs +++ b/basics/middleware/src/main.rs @@ -36,7 +36,7 @@ async fn main() -> std::io::Result<()> { "Hello, middleware! Check the console where the server is run." })) }) - .bind("127.0.0.1:8080")? + .bind(("127.0.0.1", 8080))? .run() .await } diff --git a/basics/nested-routing/src/bin/main.rs b/basics/nested-routing/src/bin/main.rs index 9eb12ff..32f699a 100644 --- a/basics/nested-routing/src/bin/main.rs +++ b/basics/nested-routing/src/bin/main.rs @@ -12,7 +12,7 @@ async fn main() -> std::io::Result<()> { .configure(config_app) .wrap(middleware::Logger::default()) }) - .bind("127.0.0.1:8080")? + .bind(("127.0.0.1", 8080))? .run() .await } diff --git a/basics/shutdown-server/src/main.rs b/basics/shutdown-server/src/main.rs index c564c6c..f7f208f 100644 --- a/basics/shutdown-server/src/main.rs +++ b/basics/shutdown-server/src/main.rs @@ -23,7 +23,7 @@ async fn main() -> std::io::Result<()> { // create a channel let (tx, rx) = mpsc::channel::<()>(); - let bind = "127.0.0.1:8080"; + let bind = ("127.0.0.1", 8080); // start server as normal but don't .await after .run() yet let server = HttpServer::new(move || { diff --git a/basics/state/src/main.rs b/basics/state/src/main.rs index cf774df..4142463 100644 --- a/basics/state/src/main.rs +++ b/basics/state/src/main.rs @@ -76,7 +76,7 @@ async fn main() -> io::Result<()> { // register simple handler .service(web::resource("/").to(index)) }) - .bind("127.0.0.1:8080")? + .bind(("127.0.0.1", 8080))? .run() .await } diff --git a/basics/static_index/src/main.rs b/basics/static_index/src/main.rs index fab6eea..f82818b 100644 --- a/basics/static_index/src/main.rs +++ b/basics/static_index/src/main.rs @@ -18,7 +18,7 @@ async fn main() -> std::io::Result<()> { // path then the service for the static images would never be reached. .service(Files::new("/", "./static/root/").index_file("index.html")) }) - .bind("127.0.0.1:8080")? + .bind(("127.0.0.1", 8080))? .run() .await } diff --git a/database_interactions/diesel/src/main.rs b/database_interactions/diesel/src/main.rs index 610ab6c..05f5ac6 100644 --- a/database_interactions/diesel/src/main.rs +++ b/database_interactions/diesel/src/main.rs @@ -72,7 +72,7 @@ async fn main() -> std::io::Result<()> { .build(manager) .expect("Failed to create pool."); - let bind = "127.0.0.1:8080"; + let bind = ("127.0.0.1", 8080); println!("Starting server at: {}", &bind); diff --git a/database_interactions/mongodb/src/main.rs b/database_interactions/mongodb/src/main.rs index 66c9e0b..1aabb73 100644 --- a/database_interactions/mongodb/src/main.rs +++ b/database_interactions/mongodb/src/main.rs @@ -71,7 +71,7 @@ async fn main() -> std::io::Result<()> { .service(add_user) .service(get_user) }) - .bind("127.0.0.1:8080")? + .bind(("127.0.0.1", 8080))? .run() .await } diff --git a/forms/form/src/main.rs b/forms/form/src/main.rs index e7f6232..6f7f1d1 100644 --- a/forms/form/src/main.rs +++ b/forms/form/src/main.rs @@ -15,7 +15,7 @@ async fn main() -> std::io::Result<()> { .wrap(middleware::Logger::default()) .configure(app_config) }) - .bind("127.0.0.1:8080")? + .bind(("127.0.0.1", 8080))? .run() .await } diff --git a/graphql/juniper-advanced/src/main.rs b/graphql/juniper-advanced/src/main.rs index c7e6958..a52a413 100644 --- a/graphql/juniper-advanced/src/main.rs +++ b/graphql/juniper-advanced/src/main.rs @@ -27,7 +27,7 @@ async fn main() -> std::io::Result<()> { .configure(register) .default_service(web::to(|| async { "404" })) }) - .bind("127.0.0.1:8080")? + .bind(("127.0.0.1", 8080))? .run() .await } diff --git a/graphql/juniper/src/main.rs b/graphql/juniper/src/main.rs index 9d7ac40..69ac0e8 100644 --- a/graphql/juniper/src/main.rs +++ b/graphql/juniper/src/main.rs @@ -57,7 +57,7 @@ async fn main() -> io::Result<()> { .service(web::resource("/graphql").route(web::post().to(graphql))) .service(web::resource("/graphiql").route(web::get().to(graphiql))) }) - .bind("127.0.0.1:8080")? + .bind(("127.0.0.1", 8080))? .run() .await } diff --git a/json/json/src/main.rs b/json/json/src/main.rs index e297648..f9b1f5a 100644 --- a/json/json/src/main.rs +++ b/json/json/src/main.rs @@ -78,7 +78,7 @@ async fn main() -> std::io::Result<()> { .service(web::resource("/mjsonrust").route(web::post().to(index_mjsonrust))) .service(web::resource("/").route(web::post().to(index))) }) - .bind("127.0.0.1:8080")? + .bind(("127.0.0.1", 8080))? .run() .await } diff --git a/json/jsonrpc/src/main.rs b/json/jsonrpc/src/main.rs index d5477a7..18ea7a4 100644 --- a/json/jsonrpc/src/main.rs +++ b/json/jsonrpc/src/main.rs @@ -159,7 +159,7 @@ async fn main() -> std::io::Result<()> { .wrap(middleware::Logger::default()) .service(web::resource("/").route(web::post().to(rpc_handler))) }) - .bind("127.0.0.1:8080") + .bind(("127.0.0.1", 8080)) .unwrap() .run() .await diff --git a/other/data_factory/src/main.rs b/other/data_factory/src/main.rs index 2a64739..6ade486 100644 --- a/other/data_factory/src/main.rs +++ b/other/data_factory/src/main.rs @@ -48,7 +48,7 @@ async fn main() -> std::io::Result<()> { .service(pool_shared_prebuilt) .service(pool_local) }) - .bind("127.0.0.1:8080")? + .bind(("127.0.0.1", 8080))? .run() .await } diff --git a/other/protobuf/.gitignore b/other/protobuf/.gitignore new file mode 100644 index 0000000..977cd8c --- /dev/null +++ b/other/protobuf/.gitignore @@ -0,0 +1,3 @@ +protobuf-python-3.11.2.zip +protobuf-3.11.2/ +__pycache__/ diff --git a/other/protobuf/Cargo.toml b/other/protobuf/Cargo.toml index 69237ec..1bc7daa 100644 --- a/other/protobuf/Cargo.toml +++ b/other/protobuf/Cargo.toml @@ -4,11 +4,10 @@ version = "1.0.0" edition = "2021" [dependencies] -actix = "0.10" -actix-protobuf = "0.6" -actix-web = "3" - -bytes = "0.5.4" +actix = "0.12" +actix-protobuf = "0.7.0-beta.5" +actix-web = "4.0.0-rc.3" env_logger = "0.9" -prost = "0.6" -prost-derive = "0.6" +log = "0.4" +prost = "0.9" +prost-derive = "0.9" diff --git a/other/protobuf/README.md b/other/protobuf/README.md index 6452efb..5592e57 100644 --- a/other/protobuf/README.md +++ b/other/protobuf/README.md @@ -6,10 +6,6 @@ ```shell cd other/protobuf -# From workspace -cargo run --bin protobuf-example - -# From ./protobuf cargo run ``` @@ -25,5 +21,6 @@ pip3 install --upgrade pip pip3 install aiohttp # Client +cd ../.. python3 client.py ``` diff --git a/other/protobuf/client.py b/other/protobuf/client.py index f02c558..58d3203 100644 --- a/other/protobuf/client.py +++ b/other/protobuf/client.py @@ -48,7 +48,7 @@ async def fetch(session): obj = test_pb2.MyObj() obj.number = 9 obj.name = 'USB' - async with session.post('http://127.0.0.1:8081/', data=obj.SerializeToString(), + async with session.post('http://127.0.0.1:8080/', data=obj.SerializeToString(), headers={"content-type": "application/protobuf"}) as resp: print(resp.status) data = await resp.read() diff --git a/other/protobuf/src/main.rs b/other/protobuf/src/main.rs index e8cc79f..27bcf1b 100644 --- a/other/protobuf/src/main.rs +++ b/other/protobuf/src/main.rs @@ -14,22 +14,23 @@ pub struct MyObj { } async fn index(msg: ProtoBuf) -> Result { - println!("model: {:?}", msg); + log::info!("model: {:?}", msg); HttpResponse::Ok().protobuf(msg.0) // <- send response } #[actix_web::main] async fn main() -> std::io::Result<()> { - std::env::set_var("RUST_LOG", "actix_web=debug,actix_server=info"); - env_logger::init(); + env_logger::init_from_env(env_logger::Env::new().default_filter_or("info")); + + log::info!("starting HTTP server at http://localhost:8080"); HttpServer::new(|| { App::new() - .wrap(middleware::Logger::default()) .service(web::resource("/").route(web::post().to(index))) + .wrap(middleware::Logger::default()) }) - .bind("127.0.0.1:8081")? - .shutdown_timeout(1) + .workers(1) + .bind(("127.0.0.1", 8080))? .run() .await } diff --git a/security/casbin/src/main.rs b/security/casbin/src/main.rs index c6f81e3..e4166e2 100644 --- a/security/casbin/src/main.rs +++ b/security/casbin/src/main.rs @@ -49,7 +49,7 @@ async fn main() -> io::Result<()> { .service(web::resource("/success").to(success)) .service(web::resource("/fail").to(fail)) }) - .bind("127.0.0.1:8080")? + .bind(("127.0.0.1", 8080))? .run() .await } diff --git a/session/cookie-auth/src/main.rs b/session/cookie-auth/src/main.rs index 1e81eed..cb0490b 100644 --- a/session/cookie-auth/src/main.rs +++ b/session/cookie-auth/src/main.rs @@ -46,7 +46,7 @@ async fn main() -> std::io::Result<()> { .service(web::resource("/logout").to(logout)) .service(web::resource("/").route(web::get().to(index))) }) - .bind("127.0.0.1:8080")? + .bind(("127.0.0.1", 8080))? .run() .await } diff --git a/session/cookie-session/src/main.rs b/session/cookie-session/src/main.rs index 973936b..1dd5b0e 100644 --- a/session/cookie-session/src/main.rs +++ b/session/cookie-session/src/main.rs @@ -39,7 +39,7 @@ async fn main() -> std::io::Result<()> { .wrap(CookieSession::signed(&[0; 32]).secure(false)) .service(web::resource("/").to(index)) }) - .bind("127.0.0.1:8080")? + .bind(("127.0.0.1", 8080))? .run() .await } diff --git a/session/redis-session/src/main.rs b/session/redis-session/src/main.rs index 27c1c54..421751f 100644 --- a/session/redis-session/src/main.rs +++ b/session/redis-session/src/main.rs @@ -92,7 +92,7 @@ async fn main() -> std::io::Result<()> { .service(resource("/login").route(post().to(login))) .service(resource("/logout").route(post().to(logout))) }) - .bind("127.0.0.1:8080")? + .bind(("127.0.0.1", 8080))? .run() .await } diff --git a/template_engines/askama/src/main.rs b/template_engines/askama/src/main.rs index ca98cce..c62ad50 100644 --- a/template_engines/askama/src/main.rs +++ b/template_engines/askama/src/main.rs @@ -39,7 +39,7 @@ async fn main() -> std::io::Result<()> { .wrap(middleware::Logger::default()) .service(web::resource("/").route(web::get().to(index))) }) - .bind("127.0.0.1:8080")? + .bind(("127.0.0.1", 8080))? .run() .await } diff --git a/template_engines/handlebars/src/main.rs b/template_engines/handlebars/src/main.rs index f35a4eb..9975b70 100644 --- a/template_engines/handlebars/src/main.rs +++ b/template_engines/handlebars/src/main.rs @@ -52,7 +52,7 @@ async fn main() -> io::Result<()> { .service(index) .service(user) }) - .bind("127.0.0.1:8080")? + .bind(("127.0.0.1", 8080))? .run() .await } diff --git a/template_engines/tera/src/main.rs b/template_engines/tera/src/main.rs index 55e9eea..5e49583 100644 --- a/template_engines/tera/src/main.rs +++ b/template_engines/tera/src/main.rs @@ -42,7 +42,7 @@ async fn main() -> std::io::Result<()> { .service(web::resource("/").route(web::get().to(index))) .service(web::scope("").wrap(error_handlers())) }) - .bind("127.0.0.1:8080")? + .bind(("127.0.0.1", 8080))? .run() .await } diff --git a/template_engines/tinytemplate/src/main.rs b/template_engines/tinytemplate/src/main.rs index 67d4119..97187e6 100644 --- a/template_engines/tinytemplate/src/main.rs +++ b/template_engines/tinytemplate/src/main.rs @@ -46,7 +46,7 @@ async fn main() -> std::io::Result<()> { .service(web::resource("/").route(web::get().to(index))) .service(web::scope("").wrap(error_handlers())) }) - .bind("127.0.0.1:8080")? + .bind(("127.0.0.1", 8080))? .run() .await } diff --git a/template_engines/yarte/src/main.rs b/template_engines/yarte/src/main.rs index f5a8446..2ad1091 100644 --- a/template_engines/yarte/src/main.rs +++ b/template_engines/yarte/src/main.rs @@ -30,7 +30,7 @@ async fn main() -> std::io::Result<()> { // start http server HttpServer::new(move || App::new().wrap(Logger::default()).service(index)) - .bind("127.0.0.1:8080")? + .bind(("127.0.0.1", 8080))? .run() .await } diff --git a/websockets/chat-broker/src/main.rs b/websockets/chat-broker/src/main.rs index e7f9fe5..049363a 100644 --- a/websockets/chat-broker/src/main.rs +++ b/websockets/chat-broker/src/main.rs @@ -22,7 +22,7 @@ async fn main() -> std::io::Result<()> { env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info")) .init(); - let addr = "127.0.0.1:8080"; + let addr = ("127.0.0.1", 8080); let srv = HttpServer::new(move || { App::new() diff --git a/websockets/chat/src/main.rs b/websockets/chat/src/main.rs index 8fcf9b0..4bbd76a 100644 --- a/websockets/chat/src/main.rs +++ b/websockets/chat/src/main.rs @@ -258,7 +258,7 @@ async fn main() -> std::io::Result<()> { // static resources .service(fs::Files::new("/static/", "static/")) }) - .bind("127.0.0.1:8080")? + .bind(("127.0.0.1", 8080))? .run() .await } diff --git a/websockets/websocket/src/main.rs b/websockets/websocket/src/main.rs index 9006088..485db1e 100644 --- a/websockets/websocket/src/main.rs +++ b/websockets/websocket/src/main.rs @@ -110,7 +110,7 @@ async fn main() -> std::io::Result<()> { .service(fs::Files::new("/", "static/").index_file("index.html")) }) // start http server on 127.0.0.1:8080 - .bind("127.0.0.1:8080")? + .bind(("127.0.0.1", 8080))? .run() .await }