perfectly working system
This commit is contained in:
@@ -1,27 +1,51 @@
|
||||
// src/server/mod.rs
|
||||
use tonic::{Request, Response, Status};
|
||||
use crate::{db, proto::api::*};
|
||||
use crate::db;
|
||||
use crate::proto::multieko2::{
|
||||
data_processor_server::{DataProcessor, DataProcessorServer},
|
||||
DataRequest, DataResponse
|
||||
};
|
||||
|
||||
pub struct AccountingService {
|
||||
pub struct DataProcessorService {
|
||||
db_pool: sqlx::PgPool,
|
||||
}
|
||||
|
||||
#[tonic::async_trait]
|
||||
impl accounting_server::Accounting for AccountingService {
|
||||
async fn create_account(
|
||||
impl DataProcessor for DataProcessorService {
|
||||
async fn process_data(
|
||||
&self,
|
||||
request: Request<CreateAccountRequest>,
|
||||
) -> Result<Response<CreateAccountResponse>, Status> {
|
||||
// Database implementation
|
||||
request: Request<DataRequest>,
|
||||
) -> Result<Response<DataResponse>, Status> {
|
||||
let data = request.into_inner().data;
|
||||
|
||||
// Store data in database
|
||||
let stored_data = sqlx::query!(
|
||||
"INSERT INTO processed_data (content) VALUES ($1) RETURNING id",
|
||||
data
|
||||
)
|
||||
.fetch_one(&self.db_pool)
|
||||
.await
|
||||
.map_err(|e| Status::internal(e.to_string()))?;
|
||||
|
||||
// Simple processing: convert to uppercase
|
||||
let processed_data = data.to_uppercase();
|
||||
|
||||
Ok(Response::new(DataResponse {
|
||||
processed_data: format!("Processed data with ID: {}", stored_data.id)
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn run_server(pool: sqlx::PgPool) -> Result<(), Box<dyn std::error::Error>> {
|
||||
pub async fn run_server(db_pool: sqlx::PgPool) -> Result<(), Box<dyn std::error::Error>> {
|
||||
let addr = "[::1]:50051".parse()?;
|
||||
let service = DataProcessorService { db_pool };
|
||||
|
||||
println!("Server listening on {}", addr);
|
||||
|
||||
tonic::transport::Server::builder()
|
||||
.add_service(accounting_server::AccountingServer::new(
|
||||
AccountingService { db_pool: pool }
|
||||
))
|
||||
.add_service(DataProcessorServer::new(service))
|
||||
.serve(addr)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user