1
0
mirror of https://github.com/actix/examples synced 2024-11-23 22:41:07 +01:00

update protobuf example

This commit is contained in:
Rob Ede 2022-02-17 20:22:36 +00:00
parent ddb84ebf6f
commit 7857ac65f8
No known key found for this signature in database
GPG Key ID: 97C636207D3EF933
32 changed files with 57 additions and 68 deletions

37
Cargo.lock generated
View File

@ -426,13 +426,11 @@ dependencies = [
[[package]] [[package]]
name = "actix-protobuf" name = "actix-protobuf"
version = "0.6.0" version = "0.7.0-beta.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "af362615b2f93a278580d218b6485cb2afa4e7a22cbc9ab36e44ba3d79c66967" checksum = "c142e6182e45703d7197aaeb1e0096b21e9abe9626836b50eab6899d2f753bac"
dependencies = [ dependencies = [
"actix-rt 1.1.1", "actix-web 4.0.0-rc.3",
"actix-web 3.3.3",
"bytes 0.5.6",
"derive_more", "derive_more",
"futures-util", "futures-util",
"prost", "prost",
@ -3207,15 +3205,6 @@ version = "2.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "68f2d64f2edebec4ce84ad108148e67e1064789bee435edc5b60ad398714a3a9" checksum = "68f2d64f2edebec4ce84ad108148e67e1064789bee435edc5b60ad398714a3a9"
[[package]]
name = "itertools"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f56a2d0bc861f9165be4eb3442afd3c236d8a98afd426f65d92324ae1091a484"
dependencies = [
"either",
]
[[package]] [[package]]
name = "itertools" name = "itertools"
version = "0.10.3" version = "0.10.3"
@ -4547,22 +4536,22 @@ dependencies = [
[[package]] [[package]]
name = "prost" name = "prost"
version = "0.6.1" version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ce49aefe0a6144a45de32927c77bd2859a5f7677b55f220ae5b744e87389c212" checksum = "444879275cb4fd84958b1a1d5420d15e6fcf7c235fe47f053c9c2a80aceb6001"
dependencies = [ dependencies = [
"bytes 0.5.6", "bytes 1.1.0",
"prost-derive", "prost-derive",
] ]
[[package]] [[package]]
name = "prost-derive" name = "prost-derive"
version = "0.6.1" version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "537aa19b95acde10a12fec4301466386f757403de4cd4e5b4fa78fb5ecb18f72" checksum = "f9cc1a3263e07e0bf68e96268f37665207b49560d98739662cdfaae215c720fe"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"itertools 0.8.2", "itertools",
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn", "syn",
@ -4572,11 +4561,11 @@ dependencies = [
name = "protobuf-example" name = "protobuf-example"
version = "1.0.0" version = "1.0.0"
dependencies = [ dependencies = [
"actix 0.10.0", "actix 0.12.0",
"actix-protobuf", "actix-protobuf",
"actix-web 3.3.3", "actix-web 4.0.0-rc.3",
"bytes 0.5.6",
"env_logger 0.9.0", "env_logger 0.9.0",
"log",
"prost", "prost",
"prost-derive", "prost-derive",
] ]
@ -5684,7 +5673,7 @@ version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b4b7922be017ee70900be125523f38bdd644f4f06a1b16e8fa5a8ee8c34bffd4" checksum = "b4b7922be017ee70900be125523f38bdd644f4f06a1b16e8fa5a8ee8c34bffd4"
dependencies = [ dependencies = [
"itertools 0.10.3", "itertools",
"nom 7.1.0", "nom 7.1.0",
"unicode_categories", "unicode_categories",
] ]

View File

@ -17,7 +17,7 @@ async fn main() -> std::io::Result<()> {
.service(web::resource("/index.html").to(|| async { "Hello world!" })) .service(web::resource("/index.html").to(|| async { "Hello world!" }))
.service(web::resource("/").to(index)) .service(web::resource("/").to(index))
}) })
.bind("127.0.0.1:8080")? .bind(("127.0.0.1", 8080))?
.run() .run()
.await .await
} }

View File

