Skip to content

Commit

Permalink
feat: implemented server config
Browse files Browse the repository at this point in the history
  • Loading branch information
paulobressan committed Jan 23, 2025
1 parent 512cb80 commit 4c7a8d2
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 19 deletions.
3 changes: 3 additions & 0 deletions examples/config.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[storage]
db_path = "dev.db"

[server]
addr="0.0.0.0:5000"

10 changes: 3 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,17 @@ async fn main() -> Result<()> {
let _tx_storage = SqliteTransaction::new(storage.clone());

let pipeline = pipeline::run();
let server = server::run();
let server = server::run(config.server);

try_join!(pipeline, server)?;

Ok(())
}

#[derive(Deserialize)]
struct ConfigStorage {
db_path: String,
}

#[derive(Deserialize)]
struct Config {
storage: ConfigStorage,
server: server::Config,
storage: storage::Config,
}

impl Config {
Expand Down
23 changes: 11 additions & 12 deletions src/server/mod.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use anyhow::Result;
use pallas::interop::utxorpc::spec as u5c;
use std::{net::SocketAddr, str::FromStr};
use serde::Deserialize;
use std::net::SocketAddr;
use tonic::transport::Server;
use tracing::{error, info};

mod utxorpc;

pub async fn run() -> Result<()> {
tokio::spawn(async {
pub async fn run(config: Config) -> Result<()> {
tokio::spawn(async move {
let submit_service = utxorpc::SubmitServiceImpl {};
let submit_service =
u5c::submit::submit_service_server::SubmitServiceServer::new(submit_service);
Expand All @@ -20,19 +21,12 @@ pub async fn run() -> Result<()> {

let mut server = Server::builder().accept_http1(true);

let address = "0.0.0.0:5000";
info!(address, "GRPC server running");

let result = SocketAddr::from_str(address);
if let Err(error) = result {
error!(?error);
std::process::exit(1);
}
info!(address = config.addr.to_string(), "GRPC server running");

let result = server
.add_service(reflection)
.add_service(submit_service)
.serve(result.unwrap())
.serve(config.addr)
.await;

if let Err(error) = result {
Expand All @@ -43,3 +37,8 @@ pub async fn run() -> Result<()> {

Ok(())
}

#[derive(Deserialize)]
pub struct Config {
pub addr: SocketAddr,
}
6 changes: 6 additions & 0 deletions src/storage/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use chrono::{DateTime, Utc};
use serde::Deserialize;

pub mod sqlite;

Expand All @@ -18,6 +19,11 @@ pub struct TransactionStorage {
pub updated_at: DateTime<Utc>,
}

#[derive(Deserialize)]
pub struct Config {
pub db_path: String,
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down

0 comments on commit 4c7a8d2

Please sign in to comment.