Skip to content

Commit

Permalink
add swagger endpoint to stats
Browse files Browse the repository at this point in the history
  • Loading branch information
bragov4ik committed Aug 13, 2024
1 parent 6a6bcc3 commit 36b09ae
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 8 deletions.
8 changes: 8 additions & 0 deletions stats/Cargo.lock

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

1 change: 1 addition & 0 deletions stats/stats-proto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ serde_json = "1.0"
actix-prost-build = { git = "https://github.com/blockscout/actix-prost" }
tonic-build = "0.8"
prost-build = "0.11"
blockscout-swagger-endpoint = { path = "../../libs/blockscout-endpoints/swagger" }
40 changes: 32 additions & 8 deletions stats/stats-proto/build.rs
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(
Expand All @@ -16,32 +16,56 @@ 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)]"
)
.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)]");
blockscout_swagger_endpoint::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/blockscout-endpoints/swagger");

// 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_swagger_endpoint::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", "proto/health.proto"]
.map(PathBuf::from)
.to_vec();
protos.extend(blockscout_swagger_endpoint::proto_files(
swagger_crate_folder,
));
let protos = vec_path_buf_to_string(protos);

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(())
}
3 changes: 3 additions & 0 deletions stats/stats-proto/proto/api_config_http.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ http:

- selector: blockscout.stats.v1.Health.Check
get: /health

- selector: blockscout.swagger.v1.Swagger.GetSwagger
get: /swagger
6 changes: 6 additions & 0 deletions stats/stats-proto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ pub mod blockscout {
include!(concat!(env!("OUT_DIR"), "/blockscout.stats.v1.rs"));
}
}

pub mod swagger {
pub mod v1 {
include!(concat!(env!("OUT_DIR"), "/blockscout.swagger.v1.rs"));
}
}
}

#[cfg(test)]
Expand Down
20 changes: 20 additions & 0 deletions stats/stats-proto/swagger/stats.swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ info:
tags:
- name: StatsService
- name: Health
- name: Swagger
consumes:
- application/json
produces:
Expand Down Expand Up @@ -89,6 +90,20 @@ paths:
type: string
tags:
- Health
/swagger:
get:
operationId: Swagger_GetSwagger
responses:
"200":
description: A successful response.
schema:
$ref: '#/definitions/v1GetSwaggerResponse'
default:
description: An unexpected error response.
schema:
$ref: '#/definitions/rpcStatus'
tags:
- Swagger
definitions:
HealthCheckResponseServingStatus:
type: string
Expand Down Expand Up @@ -139,6 +154,11 @@ definitions:
items:
type: object
$ref: '#/definitions/v1Counter'
v1GetSwaggerResponse:
type: object
properties:
swagger:
type: string
v1HealthCheckResponse:
type: object
properties:
Expand Down

0 comments on commit 36b09ae

Please sign in to comment.