-
Notifications
You must be signed in to change notification settings - Fork 147
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(health): extract health into separate crate #1008
base: main
Are you sure you want to change the base?
Changes from all commits
84bd8d4
aa90219
de2e18e
eeedcbc
99662d0
c1ed4d3
eba3cda
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
[package] | ||
name = "blockscout-endpoint-health" | ||
version = "0.1.0" | ||
description = "Configuration tools for compiling health endpoint" | ||
license = "MIT" | ||
repository = "https://github.com/blockscout/blockscout-rs" | ||
keywords = ["blockscout", "service", "http", "microservices", "health", "endpoint"] | ||
categories = ["web-programming::http-server"] | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
prost-build = "0.11" |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,49 @@ | ||||||
//! Tools for setting up `/health` endpoint. | ||||||
//! | ||||||
//! ## Usage | ||||||
//! (example can be seen in `stats-proto` crate in this repo) | ||||||
//! | ||||||
//! 1. In `build.rs` in your crateuse [`add_to_compile_config`], | ||||||
//! [`proto_files`], and [`includes`] for compiling `health.proto` | ||||||
//! to rust code using [`prost_build`]. | ||||||
//! | ||||||
//! 2. To include the generated code in your project, add the following to `lib.rs` | ||||||
//! (`/grpc.health.v1.rs` is the file name chosen based on `package` in `.proto` file): | ||||||
//! ```ignore | ||||||
//! pub mod grpc { | ||||||
//! pub mod health { | ||||||
//! pub mod v1 { | ||||||
//! include!(concat!(env!("OUT_DIR"), "/grpc.health.v1.rs")); | ||||||
//! } | ||||||
//! } | ||||||
//! } | ||||||
//! ``` | ||||||
//! | ||||||
//! 3. To enable swagger generation, add the following in `api_config_http.yaml`'s | ||||||
//! http rules: | ||||||
//! ```custom,{class=language-yaml} | ||||||
//! - selector: grpc.health.v1.Health.Check | ||||||
//! get: /health | ||||||
//! ``` | ||||||
//! | ||||||
//! Now the types should be available in `grpc::health::v1` module, and swagger | ||||||
//! entry for the endpoint should appear | ||||||
|
||||||
use std::path::{Path, PathBuf}; | ||||||
|
||||||
use prost_build::Config; | ||||||
|
||||||
pub fn add_to_compile_config(config: &mut Config) { | ||||||
config.bytes([".grpc.health"]).type_attribute( | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In my opinion it is the client code responsibility to decide should the generated code use bytes or not, so I suggest to remove it from here and only leave |
||||||
".grpc.health", | ||||||
"#[actix_prost_macros::serde(rename_all=\"snake_case\")]", | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, wdyt about leaving the case definition as the client responsibility as well? |
||||||
); | ||||||
} | ||||||
|
||||||
pub fn proto_files(path_to_swagger_crate: &Path) -> Vec<PathBuf> { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
vec![path_to_swagger_crate.join("proto/health.proto")] | ||||||
} | ||||||
|
||||||
pub fn includes(path_to_swagger_crate: &Path) -> Vec<PathBuf> { | ||||||
vec![path_to_swagger_crate.join("proto")] | ||||||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
use actix_prost_build::{ActixGenerator, GeneratorList}; | ||
use prost_build::{Config, ServiceGenerator}; | ||
use std::path::Path; | ||
use std::path::{Path, PathBuf}; | ||
|
||
// custom function to include custom generator | ||
fn compile( | ||
|
@@ -16,33 +16,55 @@ fn compile( | |
.protoc_arg("--openapiv2_opt") | ||
.protoc_arg("grpc_api_configuration=proto/api_config_http.yaml,output_format=yaml,allow_merge=true,merge_file_name=stats,json_names_for_fields=false") | ||
.bytes(["."]) | ||
.type_attribute(".", "#[actix_prost_macros::serde(rename_all=\"snake_case\")]") | ||
.type_attribute(".blockscout.stats", "#[actix_prost_macros::serde(rename_all=\"snake_case\")]") | ||
.field_attribute( | ||
".blockscout.stats.v1.HealthCheckRequest.service", | ||
"#[serde(default)]" | ||
) | ||
Comment on lines
20
to
23
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lets move that definition into the health endpoint crate There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe just add There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. need to add to proto then remove |
||
.field_attribute(".blockscout.stats.v1.Point.is_approximate", "#[serde(skip_serializing_if = \"std::ops::Not::not\")]") | ||
.field_attribute(".blockscout.stats.v1.Point.is_approximate", "#[serde(default)]") | ||
.field_attribute(".blockscout.stats.v1.GetLineChartRequest.resolution", "#[serde(default)]"); | ||
blockscout_endpoint_health::add_to_compile_config(&mut config); | ||
|
||
config.compile_protos(protos, includes)?; | ||
Ok(()) | ||
} | ||
|
||
fn vec_path_buf_to_string(v: Vec<PathBuf>) -> Vec<String> { | ||
v.into_iter() | ||
.map(|path| { | ||
path.to_str() | ||
.expect("Non UTF-8 paths are not supported") | ||
.to_string() | ||
}) | ||
.collect() | ||
} | ||
|
||
fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
let swagger_crate_folder = Path::new("../../libs/endpoints/health"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. swagger or health ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In general, will this work in other than blockscou-rs repositories and will this build inside docker containers? |
||
|
||
// We need to rebuild proto lib only if any of proto definitions | ||
// (or corresponding http mapping) has been changed. | ||
println!("cargo:rerun-if-changed=proto/"); | ||
let mut proto_files_folders = ["proto/"].map(PathBuf::from).to_vec(); | ||
proto_files_folders.extend(blockscout_endpoint_health::includes(swagger_crate_folder)); | ||
let proto_files_folders = vec_path_buf_to_string(proto_files_folders); | ||
|
||
for folder in &proto_files_folders { | ||
println!("cargo:rerun-if-changed={folder}/"); | ||
} | ||
|
||
let mut protos = ["proto/stats.proto"].map(PathBuf::from).to_vec(); | ||
protos.extend(blockscout_endpoint_health::proto_files( | ||
swagger_crate_folder, | ||
)); | ||
let protos = vec_path_buf_to_string(protos); | ||
sevenzing marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
std::fs::create_dir_all("./swagger").unwrap(); | ||
let gens = Box::new(GeneratorList::new(vec![ | ||
tonic_build::configure().service_generator(), | ||
Box::new(ActixGenerator::new("proto/api_config_http.yaml").unwrap()), | ||
])); | ||
compile( | ||
&["proto/stats.proto", "proto/health.proto"], | ||
&["proto"], | ||
gens, | ||
)?; | ||
|
||
compile(&protos, &proto_files_folders, gens)?; | ||
Ok(()) | ||
} |
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe it is better to leave the
proto/health/v1/health.proto
file as it is used by some of other services