88 lines
3.9 KiB
Rust
88 lines
3.9 KiB
Rust
use actix_web::{web, App, HttpServer};
|
|
use tracing::info;
|
|
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
|
|
use dotenvy::dotenv;
|
|
use std::env;
|
|
mod config;
|
|
mod models;
|
|
mod handlers;
|
|
mod services;
|
|
mod middleware;
|
|
mod errors;
|
|
use middleware as mw;
|
|
use config::AppConfig;
|
|
#[actix_web::main]
|
|
async fn main() -> std::io::Result<()> {
|
|
dotenv().ok();
|
|
tracing_subscriber::registry()
|
|
.with(tracing_subscriber::EnvFilter::new(
|
|
env::var("RUST_LOG").unwrap_or_else(|_| "nac_wallet_service=info,actix_web=info".into()),
|
|
))
|
|
.with(tracing_subscriber::fmt::layer())
|
|
.init();
|
|
info!("NAC原生钱包微服务启动中...");
|
|
let config = AppConfig::from_env().expect("配置加载失败");
|
|
let bind_addr = format!("{}:{}", config.host, config.port);
|
|
let db_pool = config::create_db_pool(&config)
|
|
.await
|
|
.expect("数据库连接池创建失败");
|
|
let db_pool = web::Data::new(db_pool);
|
|
info!("数据库连接池初始化成功");
|
|
info!("服务监听地址: {}", bind_addr);
|
|
let app_config = web::Data::new(config);
|
|
HttpServer::new(move || {
|
|
App::new()
|
|
.app_data(db_pool.clone())
|
|
.app_data(app_config.clone())
|
|
.app_data(web::JsonConfig::default().limit(1_048_576))
|
|
.wrap(mw::RequestLogger)
|
|
.wrap(mw::SecurityHeaders)
|
|
.service(
|
|
web::scope("/v1")
|
|
// 钱包管理
|
|
.service(
|
|
web::scope("/wallets")
|
|
.route("", web::post().to(handlers::wallet::create_wallet))
|
|
.route("/{user_id}", web::get().to(handlers::wallet::get_wallet))
|
|
.route("/{user_id}/assets", web::get().to(handlers::wallet::get_assets))
|
|
)
|
|
// 交易签名
|
|
.service(
|
|
web::scope("/transactions")
|
|
.route("/sign", web::post().to(handlers::transaction::sign_transaction))
|
|
.route("/transfer", web::post().to(handlers::transaction::transfer))
|
|
.route("/{wallet_id}/history", web::get().to(handlers::transaction::get_history))
|
|
)
|
|
// 跨链桥
|
|
.service(
|
|
web::scope("/bridge")
|
|
.route("/addresses", web::get().to(handlers::bridge::get_chain_addresses))
|
|
.route("/assets", web::get().to(handlers::bridge::get_bridge_assets))
|
|
.route("/initiate", web::post().to(handlers::bridge::initiate_bridge))
|
|
.route("/status", web::get().to(handlers::bridge::get_bridge_status))
|
|
.route("/history", web::get().to(handlers::bridge::get_bridge_history))
|
|
)
|
|
// 手续费
|
|
.service(
|
|
web::scope("/fees")
|
|
.route("/estimate", web::post().to(handlers::fee::estimate_fee))
|
|
.route("/configs", web::get().to(handlers::fee::get_fee_configs))
|
|
)
|
|
// 后台管理
|
|
.service(
|
|
web::scope("/admin")
|
|
.wrap(mw::AdminAuth)
|
|
.route("/fees", web::put().to(handlers::admin::update_fee_config))
|
|
.route("/subsidies", web::post().to(handlers::admin::create_subsidy_plan))
|
|
.route("/subsidies/{id}/toggle", web::put().to(handlers::admin::toggle_subsidy))
|
|
.route("/stats", web::get().to(handlers::admin::get_stats))
|
|
)
|
|
// 健康检查
|
|
.route("/health", web::get().to(handlers::health::health_check))
|
|
)
|
|
})
|
|
.bind(&bind_addr)?
|
|
.run()
|
|
.await
|
|
}
|