半梦半醒是什么意思| fan是什么意思| 脑梗怎么形成的原因是什么| 部队指导员是什么级别| 八髎区疼是什么原因| maga是什么意思| 什么叫窦性心律| 胰腺上长瘤意味着什么| 版心是什么| emba是什么| 难以启齿是什么意思| 香港商务签证需要什么条件| 吃得什么填词语| 什么是膝关节退行性变| 高育良什么级别| 绝情是什么意思| bally什么牌子| fs是什么单位| 懊恼是什么意思| 碳酸钠是什么东西| 鹿晗女朋友叫什么名字| 喷字去掉口念什么| 头是什么意思| 嗓子疼是什么原因| 四月七号是什么星座| 儿化音是什么意思| 为什么会得扁平疣| 发烧吃什么水果| 什么人容易得眩晕症| 怀孕第一个月有什么特征| 口加才是什么字| crpa是什么细菌| 便秘是什么意思| 政委是什么军衔| 独角仙长什么样| 血压低有什么危害| 人生得意须尽欢什么意思| 舌头痒是什么原因| 什么是囊肿| 烤冷面是什么做的| 什么时候初伏第一天| 可卡因是什么| 福报是什么意思| 共青团书记是什么级别| dx是什么| 金粉是什么| 梅毒通过什么途径传染| 射精出血是什么原因| 层峦叠翠的意思是什么| 夏末是什么时候| 物质是什么| 宫颈囊肿多发是什么意思| 男孩小名叫什么好听| 办理住院手续需要带什么证件| 对什么有好处| 治疗幽门螺杆菌用什么药效果最好| 立刀旁的字和什么有关| 脑白质脱髓鞘改变是什么意思| 海怪是什么海鲜| 火龙果有什么功效| 暨怎么读什么意思| 六六无穷是什么意思| 三十三天都是什么天| 喝什么睡眠效果最好| 风湿三项检查是什么| 猫什么时候发情| 办身份证要带什么| 熬粥用什么锅好| 女人梦见蛇预示着什么| 耳朵响是什么原因引起的| 月经几个月不来是什么原因| 憩室是什么病| 一个家庭最重要的是什么| mango是什么意思| 双肾实质回声增强是什么意思| 高血糖吃什么菜好| 阴茎进入阴道是什么感觉| 湿毒吃什么药最有效| 中医康复技术学什么| 生产方式是什么| 常规是什么意思| 什么矿泉水比较好| 什么叫风湿| 除牛反绒是什么意思| 什么药治痔疮最快| 手臂内侧是什么经络| 何首乌长什么样子| 余事勿取 什么意思| 空调开不了机是什么原因| 儿童病毒感染吃什么药| 奥倍健是什么药| 步步为营是什么意思| egm是什么意思| 隐翅虫咬到擦什么药膏| 双肾泥沙样结石是什么意思| 什么的月季| 为什么精液是黄色的| hbv是什么意思| 什么叫慢阻肺| 一加一为什么等于二| 皮肤晒伤用什么药| 小孩热感冒吃什么药好| 11月2日是什么星座| 安眠药有什么副作用| 心律不齐吃什么药好| 4月1号什么星座| 迪士尼狗狗叫什么名字| 咽喉痛吃什么药好得快| rr医学上什么意思| 黑毛茶是什么茶| 睡觉口干是什么原因| 彩超无回声是什么意思| 贤惠是什么意思| 借您吉言什么意思| 7月13日什么星座| 宫颈lsil是什么意思| 十二指肠球部溃疡吃什么药| 好事多磨是什么意思| 乙肝两对半245阳性是什么意思| 苹果是什么季节的水果| 微信屏蔽是什么意思| 子宫内膜脱落是什么原因| 什么是癔症| 白细胞低是什么原因引起的| 羊日冲牛是什么意思| 张飞的武器是什么| 彰字五行属什么| c肽测定是什么意思| 脂肪肝有什么危害| dw手表属于什么档次| 灰指甲长什么样子图片| 飞的第一笔是什么| 天王表属于什么档次| 咳嗽吃什么食物好得快| 绝育是什么意思| 一个骨一个宽是什么字| 肠绞痛吃什么药| 555是什么烟| 王者风范是什么意思| 鱼肝油又叫什么名字| 水瓶座男生喜欢什么样的女生| 祸起萧墙的萧墙指什么| 女性痔疮挂什么科室| 肺气肿有什么症状| 鸡蛋壳薄是什么原因| 甲亢不能吃什么| 胃胀气吃什么药见效快| 女人出轨有什么表现| 吃东西就打嗝是什么原因| 筋道是什么意思| beyond是什么意思| 六味地黄丸是治什么病| 0是什么| spa什么服务| 30是什么意思| 一九八七年属什么生肖| 雨水是什么季节| 全身spa是什么意思| 红细胞压积是什么意思| b超属于什么科室| 梦见小兔子是什么意思| 藕是莲的什么部位| 蛇为什么怕雄黄| 水色是什么颜色| 4.12是什么星座| 初三什么时候毕业| 寒潮是什么| 葡萄什么时候成熟| 胸口闷痛什么原因引起的| 擦枪走火什么意思| 怪力乱神是什么意思| 荆芥不能和什么一起吃| 鸟语花香是什么生肖| 浸润是什么意思| 车抛锚是什么意思| 屋尘螨是什么东西| 什么叫桑拿| 乳房疼吃什么药| 早餐吃什么营养又健康| 口甜是什么原因引起的| 在家做什么小生意| 朱元璋为什么杀徐达| 白带像豆腐渣用什么药| 芒种是什么意思| 得偿所愿什么意思| 护手霜什么牌子的效果好| 笑气是什么东西| 靶子是什么意思| 给男朋友买什么礼物比较好| 癫疯是什么原因引起| 横纹肌溶解是什么意思| 预拌粉是什么东西| 雷同是什么意思| 靶器官是什么意思| nike是什么牌子| 甲状腺病变是什么意思| 承欢膝下什么意思| 女人经期吃什么食物好| 胆经不通吃什么中成药| 擒贼先擒王是什么生肖| 护肝养肝吃什么好| 尿素酶阳性什么意思| 盲点是什么意思| 老人怕冷是什么原因| 前途是什么意思| 生肖猴和什么生肖相冲| 终板炎是什么病| 高胆固醇血症是什么病| 蜘蛛属于什么类动物| 葡萄糖有什么作用| 阳痿有什么症状| 湖北有什么好吃的| ca是什么元素| 荆州是现在的什么地方| 左眼跳什么预兆| bu什么颜色| 什么是鸡皮肤图片| 安宫牛黄丸适合什么人群吃| 女人喝什么茶减肥好| 小孩喜欢趴着睡觉是什么原因| 梦到谈恋爱预示着什么| 工会副主席是什么级别| 植物光合作用产生什么| 11月18日是什么星座| 仓鼠和老鼠有什么区别| 什么药可以降尿酸| 五行属性是什么| 梦见很多蜜蜂是什么意思| 三教九流代表什么生肖| dumpling是什么意思| 乳酸菌和益生菌有什么区别| 眼睛充血用什么眼药水好| 言外之意什么意思| 白色舌苔厚是什么原因| 什么牌子皮带结实耐用| 花枝招展是什么意思| 江西的简称是什么| 勃艮第红是什么颜色| 高什么远什么| 反清复明的组织叫什么| 结婚长明灯有什么讲究| 小年是什么时候| 棠字五行属什么| 阴疽是什么病| 丁桂鱼吃什么食物| 君子菜是什么蔬菜| 口若悬河是什么生肖| 晚饭吃什么好| 小腹胀是什么原因女性| 菜籽油是什么菜籽做的| 回奶什么意思| 失聪是什么意思| 吃什么才能减肥最快| 公安局局长什么级别| 故人什么意思| 心脏缺血吃什么补得快| 分明的意思是什么| 另煎兑服是什么意思| 南瓜可以做什么美食| 脸部爱出油是什么原因| via什么意思| 可塑性是什么意思| olay是什么档次| 放生是什么意思| 百度
Skip to content

