mirror of
https://github.com/actix/examples
synced 2024-11-23 22:41:07 +01:00
Update protobuf to actix-web 2.0.0 (#249)
* Update to actix-web 2.0.0 * Add README.md
This commit is contained in:
parent
d59129c212
commit
443b8197ed
@ -21,7 +21,7 @@ members = [
|
||||
"multipart",
|
||||
"multipart-async-std",
|
||||
"openssl",
|
||||
# "protobuf",
|
||||
"protobuf",
|
||||
"r2d2",
|
||||
"redis",
|
||||
"redis-session",
|
||||
|
@ -1,18 +1,17 @@
|
||||
[package]
|
||||
name = "protobuf-example"
|
||||
version = "0.1.0"
|
||||
authors = ["kingxsp <jin_hb_zh@126.com>"]
|
||||
version = "0.2.0"
|
||||
authors = ["kingxsp <jin.hb.zh@outlook.com>, Yuki Okushi <huyuumi.dev@gmail.com>"]
|
||||
workspace = ".."
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
bytes = "0.4"
|
||||
futures = "0.1"
|
||||
env_logger = "0.7.0"
|
||||
derive_more = "0.15.0"
|
||||
prost = "0.5.0"
|
||||
prost-derive = "0.5.0"
|
||||
bytes = "0.5.4"
|
||||
env_logger = "0.7.1"
|
||||
prost = "0.6.1"
|
||||
prost-derive = "0.6.1"
|
||||
|
||||
actix = "0.8.3"
|
||||
actix-web = "1.0.8"
|
||||
actix-protobuf = "0.4.1"
|
||||
actix = "0.9.0"
|
||||
actix-protobuf = "0.5.0"
|
||||
actix-rt = "1.0.0"
|
||||
actix-web = "2.0.0"
|
||||
|
28
protobuf/README.md
Normal file
28
protobuf/README.md
Normal file
@ -0,0 +1,28 @@
|
||||
# protobuf
|
||||
|
||||
## Usage
|
||||
|
||||
### Server
|
||||
|
||||
```shell
|
||||
# From workspace
|
||||
cargo run --bin protobuf-example
|
||||
|
||||
# From ./protobuf
|
||||
cargo run
|
||||
```
|
||||
|
||||
### Client
|
||||
|
||||
```shell
|
||||
# Dependencies
|
||||
wget https://github.com/protocolbuffers/protobuf/releases/download/v3.11.2/protobuf-python-3.11.2.zip
|
||||
unzip protobuf-python-3.11.2.zip
|
||||
cd protobuf-3.11.2/python/
|
||||
python3 setup.py install
|
||||
pip3 install --upgrade pip
|
||||
pip3 install aiohttp
|
||||
|
||||
# Client
|
||||
python3 client.py
|
||||
```
|
@ -1,14 +1,16 @@
|
||||
#!/usr/bin/env python3
|
||||
# just start server and run client.py
|
||||
|
||||
# wget https://github.com/google/protobuf/releases/download/v3.5.1/protobuf-python-3.5.1.zip
|
||||
# unzip protobuf-python-3.5.1.zip.1
|
||||
# cd protobuf-3.5.1/python/
|
||||
# python3.6 setup.py install
|
||||
# wget https://github.com/protocolbuffers/protobuf/releases/download/v3.11.2/protobuf-python-3.11.2.zip
|
||||
# unzip protobuf-python-3.11.2.zip
|
||||
# cd protobuf-3.11.2/python/
|
||||
# python3 setup.py install
|
||||
|
||||
# pip3.6 install --upgrade pip
|
||||
# pip3.6 install aiohttp
|
||||
# pip3 install --upgrade pip
|
||||
# pip3 install aiohttp
|
||||
|
||||
# python3 client.py
|
||||
|
||||
#!/usr/bin/env python
|
||||
import test_pb2
|
||||
import traceback
|
||||
import sys
|
||||
@ -46,7 +48,7 @@ async def fetch(session):
|
||||
obj = test_pb2.MyObj()
|
||||
obj.number = 9
|
||||
obj.name = 'USB'
|
||||
async with session.post('http://localhost:8081/', data=obj.SerializeToString(),
|
||||
async with session.post('http://127.0.0.1:8081/', data=obj.SerializeToString(),
|
||||
headers={"content-type": "application/protobuf"}) as resp:
|
||||
print(resp.status)
|
||||
data = await resp.read()
|
||||
|
@ -1,9 +1,3 @@
|
||||
extern crate actix;
|
||||
extern crate actix_protobuf;
|
||||
extern crate actix_web;
|
||||
extern crate bytes;
|
||||
extern crate env_logger;
|
||||
extern crate prost;
|
||||
#[macro_use]
|
||||
extern crate prost_derive;
|
||||
|
||||
@ -18,26 +12,23 @@ pub struct MyObj {
|
||||
pub name: String,
|
||||
}
|
||||
|
||||
fn index(msg: ProtoBuf<MyObj>) -> Result<HttpResponse> {
|
||||
async fn index(msg: ProtoBuf<MyObj>) -> Result<HttpResponse> {
|
||||
println!("model: {:?}", msg);
|
||||
HttpResponse::Ok().protobuf(msg.0) // <- send response
|
||||
}
|
||||
|
||||
fn main() {
|
||||
::std::env::set_var("RUST_LOG", "actix_web=info,actix_server=info");
|
||||
#[actix_rt::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
std::env::set_var("RUST_LOG", "actix_web=debug,actix_server=info");
|
||||
env_logger::init();
|
||||
let sys = actix::System::new("protobuf-example");
|
||||
|
||||
HttpServer::new(|| {
|
||||
App::new()
|
||||
.wrap(middleware::Logger::default())
|
||||
.service(web::resource("/").route(web::post().to(index)))
|
||||
})
|
||||
.bind("127.0.0.1:8081")
|
||||
.unwrap()
|
||||
.bind("127.0.0.1:8081")?
|
||||
.shutdown_timeout(1)
|
||||
.start();
|
||||
|
||||
println!("Started http server: 127.0.0.1:8081");
|
||||
let _ = sys.run();
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
@ -1,13 +1,11 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Generated by the protocol buffer compiler. DO NOT EDIT!
|
||||
# source: test.proto
|
||||
|
||||
import sys
|
||||
_b=sys.version_info[0]<3 and (lambda x:x) or (lambda x:x.encode('latin1'))
|
||||
from google.protobuf import descriptor as _descriptor
|
||||
from google.protobuf import message as _message
|
||||
from google.protobuf import reflection as _reflection
|
||||
from google.protobuf import symbol_database as _symbol_database
|
||||
from google.protobuf import descriptor_pb2
|
||||
# @@protoc_insertion_point(imports)
|
||||
|
||||
_sym_db = _symbol_database.Default()
|
||||
@ -19,9 +17,9 @@ DESCRIPTOR = _descriptor.FileDescriptor(
|
||||
name='test.proto',
|
||||
package='',
|
||||
syntax='proto3',
|
||||
serialized_pb=_b('\n\ntest.proto\"%\n\x05MyObj\x12\x0e\n\x06number\x18\x01 \x01(\x05\x12\x0c\n\x04name\x18\x02 \x01(\tb\x06proto3')
|
||||
serialized_options=None,
|
||||
serialized_pb=b'\n\ntest.proto\"%\n\x05MyObj\x12\x0e\n\x06number\x18\x01 \x01(\x05\x12\x0c\n\x04name\x18\x02 \x01(\tb\x06proto3'
|
||||
)
|
||||
_sym_db.RegisterFileDescriptor(DESCRIPTOR)
|
||||
|
||||
|
||||
|
||||
@ -39,21 +37,21 @@ _MYOBJ = _descriptor.Descriptor(
|
||||
has_default_value=False, default_value=0,
|
||||
message_type=None, enum_type=None, containing_type=None,
|
||||
is_extension=False, extension_scope=None,
|
||||
options=None),
|
||||
serialized_options=None, file=DESCRIPTOR),
|
||||
_descriptor.FieldDescriptor(
|
||||
name='name', full_name='MyObj.name', index=1,
|
||||
number=2, type=9, cpp_type=9, label=1,
|
||||
has_default_value=False, default_value=_b("").decode('utf-8'),
|
||||
has_default_value=False, default_value=b"".decode('utf-8'),
|
||||
message_type=None, enum_type=None, containing_type=None,
|
||||
is_extension=False, extension_scope=None,
|
||||
options=None),
|
||||
serialized_options=None, file=DESCRIPTOR),
|
||||
],
|
||||
extensions=[
|
||||
],
|
||||
nested_types=[],
|
||||
enum_types=[
|
||||
],
|
||||
options=None,
|
||||
serialized_options=None,
|
||||
is_extendable=False,
|
||||
syntax='proto3',
|
||||
extension_ranges=[],
|
||||
@ -64,12 +62,13 @@ _MYOBJ = _descriptor.Descriptor(
|
||||
)
|
||||
|
||||
DESCRIPTOR.message_types_by_name['MyObj'] = _MYOBJ
|
||||
_sym_db.RegisterFileDescriptor(DESCRIPTOR)
|
||||
|
||||
MyObj = _reflection.GeneratedProtocolMessageType('MyObj', (_message.Message,), dict(
|
||||
DESCRIPTOR = _MYOBJ,
|
||||
__module__ = 'test_pb2'
|
||||
MyObj = _reflection.GeneratedProtocolMessageType('MyObj', (_message.Message,), {
|
||||
'DESCRIPTOR' : _MYOBJ,
|
||||
'__module__' : 'test_pb2'
|
||||
# @@protoc_insertion_point(class_scope:MyObj)
|
||||
))
|
||||
})
|
||||
_sym_db.RegisterMessage(MyObj)
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user