Skip to content

Commit

Permalink
Merge pull request #117 from josephchimebuka/fix--#109-
Browse files Browse the repository at this point in the history
feat: add endpoint to retrieve expiring domains
  • Loading branch information
Marchand-Nicolas authored Nov 28, 2024
2 parents 867901a + a9ee11e commit c91af5a
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
78 changes: 78 additions & 0 deletions src/endpoints/get_expiring_domains.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
use crate::{
models::AppState,
utils::get_error,
};
use axum::{
extract::State,
http::{HeaderMap, HeaderValue, StatusCode},
response::{IntoResponse, Json},
};
use axum_auto_routes::route;
use futures::StreamExt;
use mongodb::bson::{doc, Document};
use std::sync::Arc;
use serde::Serialize;

#[derive(Serialize)]
struct IdDetails {
addr: String,
domain: String,
}

#[derive(Serialize)]
pub struct ExpiringDomains {
ids: Vec<IdDetails>,
}

#[route(get, "/get_expiring_domains", crate::endpoints::get_expiring_domains)]
pub async fn handler(
State(state): State<Arc<AppState>>,
) -> impl IntoResponse {
let mut headers = HeaderMap::new();
headers.insert("Cache-Control", HeaderValue::from_static("max-age=30"));

let collection = state.starknetid_db.collection::<Document>("domains");

let current_time = chrono::Utc::now().timestamp();
let one_week_later = current_time + 604800; // Add one week in seconds

// Define the aggregation pipeline
let pipeline = vec![
doc! {
"$match": {
"expiry": {
"$lt": one_week_later,
"$gt": current_time
},
"_cursor.to": null
}
},
doc! {
"$project": {
"domain": 1,
"legacy_address": 1,
}
},
];

let mut ids: Vec<IdDetails> = Vec::new();

match collection.aggregate(pipeline, None).await {
Ok(mut cursor) => {
while let Some(doc_result) = cursor.next().await {
match doc_result {
Ok(doc) => {
if let (Ok(domain), Ok(address)) = (doc.get_str("domain"), doc.get_str("legacy_address")) {
ids.push(IdDetails { addr: address.to_string(), domain: domain.to_string() });
}
}
Err(_) => {
get_error("Error while parsing document".to_string());
}
}
}
(StatusCode::OK, Json(ExpiringDomains { ids })).into_response()
}
Err(_) => get_error("Failed to retrive data from database".to_string()),
}
}
1 change: 1 addition & 0 deletions src/endpoints/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ pub mod renewal;
pub mod starkscan;
pub mod stats;
pub mod uri;
pub mod get_expiring_domains;

0 comments on commit c91af5a

Please sign in to comment.