github/twirp-rs

Repository files navigation

twirp

Twirp is an RPC protocol based on HTTP and Protocol Buffers (proto). The protocol uses HTTP URLs to specify the RPC endpoints, and sends/receives proto messages as HTTP request/response bodies. Services are defined in a .proto file, allowing easy implementation of RPC services with auto-generated clients and servers in different languages.

The canonical implementation is in Go, and this is a Rust implementation of the protocol. Rust protocol buffer support is provided by the prost ecosystem.

Unlike prost-twirp, the generated traits for serving and accessing RPCs are implemented atop async functions. Because traits containing async functions are not directly supported in Rust versions prior to 1.75, this crate uses the async_trait macro to encapsulate the scaffolding required to make them work.

Usage

See the example for a complete example project.

Define services and messages in a .proto file:

// service.proto
package service.haberdash.v1;

service HaberdasherAPI {
   rpc MakeHat(MakeHatRequest) returns (MakeHatResponse);
}
message MakeHatRequest { }
message MakeHatResponse { }

Add the twirp-build crate as a build dependency in your Cargo.toml (you'll need prost-build too):

# Cargo.toml
[build-dependencies]
twirp-build = "0.7"
prost-build = "0.13"

Add a build.rs file to your project to compile the protos and generate Rust code:

fn main() {
    let proto_source_files = ["./service.proto"];

    // Tell Cargo to rerun this build script if any of the proto files change
    for entry in &proto_source_files {
        println!("cargo:rerun-if-changed={}", entry);
    }

    prost_build::Config::new()
        .type_attribute(".", "#[derive(serde::Serialize, serde::Deserialize)]") // enable support for JSON encoding
        .service_generator(twirp_build::service_generator())
        .compile_protos(&proto_source_files, &["./"])
        .expect("error compiling protos");
}

This generates code that you can find in target/build/your-project-*/out/example.service.rs. In order to use this code, you'll need to implement the trait for the proto defined service and wire up the service handlers to a hyper web server. See the example for details.

Include the generated code, create a router, register your service, and then serve those routes in the hyper server:

mod haberdash {
    include!(concat!(env!("OUT_DIR"), "/service.haberdash.v1.rs"));
}

use axum::Router;
use haberdash::{MakeHatRequest, MakeHatResponse};

#[tokio::main]
pub async fn main() {
    let api_impl = Arc::new(HaberdasherApiServer {});
    let app = Router::new()
        .nest("/twirp", haberdash::router(api_impl))
        .fallback(twirp::server::not_found_handler);

    let tcp_listener = tokio::net::TcpListener::bind("127.0.0.1:3000").await.unwrap();
    if let Err(e) = axum::serve(tcp_listener, app).await {
        eprintln!("server error: {}", e);
    }
}

// Define the server and implement the trait.
struct HaberdasherApiServer;

#[async_trait]
impl haberdash::HaberdasherApi for HaberdasherApiServer {
    async fn make_hat(&self, req: twirp::Request<MakeHatRequest>) -> twirp::Result<twirp::Response<MakeHatResponse>> {
        todo!()
    }
}

This code creates an axum::Router, then hands it off to axum::serve() to handle networking. This use of axum::serve is optional. After building app, you can instead invoke it from any hyper-based server by importing twirp::tower::Service and doing app.call(request).await.

Usage (client side)

On the client side, you also get a generated twirp client (based on the rpc endpoints in your proto). Include the generated code, create a client, and start making rpc calls:

mod haberdash {
    include!(concat!(env!("OUT_DIR"), "/service.haberdash.v1.rs"));
}

use haberdash::{HaberdasherApiClient, MakeHatRequest, MakeHatResponse};

#[tokio::main]
pub async fn main() {
    let client = Client::from_base_url(Url::parse("http://localhost:3000/twirp/")?)?;
    let resp = client.make_hat(MakeHatRequest { inches: 1 }).await;
    eprintln!("{:?}", resp);
}

Minimum supported Rust version

The MSRV for this crate is the version defined in rust-toolchain.toml

Getting Help

You are welcome to open an issue with your question.

Contributing

?? Thanks for your help improving the project! We are so happy to have you! We have a contributing guide to help you get involved in the project.

License

This project is licensed under the MIT license.

About

Twirp RPC for Rust

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Contributors 16

头晕晕的是什么原因 为什么会牙龈出血 吃什么容易长高 什么虎不吃人 总胆固醇偏高吃什么药
什么叫青光眼 人活着到底有什么意义 用醋泡脚有什么好处 BCG是什么意思 什么是蒸馏水
精不液化是什么原因导致的 早上起来嘴巴发苦是什么原因 自负什么意思 结节性红斑是什么病 圆脸适合什么发型短发
茶叶杀青是什么意思 兰花代表什么象征意义 投喂是什么意思 甚好是什么意思 梦见大水牛是什么兆头
外科看什么病hcv9jop5ns7r.cn 久卧伤气是什么意思hcv9jop4ns7r.cn 为什么出汗有酸臭味hcv9jop0ns7r.cn 女人下身干燥无水是什么原因hcv8jop7ns2r.cn 甲钴胺片治什么病hcv9jop5ns1r.cn
打醮是什么意思hcv7jop5ns3r.cn 胆囊切除后需要注意什么hcv8jop1ns8r.cn 阑尾炎是什么原因引起的hcv7jop6ns8r.cn 溃烂用什么药治愈最快hcv8jop9ns8r.cn 嫡长子是什么意思hcv8jop1ns4r.cn
慢性胃炎吃什么好hcv9jop0ns0r.cn 突然不硬是什么原因hcv7jop6ns1r.cn 卡西欧属于什么档次hcv9jop2ns3r.cn 灯火葳蕤是什么意思hcv7jop9ns3r.cn 一月十五号是什么星座hcv7jop7ns4r.cn
胆囊炎看什么科室hcv7jop5ns5r.cn 辅酶q10有什么作用hcv9jop0ns2r.cn 酸碱度偏低是什么原因hcv9jop5ns1r.cn 木全读什么hcv7jop6ns2r.cn 木代表什么生肖hcv9jop2ns4r.cn
百度