get adresar method is now working
This commit is contained in:
14
Cargo.lock
generated
14
Cargo.lock
generated
@@ -1377,6 +1377,7 @@ dependencies = [
|
||||
"toml",
|
||||
"tonic",
|
||||
"tonic-build",
|
||||
"tonic-reflection",
|
||||
"tracing",
|
||||
"tracing-subscriber",
|
||||
]
|
||||
@@ -2665,6 +2666,19 @@ dependencies = [
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[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 = "tower"
|
||||
version = "0.4.13"
|
||||
|
||||
@@ -19,6 +19,7 @@ tokio = { version = "1.43.0", features = ["full", "macros"] }
|
||||
toml = "0.8.20"
|
||||
tonic = "0.12.3"
|
||||
tonic-build = "0.12.3"
|
||||
tonic-reflection = "0.12.3"
|
||||
tracing = "0.1.41"
|
||||
tracing-subscriber = "0.3.19"
|
||||
|
||||
|
||||
8
build.rs
8
build.rs
@@ -1,4 +1,8 @@
|
||||
// build.rs
|
||||
fn main() {
|
||||
tonic_build::compile_protos("proto/api.proto").unwrap();
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
tonic_build::configure()
|
||||
.build_server(true)
|
||||
.file_descriptor_set_path("src/proto/descriptor.bin") // Generate the file descriptor set
|
||||
.compile_protos(&["proto/api.proto"], &["proto"])?; // Use compile_protos() instead of compile()
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -9,6 +9,11 @@ service DataProcessor {
|
||||
|
||||
service Adresar {
|
||||
rpc CreateAdresar (AdresarRequest) returns (AdresarResponse);
|
||||
rpc GetAdresar (GetAdresarRequest) returns (AdresarResponse);
|
||||
}
|
||||
|
||||
message GetAdresarRequest {
|
||||
int64 id = 1; // The ID of the Adresar entry to retrieve
|
||||
}
|
||||
|
||||
message AdresarRequest {
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
// src/adresar/handlers.rs
|
||||
|
||||
pub mod create_adresar;
|
||||
pub mod get_adresar;
|
||||
|
||||
pub use create_adresar::create_adresar;
|
||||
pub use get_adresar::get_adresar;
|
||||
|
||||
42
src/adresar/handlers/get_adresar.rs
Normal file
42
src/adresar/handlers/get_adresar.rs
Normal file
@@ -0,0 +1,42 @@
|
||||
// src/adresar/handlers/get_adresar.rs
|
||||
use tonic::{Request, Response, Status};
|
||||
use sqlx::PgPool;
|
||||
use crate::adresar::models::Adresar;
|
||||
use crate::proto::multieko2::{GetAdresarRequest, AdresarResponse};
|
||||
|
||||
pub async fn get_adresar(
|
||||
db_pool: &PgPool,
|
||||
request: GetAdresarRequest,
|
||||
) -> Result<AdresarResponse, Status> {
|
||||
let adresar = sqlx::query_as!(
|
||||
Adresar,
|
||||
r#"
|
||||
SELECT id, firma, kz, drc, ulica, psc, mesto, stat, banka, ucet, skladm, ico, kontakt, telefon, skladu, fax
|
||||
FROM adresar
|
||||
WHERE id = $1
|
||||
"#,
|
||||
request.id
|
||||
)
|
||||
.fetch_one(db_pool)
|
||||
.await
|
||||
.map_err(|e| Status::not_found(e.to_string()))?;
|
||||
|
||||
Ok(AdresarResponse {
|
||||
id: adresar.id,
|
||||
firma: adresar.firma,
|
||||
kz: adresar.kz.unwrap_or_default(),
|
||||
drc: adresar.drc.unwrap_or_default(),
|
||||
ulica: adresar.ulica.unwrap_or_default(),
|
||||
psc: adresar.psc.unwrap_or_default(),
|
||||
mesto: adresar.mesto.unwrap_or_default(),
|
||||
stat: adresar.stat.unwrap_or_default(),
|
||||
banka: adresar.banka.unwrap_or_default(),
|
||||
ucet: adresar.ucet.unwrap_or_default(),
|
||||
skladm: adresar.skladm.unwrap_or_default(),
|
||||
ico: adresar.ico.unwrap_or_default(),
|
||||
kontakt: adresar.kontakt.unwrap_or_default(),
|
||||
telefon: adresar.telefon.unwrap_or_default(),
|
||||
skladu: adresar.skladu.unwrap_or_default(),
|
||||
fax: adresar.fax.unwrap_or_default(),
|
||||
})
|
||||
}
|
||||
BIN
src/proto/descriptor.bin
Normal file
BIN
src/proto/descriptor.bin
Normal file
Binary file not shown.
@@ -1,3 +1,7 @@
|
||||
// src/proto/mod.rs
|
||||
pub mod multieko2 {
|
||||
tonic::include_proto!("multieko2");
|
||||
|
||||
// Include the file descriptor set
|
||||
pub const FILE_DESCRIPTOR_SET: &[u8] = include_bytes!("descriptor.bin");
|
||||
}
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
// src/server/mod.rs
|
||||
use tonic::{Request, Response, Status};
|
||||
use tonic_reflection::server::Builder as ReflectionBuilder; // Import tonic-reflection
|
||||
use crate::db;
|
||||
use crate::adresar::handlers::create_adresar;
|
||||
use crate::adresar::handlers::{create_adresar, get_adresar};
|
||||
use crate::proto::multieko2::{
|
||||
AdresarRequest, AdresarResponse,
|
||||
AdresarRequest, AdresarResponse, GetAdresarRequest,
|
||||
adresar_server::{Adresar, AdresarServer},
|
||||
FILE_DESCRIPTOR_SET, // Import the generated file descriptor set
|
||||
};
|
||||
|
||||
pub struct AdresarService {
|
||||
@@ -20,6 +22,14 @@ impl Adresar for AdresarService {
|
||||
let response = create_adresar(&self.db_pool, request.into_inner()).await?;
|
||||
Ok(Response::new(response))
|
||||
}
|
||||
|
||||
async fn get_adresar(
|
||||
&self,
|
||||
request: Request<GetAdresarRequest>,
|
||||
) -> Result<Response<AdresarResponse>, Status> {
|
||||
let response = get_adresar(&self.db_pool, request.into_inner()).await?;
|
||||
Ok(Response::new(response))
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn run_server(db_pool: sqlx::PgPool) -> Result<(), Box<dyn std::error::Error>> {
|
||||
@@ -28,8 +38,15 @@ pub async fn run_server(db_pool: sqlx::PgPool) -> Result<(), Box<dyn std::error:
|
||||
|
||||
println!("Server listening on {}", addr);
|
||||
|
||||
// Enable reflection
|
||||
let reflection_service = ReflectionBuilder::configure()
|
||||
.register_encoded_file_descriptor_set(FILE_DESCRIPTOR_SET) // Register the file descriptor set
|
||||
.build()
|
||||
.unwrap();
|
||||
|
||||
tonic::transport::Server::builder()
|
||||
.add_service(AdresarServer::new(adresar_service))
|
||||
.add_service(reflection_service) // Add the reflection service
|
||||
.serve(addr)
|
||||
.await?;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user