initial project structure

This commit is contained in:
filipriec
2025-02-13 11:58:32 +01:00
parent d881fbf9af
commit 9233c769b0
9 changed files with 1834 additions and 187 deletions

52
src/server/mod.rs Normal file
View File

@@ -0,0 +1,52 @@
use tonic::{Request, Response, Status};
use sqlx::PgPool;
use multieko2::accounting_server::{Accounting, AccountingServer};
use multieko2::{CreateAccountRequest, CreateAccountResponse, GetAccountRequest, GetAccountResponse};
pub struct MyAccountingService {
db_pool: PgPool,
}
#[tonic::async_trait]
impl Accounting for MyAccountingService {
async fn create_account(
&self,
request: Request<CreateAccountRequest>,
) -> Result<Response<CreateAccountResponse>, Status> {
let name = request.into_inner().name;
let account = sqlx::query!("INSERT INTO accounts (name) VALUES ($1) RETURNING id", name)
.fetch_one(&self.db_pool)
.await
.map_err(|e| Status::internal(e.to_string()))?;
Ok(Response::new(CreateAccountResponse { id: account.id.to_string() }))
}
async fn get_account(
&self,
request: Request<GetAccountRequest>,
) -> Result<Response<GetAccountResponse>, Status> {
let id = request.into_inner().id;
let account = sqlx::query!("SELECT id, name FROM accounts WHERE id = $1", id)
.fetch_one(&self.db_pool)
.await
.map_err(|e| Status::internal(e.to_string()))?;
Ok(Response::new(GetAccountResponse {
id: account.id.to_string(),
name: account.name,
}))
}
}
pub async fn run_server(db_pool: PgPool) -> Result<(), Box<dyn std::error::Error>> {
let addr = "[::1]:50051".parse()?;
let service = MyAccountingService { db_pool };
tonic::transport::Server::builder()
.add_service(AccountingServer::new(service))
.serve(addr)
.await?;
Ok(())
}