@ -36,7 +36,7 @@ async fn main() -> std::io::Result<()> {
"Hello, middleware! Check the console where the server is run." "Hello, middleware! Check the console where the server is run."
})) }))
}) })
.bind("127.0.0.1:8080")? .bind(("127.0.0.1", 8080))?
.run() .run()
.await .await
} }

View File

@ -12,7 +12,7 @@ async fn main() -> std::io::Result<()> {
.configure(config_app) .configure(config_app)
.wrap(middleware::Logger::default()) .wrap(middleware::Logger::default())
}) })
.bind("127.0.0.1:8080")? .bind(("127.0.0.1", 8080))?
.run() .run()
.await .await
} }

View File

@ -23,7 +23,7 @@ async fn main() -> std::io::Result<()> {
// create a channel // create a channel
let (tx, rx) = mpsc::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 // start server as normal but don't .await after .run() yet
let server = HttpServer::new(move || { let server = HttpServer::new(move || {

View File

@ -76,7 +76,7 @@ async fn main() -> io::Result<()> {
// register simple handler // register simple handler
.service(web::resource("/").to(index)) .service(web::resource("/").to(index))
}) })
.bind("127.0.0.1:8080")? .bind(("127.0.0.1", 8080))?
.run() .run()
.await .await
} }

View File

@ -18,7 +18,7 @@ async fn main() -> std::io::Result<()> {
// path then the service for the static images would never be reached. // path then the service for the static images would never be reached.
.service(Files::new("/", "./static/root/").index_file("index.html")) .service(Files::new("/", "./static/root/").index_file("index.html"))
}) })
.bind("127.0.0.1:8080")? .bind(("127.0.0.1", 8080))?
.run() .run()
.await .await
} }

View File

@ -72,7 +72,7 @@ async fn main() -> std::io::Result<()> {
.build(manager) .build(manager)
.expect("Failed to create pool."); .expect("Failed to create pool.");
let bind = "127.0.0.1:8080"; let bind = ("127.0.0.1", 8080);
println!("Starting server at: {}", &bind); println!("Starting server at: {}", &bind);

View File

@ -71,7 +71,7 @@ async fn main() -> std::io::Result<()> {
.service(add_user) .service(add_user)
.service(get_user) .service(get_user)
}) })
.bind("127.0.0.1:8080")? .bind(("127.0.0.1", 8080))?
.run() .run()
.await .await
} }

View File

@ -15,7 +15,7 @@ async fn main() -> std::io::Result<()> {
.wrap(middleware::Logger::default()) .wrap(middleware::Logger::default())
.configure(app_config) .configure(app_config)
}) })
.bind("127.0.0.1:8080")? .bind(("127.0.0.1", 8080))?
.run() .run()
.await .await
} }

View File

@ -27,7 +27,7 @@ async fn main() -> std::io::Result<()> {
.configure(register) .configure(register)
.default_service(web::to(|| async { "404" })) .default_service(web::to(|| async { "404" }))
}) })
.bind("127.0.0.1:8080")? .bind(("127.0.0.1", 8080))?
.run() .run()
.await .await
} }

View File

@ -57,7 +57,7 @@ async fn main() -> io::Result<()> {
.service(web::resource("/graphql").route(web::post().to(graphql))) .service(web::resource("/graphql").route(web::post().to(graphql)))
.service(web::resource("/graphiql").route(web::get().to(graphiql))) .service(web::resource("/graphiql").route(web::get().to(graphiql)))
}) })
.bind("127.0.0.1:8080")? .bind(("127.0.0.1", 8080))?
.run() .run()
.await .await
} }

View File

@ -78,7 +78,7 @@ async fn main() -> std::io::Result<()> {
.service(web::resource("/mjsonrust").route(web::post().to(index_mjsonrust))) .service(web::resource("/mjsonrust").route(web::post().to(index_mjsonrust)))
.service(web::resource("/").route(web::post().to(index))) .service(web::resource("/").route(web::post().to(index)))
}) })
.bind("127.0.0.1:8080")? .bind(("127.0.0.1", 8080))?
.run() .run()
.await .await
} }

View File

