From d3ec41eac6a24e0e18d122c22f996e8603b538c5 Mon Sep 17 00:00:00 2001 From: Thomas Marchand Date: Fri, 19 Jan 2024 11:55:30 +0000 Subject: [PATCH] refactor: use axum_auto_routes --- Cargo.toml | 2 + src/endpoints/addr_has_rev.rs | 2 + src/endpoints/addr_to_available_ids.rs | 2 + src/endpoints/addr_to_domain.rs | 2 + src/endpoints/addr_to_external_domains.rs | 6 + src/endpoints/addr_to_full_ids.rs | 2 + src/endpoints/addr_to_token_id.rs | 2 + src/endpoints/addrs_to_domains.rs | 2 + src/endpoints/crosschain/solana/claim.rs | 6 + src/endpoints/data_to_ids.rs | 2 + src/endpoints/domain_to_addr.rs | 2 + src/endpoints/domain_to_data.rs | 2 + src/endpoints/galxe/verify.rs | 2 + src/endpoints/id_to_data.rs | 2 + src/endpoints/referral/add_click.rs | 2 + src/endpoints/referral/click_count.rs | 2 + src/endpoints/referral/revenue.rs | 2 + src/endpoints/referral/sales_count.rs | 2 + src/endpoints/renewal/get_metahash.rs | 2 + .../renewal/get_non_subscribed_domains.rs | 6 + src/endpoints/renewal/get_renewal_data.rs | 6 + src/endpoints/starkscan/fetch_nfts.rs | 2 + src/endpoints/stats/count_addrs.rs | 2 + src/endpoints/stats/count_club_domains.rs | 2 + src/endpoints/stats/count_created.rs | 2 + src/endpoints/stats/count_domains.rs | 2 + src/endpoints/stats/count_ids.rs | 2 + src/endpoints/stats/count_renewed.rs | 2 + src/endpoints/stats/expired_club_domains.rs | 6 + src/endpoints/uri.rs | 2 + src/main.rs | 118 +++--------------- src/utils.rs | 29 ++++- 32 files changed, 125 insertions(+), 102 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 165e5a0..90eb708 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,7 @@ edition = "2021" [dependencies] starknet = { git = "https://github.com/xJonathanLEI/starknet-rs", rev = "c974e5cb42e8d8344cee910b76005ec46b4dd3ed" } starknet-id = { git = "https://github.com/starknet-id/starknetid.rs", rev = "2b30c2453b96789a628c86d2edebb1023fa2e77d" } +axum_auto_routes = { git = "https://github.com/Th0rgal/axum_auto_routes.git", rev = "f9e1d2083e887cd264642359c4aa851938da6f09" } axum = "0.6.18" futures = "0.3.28" mongodb = "2.5.0" @@ -26,3 +27,4 @@ lazy_static = "1.4.0" regex = "1.10.2" bs58 = "0.5.0" ed25519-dalek = "2.1.0" +ctor = "0.2.6" diff --git a/src/endpoints/addr_has_rev.rs b/src/endpoints/addr_has_rev.rs index 88abcc9..cddb10b 100644 --- a/src/endpoints/addr_has_rev.rs +++ b/src/endpoints/addr_has_rev.rs @@ -8,6 +8,7 @@ use axum::{ response::IntoResponse, Json, }; +use axum_auto_routes::route; use mongodb::bson::doc; use serde::{Deserialize, Serialize}; use starknet::core::types::FieldElement; @@ -23,6 +24,7 @@ pub struct AddrHasRevQuery { addr: FieldElement, } +#[route(get, "/addr_has_rev", crate::endpoints::addr_has_rev)] pub async fn handler( State(state): State>, Query(query): Query, diff --git a/src/endpoints/addr_to_available_ids.rs b/src/endpoints/addr_to_available_ids.rs index c920216..8fc6e26 100644 --- a/src/endpoints/addr_to_available_ids.rs +++ b/src/endpoints/addr_to_available_ids.rs @@ -9,6 +9,7 @@ use axum::{ response::IntoResponse, Json, }; +use axum_auto_routes::route; use futures::StreamExt; use mongodb::bson::doc; use serde::{Deserialize, Serialize}; @@ -25,6 +26,7 @@ pub struct AddrQuery { addr: FieldElement, } +#[route(get, "/addr_to_available_ids", crate::endpoints::addr_to_available_ids)] pub async fn handler( State(state): State>, Query(query): Query, diff --git a/src/endpoints/addr_to_domain.rs b/src/endpoints/addr_to_domain.rs index 1b61f75..e172fc4 100644 --- a/src/endpoints/addr_to_domain.rs +++ b/src/endpoints/addr_to_domain.rs @@ -9,6 +9,7 @@ use axum::{ response::IntoResponse, Json, }; +use axum_auto_routes::route; use futures::StreamExt; use mongodb::{ bson::{doc, Document}, @@ -53,6 +54,7 @@ async fn aggregate_data( read_cursor(cursor).await } +#[route(get, "/addr_to_domain", crate::endpoints::addr_to_domain)] pub async fn handler( State(state): State>, Query(query): Query, diff --git a/src/endpoints/addr_to_external_domains.rs b/src/endpoints/addr_to_external_domains.rs index 0a953e4..637f1c3 100644 --- a/src/endpoints/addr_to_external_domains.rs +++ b/src/endpoints/addr_to_external_domains.rs @@ -7,6 +7,7 @@ use axum::{ http::{HeaderMap, HeaderValue, StatusCode}, response::IntoResponse, }; +use axum_auto_routes::route; use futures::StreamExt; use mongodb::bson::doc; use serde::{Deserialize, Serialize}; @@ -23,6 +24,11 @@ pub struct DomainQuery { addr: FieldElement, } +#[route( + get, + "/addr_to_external_domains", + crate::endpoints::addr_to_external_domains +)] pub async fn handler( State(state): State>, Query(query): Query, diff --git a/src/endpoints/addr_to_full_ids.rs b/src/endpoints/addr_to_full_ids.rs index 0bf7dc2..7002295 100644 --- a/src/endpoints/addr_to_full_ids.rs +++ b/src/endpoints/addr_to_full_ids.rs @@ -8,6 +8,7 @@ use axum::{ response::IntoResponse, Json, }; +use axum_auto_routes::route; use futures::future::join_all; use futures::stream::StreamExt; use mongodb::{ @@ -52,6 +53,7 @@ pub struct FullIdResponse { full_ids: Vec, } +#[route(get, "/addr_to_full_ids", crate::endpoints::addr_to_full_ids)] pub async fn handler( State(state): State>, Query(query): Query, diff --git a/src/endpoints/addr_to_token_id.rs b/src/endpoints/addr_to_token_id.rs index 67f4465..c94de63 100644 --- a/src/endpoints/addr_to_token_id.rs +++ b/src/endpoints/addr_to_token_id.rs @@ -7,6 +7,7 @@ use axum::{ http::{HeaderMap, HeaderValue, StatusCode}, response::{IntoResponse, Json}, }; +use axum_auto_routes::route; use mongodb::bson::{doc, Bson}; use serde::{Deserialize, Serialize}; use starknet::core::types::FieldElement; @@ -22,6 +23,7 @@ pub struct TokenIdQuery { addr: FieldElement, } +#[route(get, "/addr_to_token_id", crate::endpoints::addr_to_token_id)] pub async fn handler( State(state): State>, Query(query): Query, diff --git a/src/endpoints/addrs_to_domains.rs b/src/endpoints/addrs_to_domains.rs index 71e46ce..192e12b 100644 --- a/src/endpoints/addrs_to_domains.rs +++ b/src/endpoints/addrs_to_domains.rs @@ -5,6 +5,7 @@ use axum::{ http::StatusCode, response::IntoResponse, }; +use axum_auto_routes::route; use futures::stream::StreamExt; use mongodb::{ bson::{doc, Document}, @@ -56,6 +57,7 @@ async fn run_aggregation_pipeline( process_cursor(cursor, results).await } +#[route(post, "/addrs_to_domains", crate::endpoints::addrs_to_domains)] pub async fn handler( State(state): State>, Json(query): Json, diff --git a/src/endpoints/crosschain/solana/claim.rs b/src/endpoints/crosschain/solana/claim.rs index d592531..4b00e9c 100644 --- a/src/endpoints/crosschain/solana/claim.rs +++ b/src/endpoints/crosschain/solana/claim.rs @@ -8,6 +8,7 @@ use crate::{ utils::{get_error, to_hex}, }; use axum::{extract::State, http::StatusCode, response::IntoResponse, Json}; +use axum_auto_routes::route; use chrono::{Duration, Utc}; use ed25519_dalek::{Signature, Verifier, VerifyingKey}; use mongodb::bson::doc; @@ -64,6 +65,11 @@ lazy_static::lazy_static! { static ref SOL_SUBDOMAIN_STR: FieldElement = FieldElement::from_dec_str("9145722242464647959622012987758").unwrap(); } +#[route( + post, + "/crosschain/solana/claim", + crate::endpoints::crosschain::solana::claim +)] pub async fn handler( State(state): State>, Json(query): Json, diff --git a/src/endpoints/data_to_ids.rs b/src/endpoints/data_to_ids.rs index 2e3612f..8aeead0 100644 --- a/src/endpoints/data_to_ids.rs +++ b/src/endpoints/data_to_ids.rs @@ -7,6 +7,7 @@ use axum::{ http::{HeaderMap, HeaderValue, StatusCode}, response::{IntoResponse, Json}, }; +use axum_auto_routes::route; use mongodb::bson::doc; use serde::{Deserialize, Serialize}; use starknet::core::types::FieldElement; @@ -24,6 +25,7 @@ pub struct StarknetIdQuery { data: FieldElement, } +#[route(get, "/data_to_ids", crate::endpoints::data_to_ids)] pub async fn handler( State(state): State>, Query(query): Query, diff --git a/src/endpoints/domain_to_addr.rs b/src/endpoints/domain_to_addr.rs index 997ac68..63c67cc 100644 --- a/src/endpoints/domain_to_addr.rs +++ b/src/endpoints/domain_to_addr.rs @@ -7,6 +7,7 @@ use axum::{ http::{HeaderMap, HeaderValue, StatusCode}, response::{IntoResponse, Json}, }; +use axum_auto_routes::route; use futures::StreamExt; use mongodb::{bson::doc, options::AggregateOptions}; use serde::{Deserialize, Serialize}; @@ -23,6 +24,7 @@ pub struct DomainQuery { domain: String, } +#[route(get, "/domain_to_addr", crate::endpoints::domain_to_addr)] pub async fn handler( State(state): State>, Query(query): Query, diff --git a/src/endpoints/domain_to_data.rs b/src/endpoints/domain_to_data.rs index 98ad2cd..655f26f 100644 --- a/src/endpoints/domain_to_data.rs +++ b/src/endpoints/domain_to_data.rs @@ -7,6 +7,7 @@ use axum::{ http::{HeaderMap, HeaderValue, StatusCode}, response::{IntoResponse, Json}, }; +use axum_auto_routes::route; use futures::StreamExt; use mongodb::bson::{doc, from_bson, Bson, Document}; use serde::Deserialize; @@ -17,6 +18,7 @@ pub struct DomainQuery { domain: String, } +#[route(get, "/domain_to_data", crate::endpoints::domain_to_data)] pub async fn handler( State(state): State>, Query(query): Query, diff --git a/src/endpoints/galxe/verify.rs b/src/endpoints/galxe/verify.rs index c972732..c3dcc9b 100644 --- a/src/endpoints/galxe/verify.rs +++ b/src/endpoints/galxe/verify.rs @@ -2,6 +2,7 @@ use std::sync::Arc; use crate::models::AppState; use axum::{extract::State, http::StatusCode, response::IntoResponse, Json}; +use axum_auto_routes::route; use futures::StreamExt; use mongodb::{bson::doc, bson::Document}; use serde::{Deserialize, Serialize}; @@ -16,6 +17,7 @@ pub struct SimpleResponse { result: &'static str, } +#[route(post, "/galxe/verify", crate::endpoints::galxe::verify)] pub async fn handler( State(state): State>, Json(query): Json, diff --git a/src/endpoints/id_to_data.rs b/src/endpoints/id_to_data.rs index 1e1e0ec..8339409 100644 --- a/src/endpoints/id_to_data.rs +++ b/src/endpoints/id_to_data.rs @@ -7,6 +7,7 @@ use axum::{ http::{HeaderMap, HeaderValue, StatusCode}, response::{IntoResponse, Json}, }; +use axum_auto_routes::route; use futures::StreamExt; use mongodb::bson::{doc, from_bson, Bson, Document}; use serde::Deserialize; @@ -18,6 +19,7 @@ pub struct IdQuery { id: FieldElement, } +#[route(get, "/id_to_data", crate::endpoints::id_to_data)] pub async fn handler( State(state): State>, Query(query): Query, diff --git a/src/endpoints/referral/add_click.rs b/src/endpoints/referral/add_click.rs index f73244b..4ec6cb5 100644 --- a/src/endpoints/referral/add_click.rs +++ b/src/endpoints/referral/add_click.rs @@ -4,6 +4,7 @@ use axum::{ http::StatusCode, response::IntoResponse, }; +use axum_auto_routes::route; use chrono::Utc; use mongodb::{ bson::{doc, DateTime as BsonDateTime}, @@ -18,6 +19,7 @@ pub struct AddClickQuery { sponsor_addr: FieldElement, } +#[route(post, "/referral/add_click", crate::endpoints::referral::add_click)] pub async fn handler( State(state): State>, Json(query): Json, diff --git a/src/endpoints/referral/click_count.rs b/src/endpoints/referral/click_count.rs index 4ff7b01..c457091 100644 --- a/src/endpoints/referral/click_count.rs +++ b/src/endpoints/referral/click_count.rs @@ -4,6 +4,7 @@ use axum::{ http::{HeaderMap, HeaderValue, StatusCode}, response::{IntoResponse, Json}, }; +use axum_auto_routes::route; use chrono::{DateTime, NaiveDateTime, Utc}; use futures::StreamExt; use mongodb::bson::{doc, Bson, DateTime as BsonDateTime}; @@ -22,6 +23,7 @@ pub struct IdQuery { spacing: i64, } +#[route(get, "/referral/click_count", crate::endpoints::referral::click_count)] pub async fn handler( State(state): State>, Query(query): Query, diff --git a/src/endpoints/referral/revenue.rs b/src/endpoints/referral/revenue.rs index 35dd284..680f11f 100644 --- a/src/endpoints/referral/revenue.rs +++ b/src/endpoints/referral/revenue.rs @@ -4,6 +4,7 @@ use axum::{ http::{HeaderMap, HeaderValue, StatusCode}, response::{IntoResponse, Json}, }; +use axum_auto_routes::route; use chrono::{DateTime, NaiveDateTime, Utc}; use futures::StreamExt; use mongodb::bson::{doc, Bson, DateTime as BsonDateTime}; @@ -22,6 +23,7 @@ pub struct IdQuery { spacing: i64, } +#[route(get, "/referral/revenue", crate::endpoints::referral::revenue)] pub async fn handler( State(state): State>, Query(query): Query, diff --git a/src/endpoints/referral/sales_count.rs b/src/endpoints/referral/sales_count.rs index 2e84d56..f2fe2c9 100644 --- a/src/endpoints/referral/sales_count.rs +++ b/src/endpoints/referral/sales_count.rs @@ -4,6 +4,7 @@ use axum::{ http::{HeaderMap, HeaderValue, StatusCode}, response::{IntoResponse, Json}, }; +use axum_auto_routes::route; use chrono::{DateTime, NaiveDateTime, Utc}; use futures::StreamExt; use mongodb::bson::{doc, Bson, DateTime as BsonDateTime}; @@ -22,6 +23,7 @@ pub struct IdQuery { spacing: i64, } +#[route(get, "/referral/sales_count", crate::endpoints::referral::sales_count)] pub async fn handler( State(state): State>, Query(query): Query, diff --git a/src/endpoints/renewal/get_metahash.rs b/src/endpoints/renewal/get_metahash.rs index b8a5dc3..c571076 100644 --- a/src/endpoints/renewal/get_metahash.rs +++ b/src/endpoints/renewal/get_metahash.rs @@ -7,6 +7,7 @@ use axum::{ http::{HeaderMap, HeaderValue, StatusCode}, response::{IntoResponse, Json}, }; +use axum_auto_routes::route; use futures::TryStreamExt; use mongodb::bson::{doc, Bson}; use serde::{Deserialize, Serialize}; @@ -24,6 +25,7 @@ pub struct GetMetaHashQuery { addr: FieldElement, } +#[route(get, "/renewal/get_metahash", crate::endpoints::renewal::get_metahash)] pub async fn handler( State(state): State>, Query(query): Query, diff --git a/src/endpoints/renewal/get_non_subscribed_domains.rs b/src/endpoints/renewal/get_non_subscribed_domains.rs index dc7205e..3edaeb0 100644 --- a/src/endpoints/renewal/get_non_subscribed_domains.rs +++ b/src/endpoints/renewal/get_non_subscribed_domains.rs @@ -7,6 +7,7 @@ use axum::{ http::StatusCode, response::{IntoResponse, Json}, }; +use axum_auto_routes::route; use futures::StreamExt; use mongodb::{bson::doc, options::AggregateOptions}; use regex::Regex; @@ -23,6 +24,11 @@ lazy_static::lazy_static! { static ref DOMAIN_REGEX: Regex = Regex::new(r"^[^.]+\.stark$").unwrap(); } +#[route( + get, + "/renewal/get_non_subscribed_domains", + crate::endpoints::renewal::get_non_subscribed_domains +)] pub async fn handler( State(state): State>, Query(query): Query, diff --git a/src/endpoints/renewal/get_renewal_data.rs b/src/endpoints/renewal/get_renewal_data.rs index 10ef5f7..e669035 100644 --- a/src/endpoints/renewal/get_renewal_data.rs +++ b/src/endpoints/renewal/get_renewal_data.rs @@ -7,6 +7,7 @@ use axum::{ http::{HeaderMap, HeaderValue, StatusCode}, response::{IntoResponse, Json}, }; +use axum_auto_routes::route; use futures::StreamExt; use mongodb::{bson::doc, options::FindOptions}; use serde::{Deserialize, Serialize}; @@ -24,6 +25,11 @@ pub struct StarknetIdQuery { domain: String, } +#[route( + get, + "/renewal/get_renewal_data", + crate::endpoints::renewal::get_renewal_data +)] pub async fn handler( State(state): State>, Query(query): Query, diff --git a/src/endpoints/starkscan/fetch_nfts.rs b/src/endpoints/starkscan/fetch_nfts.rs index 8a6c378..a034014 100644 --- a/src/endpoints/starkscan/fetch_nfts.rs +++ b/src/endpoints/starkscan/fetch_nfts.rs @@ -7,6 +7,7 @@ use axum::{ http::StatusCode, response::{IntoResponse, Json}, }; +use axum_auto_routes::route; use mongodb::bson::doc; use serde::{Deserialize, Serialize}; use serde_json::Value; @@ -44,6 +45,7 @@ pub struct StarkscanNftProps { minted_at_timestamp: i64, } +#[route(get, "/starkscan/fetch_nfts", crate::endpoints::starkscan::fetch_nfts)] pub async fn handler( State(state): State>, Query(query): Query, diff --git a/src/endpoints/stats/count_addrs.rs b/src/endpoints/stats/count_addrs.rs index 31a4605..a11e2c4 100644 --- a/src/endpoints/stats/count_addrs.rs +++ b/src/endpoints/stats/count_addrs.rs @@ -5,6 +5,7 @@ use axum::{ response::IntoResponse, Json, }; +use axum_auto_routes::route; use futures::StreamExt; use mongodb::bson::{doc, Bson}; use serde::{Deserialize, Serialize}; @@ -20,6 +21,7 @@ pub struct CountAddrsQuery { since: i64, } +#[route(get, "/stats/count_addrs", crate::endpoints::stats::count_addrs)] pub async fn handler( State(state): State>, Query(query): Query, diff --git a/src/endpoints/stats/count_club_domains.rs b/src/endpoints/stats/count_club_domains.rs index 70062ff..87278da 100644 --- a/src/endpoints/stats/count_club_domains.rs +++ b/src/endpoints/stats/count_club_domains.rs @@ -5,6 +5,7 @@ use axum::{ response::IntoResponse, Json, }; +use axum_auto_routes::route; use futures::TryStreamExt; use mongodb::bson::{self, doc, Bson}; use serde::{Deserialize, Serialize}; @@ -22,6 +23,7 @@ pub struct CountClubDomainsQuery { since: i64, } +#[route(get, "/stats/count_club_domains", crate::endpoints::stats::count_club_domains)] pub async fn handler( State(state): State>, Query(query): Query, diff --git a/src/endpoints/stats/count_created.rs b/src/endpoints/stats/count_created.rs index 1b89b14..5c85b12 100644 --- a/src/endpoints/stats/count_created.rs +++ b/src/endpoints/stats/count_created.rs @@ -5,6 +5,7 @@ use axum::{ response::IntoResponse, Json, }; +use axum_auto_routes::route; use futures::StreamExt; use mongodb::bson::{doc, Bson}; use serde::{Deserialize, Serialize}; @@ -23,6 +24,7 @@ pub struct CountCreatedQuery { segments: i64, } +#[route(get, "/stats/count_created", crate::endpoints::stats::count_created)] pub async fn handler( State(state): State>, Query(query): Query, diff --git a/src/endpoints/stats/count_domains.rs b/src/endpoints/stats/count_domains.rs index 93ed665..6ca89f0 100644 --- a/src/endpoints/stats/count_domains.rs +++ b/src/endpoints/stats/count_domains.rs @@ -5,6 +5,7 @@ use axum::{ response::IntoResponse, Json, }; +use axum_auto_routes::route; use mongodb::bson::{doc, Bson}; use serde::{Deserialize, Serialize}; use std::sync::Arc; @@ -19,6 +20,7 @@ pub struct CountDomainsQuery { since: i64, } +#[route(get, "/stats/count_domains", crate::endpoints::stats::count_domains)] pub async fn handler( State(state): State>, Query(query): Query, diff --git a/src/endpoints/stats/count_ids.rs b/src/endpoints/stats/count_ids.rs index 93ed665..381410e 100644 --- a/src/endpoints/stats/count_ids.rs +++ b/src/endpoints/stats/count_ids.rs @@ -5,6 +5,7 @@ use axum::{ response::IntoResponse, Json, }; +use axum_auto_routes::route; use mongodb::bson::{doc, Bson}; use serde::{Deserialize, Serialize}; use std::sync::Arc; @@ -19,6 +20,7 @@ pub struct CountDomainsQuery { since: i64, } +#[route(get, "/stats/count_ids", crate::endpoints::stats::count_ids)] pub async fn handler( State(state): State>, Query(query): Query, diff --git a/src/endpoints/stats/count_renewed.rs b/src/endpoints/stats/count_renewed.rs index c905199..46ec8fc 100644 --- a/src/endpoints/stats/count_renewed.rs +++ b/src/endpoints/stats/count_renewed.rs @@ -5,6 +5,7 @@ use axum::{ response::IntoResponse, Json, }; +use axum_auto_routes::route; use futures::StreamExt; use mongodb::bson::{doc, Bson}; use serde::{Deserialize, Serialize}; @@ -23,6 +24,7 @@ pub struct CountRenewedQuery { segments: i64, } +#[route(get, "/stats/count_renewed", crate::endpoints::stats::count_renewed)] pub async fn handler( State(state): State>, Query(query): Query, diff --git a/src/endpoints/stats/expired_club_domains.rs b/src/endpoints/stats/expired_club_domains.rs index 39fb3b0..74bf647 100644 --- a/src/endpoints/stats/expired_club_domains.rs +++ b/src/endpoints/stats/expired_club_domains.rs @@ -5,6 +5,7 @@ use axum::{ response::IntoResponse, Json, }; +use axum_auto_routes::route; use futures::StreamExt; use mongodb::{ bson::{doc, Bson}, @@ -19,6 +20,11 @@ pub struct CountClubDomainsData { count: i32, } +#[route( + get, + "/stats/expired_club_domains", + crate::endpoints::stats::expired_club_domains +)] pub async fn handler(State(state): State>) -> impl IntoResponse { let mut headers = HeaderMap::new(); headers.insert("Cache-Control", HeaderValue::from_static("max-age=60")); diff --git a/src/endpoints/uri.rs b/src/endpoints/uri.rs index 7536b91..9a11d71 100644 --- a/src/endpoints/uri.rs +++ b/src/endpoints/uri.rs @@ -7,6 +7,7 @@ use axum::{ http::{HeaderMap, HeaderValue, StatusCode}, response::{IntoResponse, Json}, }; +use axum_auto_routes::route; use chrono::NaiveDateTime; use futures::StreamExt; use mongodb::bson::doc; @@ -47,6 +48,7 @@ const NFT_PP_CONTRACT: &'static str = const NFT_PP_ID: &'static str = "0x00000000000000000000000000000000000000000000006e66745f70705f6964"; +#[route(get, "/uri", crate::endpoints::uri)] pub async fn handler( State(state): State>, Query(query): Query, diff --git a/src/main.rs b/src/main.rs index 054eabb..f240bd3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,17 +6,19 @@ mod models; mod resolving; mod tax; mod utils; -use axum::{ - http::StatusCode, - routing::{get, post}, - Router, -}; +use axum::{http::StatusCode, Router}; +use axum_auto_routes::route; use mongodb::{bson::doc, options::ClientOptions, Client}; -use std::net::SocketAddr; use std::sync::Arc; +use std::{net::SocketAddr, sync::Mutex}; +use utils::WithState; use tower_http::cors::{Any, CorsLayer}; +lazy_static::lazy_static! { + pub static ref ROUTE_REGISTRY: Mutex>> = Mutex::new(Vec::new()); +} + #[tokio::main] async fn main() { println!("starknetid_server: starting v{}", env!("CARGO_PKG_VERSION")); @@ -58,101 +60,14 @@ async fn main() { } let cors = CorsLayer::new().allow_headers(Any).allow_origin(Any); - let app = Router::new() - .route("/", get(root)) - .route( - "/addr_to_available_ids", - get(endpoints::addr_to_available_ids::handler), - ) - .route("/addr_to_domain", get(endpoints::addr_to_domain::handler)) - .route( - "/addr_to_external_domains", - get(endpoints::addr_to_external_domains::handler), - ) - .route( - "/addr_to_full_ids", - get(endpoints::addr_to_full_ids::handler), - ) - .route( - "/addr_to_token_id", - get(endpoints::addr_to_token_id::handler), - ) - .route( - "/addrs_to_domains", - post(endpoints::addrs_to_domains::handler), - ) - .route("/data_to_ids", get(endpoints::data_to_ids::handler)) - .route("/domain_to_addr", get(endpoints::domain_to_addr::handler)) - .route("/domain_to_data", get(endpoints::domain_to_data::handler)) - .route("/addr_has_rev", get(endpoints::addr_has_rev::handler)) - .route("/id_to_data", get(endpoints::id_to_data::handler)) - .route("/uri", get(endpoints::uri::handler)) - .route( - "/referral/add_click", - post(endpoints::referral::add_click::handler), - ) - .route( - "/referral/revenue", - get(endpoints::referral::revenue::handler), - ) - .route( - "/referral/sales_count", - get(endpoints::referral::sales_count::handler), - ) - .route( - "/referral/click_count", - get(endpoints::referral::click_count::handler), - ) - .route( - "/stats/count_addrs", - get(endpoints::stats::count_addrs::handler), - ) - .route( - "/stats/count_club_domains", - get(endpoints::stats::count_club_domains::handler), - ) - .route( - "/stats/count_domains", - get(endpoints::stats::count_domains::handler), - ) - .route( - "/stats/count_ids", - get(endpoints::stats::count_ids::handler), - ) - .route( - "/stats/count_created", - get(endpoints::stats::count_created::handler), - ) - .route( - "/stats/expired_club_domains", - get(endpoints::stats::expired_club_domains::handler), - ) - .route( - "/stats/count_renewed", - get(endpoints::stats::count_renewed::handler), - ) - .route( - "/starkscan/fetch_nfts", - get(endpoints::starkscan::fetch_nfts::handler), - ) - .route( - "/renewal/get_renewal_data", - get(endpoints::renewal::get_renewal_data::handler), - ) - .route( - "/renewal/get_metahash", - get(endpoints::renewal::get_metahash::handler), - ) - .route( - "/renewal/get_non_subscribed_domains", - get(endpoints::renewal::get_non_subscribed_domains::handler), - ) - .route("/galxe/verify", post(endpoints::galxe::verify::handler)) - .route( - "/crosschain/solana/claim", - post(endpoints::crosschain::solana::claim::handler), - ) - .with_state(shared_state) + let app = ROUTE_REGISTRY + .lock() + .unwrap() + .clone() + .into_iter() + .fold(Router::new().with_state(shared_state.clone()), |acc, r| { + acc.merge(r.to_router(shared_state.clone())) + }) .layer(cors); let addr = SocketAddr::from(([0, 0, 0, 0], conf.server.port)); @@ -163,6 +78,7 @@ async fn main() { .unwrap(); } +#[route(get, "/")] async fn root() -> (StatusCode, String) { ( StatusCode::ACCEPTED, diff --git a/src/utils.rs b/src/utils.rs index 7820a01..01e3bfa 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,12 +1,16 @@ use ark_ff::{biginteger::BigInteger256, BigInteger}; use axum::{ + body::Body, http::StatusCode, response::{IntoResponse, Response}, + Router, }; use serde::Serialize; use serde_json::Value; use starknet::core::types::FieldElement; -use std::fmt::Write; +use std::{fmt::Write, sync::Arc}; + +use crate::models::AppState; #[derive(Serialize)] pub struct ErrorMessage { @@ -102,3 +106,26 @@ pub async fn fetch_img_url( json.get("image_url") .and_then(|v| v.as_str().map(ToString::to_string)) } + +// required for axum_auto_routes +pub trait WithState: Send { + fn to_router(self: Box, shared_state: Arc) -> Router; + + fn box_clone(&self) -> Box; +} + +impl WithState for Router, Body> { + fn to_router(self: Box, shared_state: Arc) -> Router { + self.with_state(shared_state) + } + + fn box_clone(&self) -> Box { + Box::new((*self).clone()) + } +} + +impl Clone for Box { + fn clone(&self) -> Box { + self.box_clone() + } +}