use axum::{
middleware,
routing::{get, post},
Router,
};
use std::sync::Arc;
use tower_http::trace::TraceLayer;
use crate::api::handlers;
use crate::web::web5_adapter::Web5Adapter;
use super::handlers::auth::auth_middleware;
#[cfg(feature = "rust-bitcoin")]
use crate::bitcoin::wallet::Wallet;
pub fn configure_routes(wallet: Arc<Wallet>, web5_adapter: Arc<Web5Adapter>) -> Router {
let public_routes = Router::new()
.route("/health", get(handlers::system::health_check))
.route("/info", get(handlers::system::system_info))
.route("/login", post(handlers::auth::login));
let wallet_routes = Router::new()
.route("/wallets", post(handlers::wallet::create_wallet))
.route("/wallets/:id", get(handlers::wallet::get_wallet))
.route("/wallets/:id/balance", get(handlers::wallet::get_balance))
.route(
"/wallets/:id/address",
post(handlers::wallet::generate_address),
)
.route(
"/wallets/:id/transactions",
post(handlers::wallet::send_transaction),
)
.route(
"/wallets/:id/transactions",
get(handlers::wallet::list_transactions),
)
.with_state(wallet.clone());
let identity_routes = Router::new()
.route("/identities", post(handlers::identity::create_identity))
.route("/identities/:id", get(handlers::identity::get_identity))
.route("/credentials", post(handlers::identity::create_credential))
.route("/credentials/:id", get(handlers::identity::get_credential))
.route(
"/credentials/verify",
post(handlers::identity::verify_credential),
)
.with_state(web5_adapter.clone());
let dlc_routes = Router::new()
.route("/dlc", post(handlers::dlc::create_contract))
.route("/dlc/:id", get(handlers::dlc::get_contract))
.route("/dlc/:id/accept", post(handlers::dlc::accept_contract))
.route("/dlc/:id/finalize", post(handlers::dlc::finalize_contract))
.route("/dlc/:id/execute", post(handlers::dlc::execute_contract));
let authenticated_api = Router::new()
.merge(wallet_routes)
.merge(identity_routes)
.merge(dlc_routes)
.layer(middleware::from_fn(auth_middleware));
Router::new()
.nest("/api/v1", public_routes.merge(authenticated_api))
.layer(TraceLayer::new_for_http())
}
#[cfg(not(feature = "rust-bitcoin"))]
pub fn configure_routes(web5_adapter: Arc<Web5Adapter>) -> Router {
let public_routes = Router::new()
.route("/health", get(handlers::system::health_check))
.route("/info", get(handlers::system::system_info))
.route("/login", post(handlers::auth::login));
let identity_routes = Router::new()
.route("/identity", post(handlers::identity::create_identity))
.route("/identity/:id", get(handlers::identity::get_identity))
.route(
"/identity/resolve",
post(handlers::identity::resolve_identity),
)
.route(
"/credentials/issue",
post(handlers::identity::issue_credential),
)
.route(
"/credentials/verify",
post(handlers::identity::verify_credential),
)
.with_state(web5_adapter.clone());
let dlc_routes = Router::new()
.route("/dlc", post(handlers::dlc::create_contract))
.route("/dlc/:id", get(handlers::dlc::get_contract))
.route("/dlc/:id/accept", post(handlers::dlc::accept_contract))
.route("/dlc/:id/finalize", post(handlers::dlc::finalize_contract))
.route("/dlc/:id/execute", post(handlers::dlc::execute_contract));
let authenticated_api = Router::new()
.merge(identity_routes)
.merge(dlc_routes)
.layer(middleware::from_fn(auth_middleware));
Router::new()
.nest("/api/v1", public_routes.merge(authenticated_api))
.layer(TraceLayer::new_for_http())
}