From ee4a1a9457226caa235d6f5a2c9cce81fce2cc1e Mon Sep 17 00:00:00 2001
From: Tsubasa SEKIGUCHI <oss@tinykitten.me>
Date: Wed, 12 Mar 2025 08:54:17 +0900
Subject: [PATCH 1/3] =?UTF-8?q?reflection=E5=AF=BE=E5=BF=9C?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 Cargo.lock                                         | 14 ++++++++++++++
 stationapi/Cargo.toml                              |  1 +
 stationapi/build.rs                                |  7 +++++++
 stationapi/src/domain/entity/station.rs            |  2 +-
 .../src/infrastructure/station_repository.rs       |  2 +-
 stationapi/src/lib.rs                              |  5 ++++-
 stationapi/src/main.rs                             |  9 ++++++++-
 stationapi/src/presentation/controller/grpc.rs     |  2 +-
 stationapi/src/proto/mod.rs                        |  1 +
 stationapi/src/use_case/dto/company.rs             |  2 +-
 stationapi/src/use_case/dto/line.rs                |  2 +-
 stationapi/src/use_case/dto/line_symbol.rs         |  2 +-
 stationapi/src/use_case/dto/station.rs             |  2 +-
 stationapi/src/use_case/dto/station_number.rs      |  2 +-
 stationapi/src/use_case/dto/train_type.rs          |  2 +-
 stationapi/src/use_case/interactor/query.rs        |  4 ++--
 stationapi/src/use_case/traits/query.rs            |  2 +-
 17 files changed, 47 insertions(+), 14 deletions(-)
 create mode 100644 stationapi/src/proto/mod.rs

diff --git a/Cargo.lock b/Cargo.lock
index 3d24109e..99cba79a 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1838,6 +1838,7 @@ dependencies = [
  "tonic",
  "tonic-build",
  "tonic-health",
+ "tonic-reflection",
  "tonic-web",
  "tracing",
  "tracing-subscriber",
@@ -2059,6 +2060,19 @@ dependencies = [
  "tonic",
 ]
 
+[[package]]
+name = "tonic-reflection"
+version = "0.12.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "878d81f52e7fcfd80026b7fdb6a9b578b3c3653ba987f87f0dce4b64043cba27"
+dependencies = [
+ "prost",
+ "prost-types",
+ "tokio",
+ "tokio-stream",
+ "tonic",
+]
+
 [[package]]
 name = "tonic-web"
 version = "0.12.3"
diff --git a/stationapi/Cargo.toml b/stationapi/Cargo.toml
index 85c07239..ce5cf5f3 100644
--- a/stationapi/Cargo.toml
+++ b/stationapi/Cargo.toml
@@ -24,6 +24,7 @@ serde = { version = "1.0.189", features = ["derive"] }
 serde_json = "1.0.107"
 tonic-health = "0.12.3"
 petgraph = "0.7.1"
+tonic-reflection = "0.12.3"
 
 [build-dependencies]
 tonic-build = "0.12.3"
diff --git a/stationapi/build.rs b/stationapi/build.rs
index c876853a..d53631d8 100644
--- a/stationapi/build.rs
+++ b/stationapi/build.rs
@@ -1,5 +1,11 @@
+use std::{env, path::PathBuf};
+
 fn main() -> Result<(), Box<dyn std::error::Error>> {
+    let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
+
     tonic_build::configure()
+        .build_client(false)
+        .build_server(true)
         .type_attribute("Company", "#[derive(serde::Serialize, serde::Deserialize)]")
         .type_attribute(
             "LineSymbol",
@@ -20,6 +26,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
             "#[derive(serde::Serialize, serde::Deserialize)]",
         )
         .protoc_arg("--experimental_allow_proto3_optional")
+        .file_descriptor_set_path(out_dir.join("stationapi_descriptor.bin"))
         .compile_protos(&["proto/stationapi.proto"], &["proto"])?;
     Ok(())
 }
diff --git a/stationapi/src/domain/entity/station.rs b/stationapi/src/domain/entity/station.rs
index a76b3729..7473e89d 100644
--- a/stationapi/src/domain/entity/station.rs
+++ b/stationapi/src/domain/entity/station.rs
@@ -1,6 +1,6 @@
 use serde::{Deserialize, Serialize};
 
-use crate::station_api::StopCondition;
+use crate::proto::StopCondition;
 
 use super::{line::Line, station_number::StationNumber, train_type::TrainType as TrainTypeEntity};
 
diff --git a/stationapi/src/infrastructure/station_repository.rs b/stationapi/src/infrastructure/station_repository.rs
index 1f0cafe6..16bb65ba 100644
--- a/stationapi/src/infrastructure/station_repository.rs
+++ b/stationapi/src/infrastructure/station_repository.rs
@@ -8,7 +8,7 @@ use crate::{
         error::DomainError,
         repository::station_repository::StationRepository,
     },
-    station_api::StopCondition,
+    proto::StopCondition,
 };
 
 #[derive(sqlx::FromRow)]
