mirror of
https://github.com/actix/examples
synced 2024-11-23 14:31:07 +01:00
update protobuf example
This commit is contained in:
parent
ddb84ebf6f
commit
7857ac65f8
37
Cargo.lock
generated
37
Cargo.lock
generated
@ -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",
|
||||
]
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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 || {
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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);
|
||||
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
}
|
||||
|
3
other/protobuf/.gitignore
vendored
Normal file
3
other/protobuf/.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
protobuf-python-3.11.2.zip
|
||||
protobuf-3.11.2/
|
||||
__pycache__/
|
@ -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"
|
||||
|
@ -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
|
||||
```
|
||||
|
@ -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()
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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()
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user