init_mint.rs 941 B

12345678910111213141516171819202122232425262728293031323334353637
  1. use std::sync::Arc;
  2. use anyhow::Result;
  3. use axum::Router;
  4. use cdk::mint::Mint;
  5. use tokio::sync::Notify;
  6. use tower_http::cors::CorsLayer;
  7. use tracing::instrument;
  8. #[instrument(skip_all)]
  9. pub async fn start_mint(addr: &str, port: u16, mint: Mint) -> Result<()> {
  10. let mint_arc = Arc::new(mint);
  11. let v1_service = cdk_axum::create_mint_router(Arc::clone(&mint_arc))
  12. .await
  13. .unwrap();
  14. let mint_service = Router::new()
  15. .merge(v1_service)
  16. .layer(CorsLayer::permissive());
  17. let mint = Arc::clone(&mint_arc);
  18. let shutdown = Arc::new(Notify::new());
  19. tokio::spawn({
  20. let shutdown = Arc::clone(&shutdown);
  21. async move { mint.wait_for_paid_invoices(shutdown).await }
  22. });
  23. tracing::info!("Staring Axum server");
  24. axum::Server::bind(&format!("{}:{}", addr, port).as_str().parse().unwrap())
  25. .serve(mint_service.into_make_service())
  26. .await?;
  27. Ok(())
  28. }