Skip to content
This repository has been archived by the owner on May 18, 2023. It is now read-only.

Commit

Permalink
Feat: Add status route
Browse files Browse the repository at this point in the history
  • Loading branch information
guscalonico committed Aug 16, 2022
1 parent 85e6ad0 commit a02d451
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ where
.app_data(Data::new(runtime_context.clone()))
.wrap(Logger::default())
.route("/", web::get().to(index::<Context, KeyManager>))
.route("/status", web::get().to(index::<Context, KeyManager>))
.route("/tx/{tx_id}", web::get().to(get_tx::<Context>))
.service(
web::scope("/cosigner")
Expand Down
1 change: 1 addition & 0 deletions src/server/routes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ pub mod get_tx;
pub mod index;
pub mod sign;
pub mod test;
pub mod status;
44 changes: 44 additions & 0 deletions src/server/routes/status.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use actix_web::{
web::Data,
HttpResponse,
};
use diesel::{QueryDsl, RunQueryDsl};
use diesel::dsl::count;
use serde::Serialize;

use crate::database::schema::transactions::{self, id};
use crate::server::RuntimeContext;
use crate::{server::error::ValidatorServerError, key_manager};
use crate::server::routes::sign::Config;

#[derive(Serialize)]
struct StatusBody {
total_txs: i64,
epoch: u128,
next_epoch: u128,
previous_epoch: u128
}

pub async fn index<Context, KeyManager>(
ctx: Data<Context>,
) -> actix_web::Result<HttpResponse, ValidatorServerError>
where
Context: self::Config<KeyManager> + RuntimeContext,
KeyManager: key_manager::KeyManager,
{
let conn = ctx.get_db_connection();
let total_txs = transactions::table
.select(count(id))
.first(&conn)
.unwrap_or(0 as i64);
let current_epoch = ctx.current_epoch();

let body = StatusBody {
total_txs,
epoch: current_epoch,
next_epoch: current_epoch + 1,
previous_epoch: current_epoch - 1
};

Ok(HttpResponse::Ok().json(body))
}

0 comments on commit a02d451

Please sign in to comment.