mirror of
https://github.com/actix/actix-extras.git
synced 2024-11-24 07:53:00 +01:00
updating example
This commit is contained in:
parent
5ab0474781
commit
90648058b4
10
examples/prost-example/client.py
Normal file → Executable file
10
examples/prost-example/client.py
Normal file → Executable file
@ -1,3 +1,4 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
# just start server and run client.py
|
# just start server and run client.py
|
||||||
|
|
||||||
# wget https://github.com/google/protobuf/releases/download/v3.5.1/protobuf-python-3.5.1.zip
|
# wget https://github.com/google/protobuf/releases/download/v3.5.1/protobuf-python-3.5.1.zip
|
||||||
@ -5,12 +6,11 @@
|
|||||||
# cd protobuf-3.5.1/python/
|
# cd protobuf-3.5.1/python/
|
||||||
# python3.6 setup.py install
|
# python3.6 setup.py install
|
||||||
|
|
||||||
# pip3.6 install --upgrade pip
|
# pip3 install --upgrade pip
|
||||||
# pip3.6 install aiohttp
|
# pip3 install aiohttp
|
||||||
|
|
||||||
# python3.6 client.py
|
# python3 client.py
|
||||||
|
|
||||||
#!/usr/bin/env python
|
|
||||||
import test_pb2
|
import test_pb2
|
||||||
import traceback
|
import traceback
|
||||||
import sys
|
import sys
|
||||||
@ -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://localhost:8080/', data=obj.SerializeToString(),
|
async with session.post('http://127.0.0.1:8081/', 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()
|
||||||
|
@ -1,43 +1,43 @@
|
|||||||
extern crate bytes;
|
|
||||||
extern crate actix;
|
extern crate actix;
|
||||||
extern crate actix_web;
|
|
||||||
extern crate actix_protobuf;
|
extern crate actix_protobuf;
|
||||||
|
extern crate actix_web;
|
||||||
|
extern crate bytes;
|
||||||
extern crate env_logger;
|
extern crate env_logger;
|
||||||
extern crate prost;
|
extern crate prost;
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate prost_derive;
|
extern crate prost_derive;
|
||||||
|
|
||||||
use actix_web::*;
|
|
||||||
use actix_protobuf::*;
|
use actix_protobuf::*;
|
||||||
|
use actix_web::*;
|
||||||
|
|
||||||
#[derive(Clone, PartialEq, Message)]
|
#[derive(Clone, PartialEq, Message)]
|
||||||
pub struct MyObj {
|
pub struct MyObj {
|
||||||
#[prost(int32, tag="1")]
|
#[prost(int32, tag = "1")]
|
||||||
pub number: i32,
|
pub number: i32,
|
||||||
#[prost(string, tag="2")]
|
#[prost(string, tag = "2")]
|
||||||
pub name: String,
|
pub name: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fn index(msg: ProtoBuf<MyObj>) -> Result<HttpResponse> {
|
fn index(msg: ProtoBuf<MyObj>) -> Result<HttpResponse> {
|
||||||
println!("model: {:?}", msg);
|
println!("model: {:?}", msg);
|
||||||
HttpResponse::Ok().protobuf(msg.0) // <- send response
|
HttpResponse::Ok().protobuf(msg.0) // <- send response
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
::std::env::set_var("RUST_LOG", "actix_web=info");
|
::std::env::set_var("RUST_LOG", "actix_web=info");
|
||||||
env_logger::init();
|
env_logger::init();
|
||||||
let sys = actix::System::new("prost-example");
|
let sys = actix::System::new("prost-example");
|
||||||
|
|
||||||
server::new(|| {
|
HttpServer::new(|| {
|
||||||
App::new()
|
App::new()
|
||||||
.middleware(middleware::Logger::default())
|
.wrap(middleware::Logger::default())
|
||||||
.resource("/", |r| r.method(http::Method::POST).with(index))})
|
.service(web::resource("/").route(web::post().to(index)))
|
||||||
.bind("127.0.0.1:8080").unwrap()
|
})
|
||||||
|
.bind("127.0.0.1:8081")
|
||||||
|
.unwrap()
|
||||||
.shutdown_timeout(1)
|
.shutdown_timeout(1)
|
||||||
.start();
|
.start();
|
||||||
|
|
||||||
println!("Started http server: 127.0.0.1:8080");
|
println!("Started http server: 127.0.0.1:8081");
|
||||||
let _ = sys.run();
|
let _ = sys.run();
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user