@ -159,7 +159,7 @@ async fn main() -> std::io::Result<()> {
.wrap(middleware::Logger::default()) .wrap(middleware::Logger::default())
.service(web::resource("/").route(web::post().to(rpc_handler))) .service(web::resource("/").route(web::post().to(rpc_handler)))
}) })
.bind("127.0.0.1:8080") .bind(("127.0.0.1", 8080))
.unwrap() .unwrap()
.run() .run()
.await .await

View File

@ -48,7 +48,7 @@ async fn main() -> std::io::Result<()> {
.service(pool_shared_prebuilt) .service(pool_shared_prebuilt)
.service(pool_local) .service(pool_local)
}) })
.bind("127.0.0.1:8080")? .bind(("127.0.0.1", 8080))?
.run() .run()
.await .await
} }

3
other/protobuf/.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
protobuf-python-3.11.2.zip
protobuf-3.11.2/
__pycache__/

View File

@ -4,11 +4,10 @@ version = "1.0.0"
edition = "2021" edition = "2021"
[dependencies] [dependencies]
actix = "0.10" actix = "0.12"
actix-protobuf = "0.6" actix-protobuf = "0.7.0-beta.5"
actix-web = "3" actix-web = "4.0.0-rc.3"
bytes = "0.5.4"
env_logger = "0.9" env_logger = "0.9"
prost = "0.6" log = "0.4"
prost-derive = "0.6" prost = "0.9"
prost-derive = "0.9"

View File

@ -6,10 +6,6 @@
```shell ```shell
cd other/protobuf cd other/protobuf
# From workspace
cargo run --bin protobuf-example
# From ./protobuf
cargo run cargo run
``` ```
@ -25,5 +21,6 @@ pip3 install --upgrade pip
pip3 install aiohttp pip3 install aiohttp
# Client # Client
cd ../..
python3 client.py python3 client.py
``` ```

View File

@ -48,7 +48,7 @@ async def fetch(session):
obj = test_pb2.MyObj() obj = test_pb2.MyObj()
obj.number = 9 obj.number = 9
obj.name = 'USB' 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: headers={"content-type": "application/protobuf"}) as resp:
print(resp.status) print(resp.status)
data = await resp.read() data = await resp.read()

View File

@ -14,22 +14,23 @@ pub struct MyObj {
} }
async fn index(msg: ProtoBuf<MyObj>) -> Result<HttpResponse> { async fn index(msg: ProtoBuf<MyObj>) -> Result<HttpResponse> {
println!("model: {:?}", msg); log::info!("model: {:?}", msg);
HttpResponse::Ok().protobuf(msg.0) // <- send response HttpResponse::Ok().protobuf(msg.0) // <- send response
} }
#[actix_web::main] #[actix_web::main]
async fn main() -> std::io::Result<()> { async fn main() -> std::io::Result<()> {
std::env::set_var("RUST_LOG", "actix_web=debug,actix_server=info"); env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
env_logger::init();
log::info!("starting HTTP server at http://localhost:8080");
HttpServer::new(|| { HttpServer::new(|| {
App::new() App::new()
.wrap(middleware::Logger::default())
.service(web::resource("/").route(web::post().to(index))) .service(web::resource("/").route(web::post().to(index)))
.wrap(middleware::Logger::default())
}) })
.bind("127.0.0.1:8081")? .workers(1)
.shutdown_timeout(1) .bind(("127.0.0.1", 8080))?
.run() .run()
.await .await
} }

View File

@ -49,7 +49,7 @@ async fn main() -> io::Result<()> {
.service(web::resource("/success").to(success)) .service(web::resource("/success").to(success))
.service(web::resource("/fail").to(fail)) .service(web::resource("/fail").to(fail))
}) })
.bind("127.0.0.1:8080")? .bind(("127.0.0.1", 8080))?
.run() .run()
.await .await
} }

View File

@ -46,7 +46,7 @@ async fn main() -> std::io::Result<()> {
.service(web::resource("/logout").to(logout)) .service(web::resource("/logout").to(logout))
.service(web::resource("/").route(web::get().to(index))) .service(web::resource("/").route(web::get().to(index)))
}) })
.bind("127.0.0.1:8080")? .bind(("127.0.0.1", 8080))?
.run() .run()
.await .await
} }

