1
0
mirror of https://github.com/actix/examples synced 2025-06-29 10:14:58 +02:00

update protobuf example

This commit is contained in:
Rob Ede
2022-02-17 20:22:36 +00:00
parent ddb84ebf6f
commit 7857ac65f8
32 changed files with 57 additions and 68 deletions

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"
[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"

View File

@ -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
```

View File

@ -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()

View File

@ -14,22 +14,23 @@ pub struct MyObj {
}
async fn index(msg: ProtoBuf<MyObj>) -> Result<HttpResponse> {
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
}