initial project structure
This commit is contained in:
1881
Cargo.lock
generated
1881
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
10
Cargo.toml
10
Cargo.toml
@@ -4,7 +4,9 @@ version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
serde = { version = "1.0.217", features = ["derive"] }
|
||||
serde_json = "1.0.138"
|
||||
tokio = { version = "1.43.0", features = ["full"] }
|
||||
tokio-postgres = { version = "0.7.13", features = ["with-uuid-1"] }
|
||||
clap = { version = "4.5.29", features = ["derive"] }
|
||||
prost = "0.13.5"
|
||||
sqlx = { version = "0.8.3", features = ["postgres", "runtime-tokio"] }
|
||||
tokio = { version = "1.43.0", features = ["full", "macros"] }
|
||||
tonic = "0.12.3"
|
||||
tonic-build = "0.12.3"
|
||||
|
||||
0
proto/api.proto
Normal file
0
proto/api.proto
Normal file
21
src/client/mod.rs
Normal file
21
src/client/mod.rs
Normal file
@@ -0,0 +1,21 @@
|
||||
use multieko2::accounting_client::AccountingClient;
|
||||
use multieko2::{CreateAccountRequest, GetAccountRequest};
|
||||
|
||||
pub async fn run_client(server_url: String) -> Result<(), Box<dyn std::error::Error>> {
|
||||
let mut client = AccountingClient::connect(server_url).await?;
|
||||
|
||||
// Create an account
|
||||
let create_response = client
|
||||
.create_account(CreateAccountRequest { name: "John Doe".into() })
|
||||
.await?;
|
||||
println!("Created account with ID: {}", create_response.into_inner().id);
|
||||
|
||||
// Get an account
|
||||
let get_response = client
|
||||
.get_account(GetAccountRequest { id: "1".into() })
|
||||
.await?;
|
||||
let account = get_response.into_inner();
|
||||
println!("Account ID: {}, Name: {}", account.id, account.name);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
0
src/common/mod.rs
Normal file
0
src/common/mod.rs
Normal file
0
src/lib.rs
Normal file
0
src/lib.rs
Normal file
36
src/main.rs
36
src/main.rs
@@ -1,3 +1,35 @@
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
use clap::{Parser, Subcommand};
|
||||
use sqlx::PgPool;
|
||||
|
||||
#[derive(Parser)]
|
||||
struct Cli {
|
||||
#[clap(subcommand)]
|
||||
command: Commands,
|
||||
}
|
||||
|
||||
#[derive(Subcommand)]
|
||||
enum Commands {
|
||||
Standalone,
|
||||
Server,
|
||||
Client { server_url: String },
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let args = Cli::parse();
|
||||
|
||||
match args.command {
|
||||
Commands::Standalone => {
|
||||
standalone::run().await?;
|
||||
}
|
||||
Commands::Server => {
|
||||
let db_pool = PgPool::connect("postgres://user:password@localhost/multieko2").await?;
|
||||
server::run_server(db_pool).await?;
|
||||
}
|
||||
Commands::Client { server_url } => {
|
||||
client::run_client(server_url).await?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
52
src/server/mod.rs
Normal file
52
src/server/mod.rs
Normal 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(())
|
||||
}
|
||||
21
src/standalone/mod.rs
Normal file
21
src/standalone/mod.rs
Normal file
@@ -0,0 +1,21 @@
|
||||
use crate::server::run_server;
|
||||
use crate::client::run_client;
|
||||
use sqlx::PgPool;
|
||||
|
||||
pub async fn run() -> Result<(), Box<dyn std::error::Error>> {
|
||||
// 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(())
|
||||
}
|
||||
Reference in New Issue
Block a user