View File

@ -39,7 +39,7 @@ async fn main() -> std::io::Result<()> {
.wrap(CookieSession::signed(&[0; 32]).secure(false)) .wrap(CookieSession::signed(&[0; 32]).secure(false))
.service(web::resource("/").to(index)) .service(web::resource("/").to(index))
}) })
.bind("127.0.0.1:8080")? .bind(("127.0.0.1", 8080))?
.run() .run()
.await .await
} }

View File

@ -92,7 +92,7 @@ async fn main() -> std::io::Result<()> {
.service(resource("/login").route(post().to(login))) .service(resource("/login").route(post().to(login)))
.service(resource("/logout").route(post().to(logout))) .service(resource("/logout").route(post().to(logout)))
}) })
.bind("127.0.0.1:8080")? .bind(("127.0.0.1", 8080))?
.run() .run()
.await .await
} }

View File

@ -39,7 +39,7 @@ async fn main() -> std::io::Result<()> {
.wrap(middleware::Logger::default()) .wrap(middleware::Logger::default())
.service(web::resource("/").route(web::get().to(index))) .service(web::resource("/").route(web::get().to(index)))
}) })
.bind("127.0.0.1:8080")? .bind(("127.0.0.1", 8080))?
.run() .run()
.await .await
} }

View File

@ -52,7 +52,7 @@ async fn main() -> io::Result<()> {
.service(index) .service(index)
.service(user) .service(user)
}) })
.bind("127.0.0.1:8080")? .bind(("127.0.0.1", 8080))?
.run() .run()
.await .await
} }

View File

@ -42,7 +42,7 @@ async fn main() -> std::io::Result<()> {
.service(web::resource("/").route(web::get().to(index))) .service(web::resource("/").route(web::get().to(index)))
.service(web::scope("").wrap(error_handlers())) .service(web::scope("").wrap(error_handlers()))
}) })
.bind("127.0.0.1:8080")? .bind(("127.0.0.1", 8080))?
.run() .run()
.await .await
} }

View File

@ -46,7 +46,7 @@ async fn main() -> std::io::Result<()> {
.service(web::resource("/").route(web::get().to(index))) .service(web::resource("/").route(web::get().to(index)))
.service(web::scope("").wrap(error_handlers())) .service(web::scope("").wrap(error_handlers()))
}) })
.bind("127.0.0.1:8080")? .bind(("127.0.0.1", 8080))?
.run() .run()
.await .await
} }

View File

@ -30,7 +30,7 @@ async fn main() -> std::io::Result<()> {
// start http server // start http server
HttpServer::new(move || App::new().wrap(Logger::default()).service(index)) HttpServer::new(move || App::new().wrap(Logger::default()).service(index))
.bind("127.0.0.1:8080")? .bind(("127.0.0.1", 8080))?
.run() .run()
.await .await
} }

View File

@ -22,7 +22,7 @@ async fn main() -> std::io::Result<()> {
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info")) env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info"))
.init(); .init();
let addr = "127.0.0.1:8080"; let addr = ("127.0.0.1", 8080);
let srv = HttpServer::new(move || { let srv = HttpServer::new(move || {
App::new() App::new()

View File

@ -258,7 +258,7 @@ async fn main() -> std::io::Result<()> {
// static resources // static resources
.service(fs::Files::new("/static/", "static/")) .service(fs::Files::new("/static/", "static/"))
}) })
.bind("127.0.0.1:8080")? .bind(("127.0.0.1", 8080))?
.run() .run()
.await .await
} }

View File

@ -110,7 +110,7 @@ async fn main() -> std::io::Result<()> {
.service(fs::Files::new("/", "static/").index_file("index.html")) .service(fs::Files::new("/", "static/").index_file("index.html"))
}) })
// start http server on 127.0.0.1:8080 // start http server on 127.0.0.1:8080
.bind("127.0.0.1:8080")? .bind(("127.0.0.1", 8080))?
.run() .run()
.await .await
} }