diff --git a/migrations/20250215223128_create_processed_data_table.sql b/migrations/20250215223128_create_processed_data_table.sql new file mode 100644 index 0000000..cc5715f --- /dev/null +++ b/migrations/20250215223128_create_processed_data_table.sql @@ -0,0 +1,6 @@ +-- Add migration script here +CREATE TABLE processed_data ( + id SERIAL PRIMARY KEY, + content TEXT NOT NULL, + created_at TIMESTAMP DEFAULT NOW() +); diff --git a/proto/api.proto b/proto/api.proto index efb516a..16c7a45 100644 --- a/proto/api.proto +++ b/proto/api.proto @@ -1,15 +1,16 @@ +// proto/api.proto syntax = "proto3"; -package my_service; +package multieko2; -service MyService { - rpc MyMethod (MyRequest) returns (MyResponse); +service DataProcessor { + rpc ProcessData (DataRequest) returns (DataResponse); } -message MyRequest { - int32 id = 1; +message DataRequest { + string data = 1; } -message MyResponse { - string message = 1; +message DataResponse { + string processed_data = 1; } diff --git a/src/client/mod.rs b/src/client/mod.rs index 9699c22..dd1796c 100644 --- a/src/client/mod.rs +++ b/src/client/mod.rs @@ -1,7 +1,18 @@ -use crate::proto::api::accounting_client::AccountingClient; +// src/client/mod.rs +use tonic::transport::Channel; +use crate::proto::multieko2::data_processor_client::DataProcessorClient; +use crate::proto::multieko2::DataRequest; pub async fn run_client() -> Result<(), Box> { - let mut client = AccountingClient::connect("http://[::1]:50051").await?; - // Client implementation + let mut client = DataProcessorClient::connect("http://[::1]:50051").await?; + + let request = tonic::Request::new(DataRequest { + data: "hello world".to_string(), + }); + + let response = client.process_data(request).await?; + + println!("RESPONSE={:?}", response.into_inner().processed_data); + Ok(()) } diff --git a/src/lib.rs b/src/lib.rs index e878331..a417c12 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,5 @@ // src/lib.rs pub mod db; +pub mod client; pub mod server; +pub mod proto; diff --git a/src/main.rs b/src/main.rs index 6a2324b..91d5bfd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,33 +1,21 @@ // src/main.rs -use tonic::transport::Server; -use sqlx::PgPool; -use crate::db; -use crate::server; +use std::env; +use dotenvy::dotenv; +use multieko2::{server, client, db}; #[tokio::main] async fn main() -> Result<(), Box> { - // Load .env file - dotenvy::dotenv().ok(); + dotenv().ok(); // Initialize database let db_config = db::DatabaseConfig::from_env(); - let db_pool = db::create_pool(&db_config) - .await - .expect("Failed to create database pool"); + let db_pool = db::create_pool(&db_config).await?; - - // Check database connection - db::check_connection(&db_pool) - .await - .expect("Failed to connect to database"); - - // Choose mode based on command line arguments - let args: Vec = env::args().collect(); - match args.get(1).map(|s| s.as_str()) { + match env::args().nth(1).as_deref() { Some("server") => server::run_server(db_pool).await?, Some("client") => client::run_client().await?, - Some("standalone") => standalone::run().await?, - _ => println!("Usage: [server|client|standalone]"), + _ => println!("Usage: cargo run -- [server|client]"), } + Ok(()) } diff --git a/src/proto/mod.rs b/src/proto/mod.rs new file mode 100644 index 0000000..0ca672e --- /dev/null +++ b/src/proto/mod.rs @@ -0,0 +1,3 @@ +pub mod multieko2 { + tonic::include_proto!("multieko2"); +} diff --git a/src/server/mod.rs b/src/server/mod.rs index 9039731..f499b55 100644 --- a/src/server/mod.rs +++ b/src/server/mod.rs @@ -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, - ) -> Result, Status> { - // Database implementation + request: Request, + ) -> Result, 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> { +pub async fn run_server(db_pool: sqlx::PgPool) -> Result<(), Box> { 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(()) } diff --git a/src/standalone/mod.rs b/src/standalone/mod.rs deleted file mode 100644 index 2f3319c..0000000 --- a/src/standalone/mod.rs +++ /dev/null @@ -1,21 +0,0 @@ -use crate::server::run_server; -use crate::client::run_client; -use sqlx::PgPool; - -pub async fn run() -> Result<(), Box> { - // Set up local PostgreSQL connection - let db_pool = PgPool::connect("postgres://user:password@localhost/multieko2").await?; - - // Run the server in the background - let server_handle = tokio::spawn(async move { - run_server(db_pool).await.unwrap(); - }); - - // Run the client - run_client("http://[::1]:50051".to_string()).await?; - - // Wait for the server to finish (it won't, but this keeps the app running) - server_handle.await?; - - Ok(()) -}