diff --git a/stationapi/src/lib.rs b/stationapi/src/lib.rs
index d3b25cea..6b78f653 100644
--- a/stationapi/src/lib.rs
+++ b/stationapi/src/lib.rs
@@ -3,6 +3,9 @@ pub mod infrastructure;
 pub mod presentation;
 pub mod use_case;
 
-pub mod station_api {
+pub mod proto {
     tonic::include_proto!("app.trainlcd.grpc");
+
+    pub const FILE_DESCRIPTOR_SET: &[u8] =
+        tonic::include_file_descriptor_set!("stationapi_descriptor");
 }
diff --git a/stationapi/src/main.rs b/stationapi/src/main.rs
index 9ba01089..b94714e8 100644
--- a/stationapi/src/main.rs
+++ b/stationapi/src/main.rs
@@ -6,7 +6,7 @@ use stationapi::{
         train_type_repository::MyTrainTypeRepository,
     },
     presentation::controller::grpc::MyApi,
-    station_api::station_api_server::StationApiServer,
+    proto::{self, station_api_server::StationApiServer},
     use_case::interactor::query::QueryInteractor,
 };
 use std::sync::Arc;
@@ -80,12 +80,18 @@ async fn run() -> std::result::Result<(), anyhow::Error> {
         .accept_compressed(CompressionEncoding::Zstd)
         .send_compressed(CompressionEncoding::Zstd);
 
+    let reflection_svc = tonic_reflection::server::Builder::configure()
+        .register_encoded_file_descriptor_set(proto::FILE_DESCRIPTOR_SET)
+        .build_v1()
+        .unwrap();
+
     info!("StationAPI Server listening on {}", addr);
 
     if disable_grpc_web {
         Server::builder()
             .add_service(health_service)
             .add_service(svc)
+            .add_service(reflection_svc)
             .serve(addr)
             .await?;
     } else {
@@ -93,6 +99,7 @@ async fn run() -> std::result::Result<(), anyhow::Error> {
             .accept_http1(true)
             .add_service(tonic_web::enable(health_service))
             .add_service(tonic_web::enable(svc))
+            .add_service(tonic_web::enable(reflection_svc))
             .serve(addr)
             .await?;
     }
diff --git a/stationapi/src/presentation/controller/grpc.rs b/stationapi/src/presentation/controller/grpc.rs
index 230c717e..8bf544bd 100644
--- a/stationapi/src/presentation/controller/grpc.rs
+++ b/stationapi/src/presentation/controller/grpc.rs
@@ -5,7 +5,7 @@ use crate::{
         train_type_repository::MyTrainTypeRepository,
     },
     presentation::error::PresentationalError,
-    station_api::{
+    proto::{
         station_api_server::StationApi, CoordinatesRequest, DistanceResponse,
         DistanceResponseState, GetConnectedStationsRequest, GetLineByIdRequest,
         GetLinesByNameRequest, GetRouteRequest, GetStationByCoordinatesRequest,
diff --git a/stationapi/src/proto/mod.rs b/stationapi/src/proto/mod.rs
new file mode 100644
index 00000000..c74d956e
--- /dev/null
+++ b/stationapi/src/proto/mod.rs
@@ -0,0 +1 @@
+pub const FILE_DESCRIPTOR_SET: &[u8] = include_bytes!("station_api.bin");
diff --git a/stationapi/src/use_case/dto/company.rs b/stationapi/src/use_case/dto/company.rs
index 460eb882..b5be6751 100644
--- a/stationapi/src/use_case/dto/company.rs
+++ b/stationapi/src/use_case/dto/company.rs
@@ -1,4 +1,4 @@
-use crate::{domain::entity::company::Company, station_api::Company as GrpcCompany};
+use crate::{domain::entity::company::Company, proto::Company as GrpcCompany};
 
 impl From<Company> for GrpcCompany {
     fn from(company: Company) -> Self {
diff --git a/stationapi/src/use_case/dto/line.rs b/stationapi/src/use_case/dto/line.rs
index 1d7c4d93..73f443af 100644
--- a/stationapi/src/use_case/dto/line.rs
+++ b/stationapi/src/use_case/dto/line.rs
@@ -1,4 +1,4 @@
-use crate::{domain::entity::line::Line, station_api::Line as GrpcLine};
+use crate::{domain::entity::line::Line, proto::Line as GrpcLine};
 
 impl From<Line> for GrpcLine {
     fn from(line: Line) -> Self {
diff --git a/stationapi/src/use_case/dto/line_symbol.rs b/stationapi/src/use_case/dto/line_symbol.rs
index 23113235..9c0365fd 100644
--- a/stationapi/src/use_case/dto/line_symbol.rs
+++ b/stationapi/src/use_case/dto/line_symbol.rs
@@ -1,4 +1,4 @@
-use crate::{domain::entity::line_symbol::LineSymbol, station_api::LineSymbol as GrpcLineSymbol};
+use crate::{domain::entity::line_symbol::LineSymbol, proto::LineSymbol as GrpcLineSymbol};
 
 impl From<LineSymbol> for GrpcLineSymbol {
     fn from(symbol: LineSymbol) -> Self {
diff --git a/stationapi/src/use_case/dto/station.rs b/stationapi/src/use_case/dto/station.rs
index 51f134de..fb0e6f8d 100644
--- a/stationapi/src/use_case/dto/station.rs
+++ b/stationapi/src/use_case/dto/station.rs
@@ -1,4 +1,4 @@
-use crate::{domain::entity::station::Station, station_api::Station as GrpcStation};
+use crate::{domain::entity::station::Station, proto::Station as GrpcStation};
 
 impl From<Station> for GrpcStation {
     fn from(station: Station) -> Self {
diff --git a/stationapi/src/use_case/dto/station_number.rs b/stationapi/src/use_case/dto/station_number.rs
index 91b88022..8173e394 100644
--- a/stationapi/src/use_case/dto/station_number.rs
+++ b/stationapi/src/use_case/dto/station_number.rs
@@ -1,5 +1,5 @@
 use crate::{
-    domain::entity::station_number::StationNumber, station_api::StationNumber as GrpcStationNumber,
+    domain::entity::station_number::StationNumber, proto::StationNumber as GrpcStationNumber,
 };
 
 impl From<StationNumber> for GrpcStationNumber {
diff --git a/stationapi/src/use_case/dto/train_type.rs b/stationapi/src/use_case/dto/train_type.rs
index 1b9496af..4859a94c 100644
--- a/stationapi/src/use_case/dto/train_type.rs
+++ b/stationapi/src/use_case/dto/train_type.rs
@@ -1,4 +1,4 @@
-use crate::{domain::entity::train_type::TrainType, station_api::TrainType as GrpcTrainType};
+use crate::{domain::entity::train_type::TrainType, proto::TrainType as GrpcTrainType};
 
 impl From<TrainType> for GrpcTrainType {
     fn from(train_type: TrainType) -> Self {
diff --git a/stationapi/src/use_case/interactor/query.rs b/stationapi/src/use_case/interactor/query.rs
index 6b49478a..5575dec9 100644
--- a/stationapi/src/use_case/interactor/query.rs
+++ b/stationapi/src/use_case/interactor/query.rs
@@ -15,7 +15,7 @@ use crate::{
             train_type_repository::TrainTypeRepository,
         },
     },
-    station_api::{self, Route},
+    proto::{self, Route},
     use_case::{error::UseCaseError, traits::query::QueryUseCase},
 };
 use async_trait::async_trait;
@@ -827,7 +827,7 @@ where
 
                         stop.into()
                     })
-                    .collect::<Vec<station_api::Station>>();
+                    .collect::<Vec<proto::Station>>();
 
                 // TODO: SQLで同等の処理を行う
                 let includes_requested_station = stops
diff --git a/stationapi/src/use_case/traits/query.rs b/stationapi/src/use_case/traits/query.rs
index 660c4ea8..56b76896 100644
--- a/stationapi/src/use_case/traits/query.rs
+++ b/stationapi/src/use_case/traits/query.rs
@@ -8,7 +8,7 @@ use crate::{
         company::Company, line::Line, line_symbol::LineSymbol, misc::StationIdWithDistance,
         station::Station, station_number::StationNumber, train_type::TrainType,
     },
-    station_api::Route,
+    proto::Route,
     use_case::error::UseCaseError,
 };
 

From 63ccfad7a146d75bb0f25dd355042350f01e8001 Mon Sep 17 00:00:00 2001
From: Tsubasa SEKIGUCHI <oss@tinykitten.me>
Date: Wed, 12 Mar 2025 08:56:28 +0900
Subject: [PATCH 2/3] =?UTF-8?q?=E5=A4=9A=E5=88=86=E3=81=84=E3=82=89?=
 =?UTF-8?q?=E3=81=AA=E3=81=84=E3=83=95=E3=82=A1=E3=82=A4=E3=83=AB=E3=81=AE?=
 =?UTF-8?q?=E5=89=8A=E9=99=A4?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 stationapi/src/proto/mod.rs | 1 -
 1 file changed, 1 deletion(-)
 delete mode 100644 stationapi/src/proto/mod.rs

diff --git a/stationapi/src/proto/mod.rs b/stationapi/src/proto/mod.rs
deleted file mode 100644
index c74d956e..00000000
--- a/stationapi/src/proto/mod.rs
+++ /dev/null
@@ -1 +0,0 @@
-pub const FILE_DESCRIPTOR_SET: &[u8] = include_bytes!("station_api.bin");

From 964f5c54cef850a3924e4dec170867594bb0ac55 Mon Sep 17 00:00:00 2001
From: Tsubasa SEKIGUCHI <oss@tinykitten.me>
Date: Wed, 12 Mar 2025 08:58:48 +0900
Subject: [PATCH 3/3] import fix

---
 stationapi/src/domain/entity/station.rs | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/stationapi/src/domain/entity/station.rs b/stationapi/src/domain/entity/station.rs
index 7473e89d..6ed22b4e 100644
--- a/stationapi/src/domain/entity/station.rs
+++ b/stationapi/src/domain/entity/station.rs
@@ -204,7 +204,7 @@ impl Station {
 mod tests {
     use super::Station;
     use crate::domain::entity::{line::Line, station_number::StationNumber};
-    use crate::station_api::StopCondition;
+    use crate::proto::StopCondition;
 
     #[test]
     fn new() {
