-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.rs
More file actions
123 lines (101 loc) · 4.18 KB
/
Copy pathmain.rs
File metadata and controls
123 lines (101 loc) · 4.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
mod api;
mod services;
mod models;
mod storage;
use axum::{
routing::{get, post},
Router,
};
use std::net::SocketAddr;
use tower_http::{
cors::{Any, CorsLayer},
trace::TraceLayer,
};
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
use dotenv::dotenv;
use tokio::net::TcpListener;
#[tokio::main]
async fn main() {
// load env vars
dotenv().ok();
// initialize logging
tracing_subscriber::registry()
.with(tracing_subscriber::EnvFilter::new(
std::env::var("RUST_LOG").unwrap_or_else(|_| "info".into()),
))
.with(tracing_subscriber::fmt::layer())
.init();
// create data directory if it doesn't exist
let data_dir = std::env::var("DATA_DIR").unwrap_or_else(|_| "./data".to_string());
std::fs::create_dir_all(&data_dir).expect("Failed to create data directory");
// initialize Ark client
match services::APP_STATE.initialize().await {
Ok(_) => tracing::info!("Ark client initialized successfully"),
Err(e) => tracing::error!("Failed to initialize Ark client: {}", e),
}
let app_state = services::APP_STATE.clone();
tokio::spawn(async move {
loop {
// Sync every 30 seconds
tokio::time::sleep(tokio::time::Duration::from_secs(30)).await;
let grpc_client = app_state.grpc_client.lock().await;
if grpc_client.is_connected() {
match grpc_client.update_app_state().await {
Ok(_) => tracing::debug!("Successfully synced app state with Ark client"),
Err(e) => tracing::warn!("Failed to sync app state with Ark client: {}", e),
}
}
}
});
// CORS layer
let cors = CorsLayer::new()
.allow_origin(Any)
.allow_methods(Any)
.allow_headers(Any);
let app = Router::new()
// wallet routes
.route("/api/wallet/info", get(api::wallet::get_info))
.route("/api/wallet/balance", get(api::wallet::get_balance))
.route("/api/wallet/address", get(api::wallet::get_address))
.route("/api/wallet/boarding-address", get(api::wallet::get_boarding_address))
.route("/api/wallet/onchain-address", get(api::wallet::get_onchain_address))
.route("/api/wallet/send", post(api::wallet::send_vtxo))
.route("/api/wallet/available-balance", get(api::wallet::get_available_balance))
// .route("/api/wallet/check-deposits", post(api::wallet::check_deposits))
.route("/api/wallet/receive", post(api::wallet::receive_vtxo))
// on-chain tx
.route("/api/wallet/onchain-balance", get(api::wallet::get_onchain_balance))
.route("/api/wallet/fee-estimates", get(api::wallet::get_fee_estimates_detailed))
.route("/api/wallet/estimate-transaction-fees", post(api::wallet::estimate_transaction_fees))
.route("/api/wallet/send-onchain", post(api::wallet::send_onchain_with_priority))
// tx routes
.route("/api/transactions", get(api::transactions::get_history))
.route("/api/transactions/:txid", get(api::transactions::get_transaction))
// round participation
.route("/api/round/participate", post(api::transactions::participate_in_round))
// unilateral exit
.route("/api/transactions/exit", post(api::transactions::unilateral_exit))
// debug
.route("/api/debug/vtxos", get(api::wallet::debug_vtxos))
// add middleware
.layer(TraceLayer::new_for_http())
.layer(cors);
// run the server
let port = std::env::var("PORT")
.unwrap_or_else(|_| "3000".to_string())
.parse::<u16>()
.expect("PORT must be a number");
let addr = SocketAddr::from(([0, 0, 0, 0], port));
let listener = TcpListener::bind(addr).await.unwrap();
tracing::info!("listening on {}", addr);
axum::serve(listener, app)
.with_graceful_shutdown(shutdown_signal())
.await
.unwrap();
}
async fn shutdown_signal() {
tokio::signal::ctrl_c()
.await
.expect("Failed to install CTRL+C signal handler");
tracing::info!("Shutting down gracefully...");
}