Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add custom reflection server for easy debug #9

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 108 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ tower = "0.4.13"
hyper-util = {version = "0.1.3",features = ["tokio"]}
http-body-util = "0.1.0"
anyhow = "1.0.79"
tokio-stream = "0.1.14"
protox-parse = "0.6.0"

[build-dependencies]
tonic-build = "0.10.2"
7 changes: 0 additions & 7 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,4 @@ fn main() {
let mut news = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
news.push("news.proto");
tonic_build::compile_protos(news).expect("Failed to compile protos");

let out_dir = PathBuf::from(std::env::var("OUT_DIR").unwrap());

tonic_build::configure()
.file_descriptor_set_path(out_dir.join("news_descriptor.bin"))
.compile(&["news.proto"], &["proto"])
.unwrap();
}
4 changes: 1 addition & 3 deletions renovate.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
{
"$schema": "https://docs.renovatebot.com/renovate-schema.json",
"extends": [
"config:recommended"
]
"extends": ["config:recommended"]
}
27 changes: 19 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
use tonic::{transport::Server as TonicServer, Response, Status};
mod server;

use crate::news::news_service_server::NewsServiceServer;
use crate::server::Builder;
use anyhow::Result;
use news::news_service_server::NewsService;
use news::{MultipleNewsId, News, NewsId, NewsList};
use prost_types::FileDescriptorSet;
use std::fs::File;
use std::io::Read;
use std::sync::{Arc, Mutex};
use tonic::{transport::Server as TonicServer, Response, Status};
use tower::make::Shared;

pub mod news {
tonic::include_proto!("news"); // The package name specified in your .proto
pub(crate) const FILE_DESCRIPTOR_SET: &[u8] =
tonic::include_file_descriptor_set!("news_descriptor");
}

#[derive(Debug, Default)]
Expand Down Expand Up @@ -152,16 +155,24 @@ async fn main() -> Result<()> {
let addr = ([127, 0, 0, 1], 50051).into();

let news_service = MyNewsService::new();
let service = tonic_reflection::server::Builder::configure()
.register_encoded_file_descriptor_set(news::FILE_DESCRIPTOR_SET)
.build()
.unwrap();

let mut file = File::open("news.proto")?;
let mut content = String::new();
file.read_to_string(&mut content)?;

let news = protox_parse::parse("news.proto", &content)?;
let mut news_descriptor_set = FileDescriptorSet::default();
news_descriptor_set.file.push(news);

let service = Builder::configure()
.register_file_descriptor_set(news_descriptor_set)
.build()?;

println!("NewsService server listening on {}", addr);

let tonic_service = TonicServer::builder()
.add_service(NewsServiceServer::new(news_service))
.add_service(service)
.add_service(NewsServiceServer::new(news_service))
.into_service();
let make_svc = Shared::new(tonic_service);
println!("Server listening on http://{}", addr);
Expand Down
Loading