NAC_Blockchain/nac-api-server/src/main.rs

58 lines
1.2 KiB
Rust

use axum::{
routing::get,
Router,
Json,
};
use serde::Serialize;
use tower_http::cors::{CorsLayer, Any};
use tracing_subscriber;
mod wallet;
mod exchange;
#[tokio::main]
async fn main() {
// 初始化日志
tracing_subscriber::fmt::init();
// 创建路由
let app = Router::new()
.route("/", get(root))
.route("/health", get(health_check))
// 钱包API
.nest("/api/wallet", wallet::routes())
// 交易所API
.nest("/api/exchange", exchange::routes())
// CORS配置
.layer(
CorsLayer::new()
.allow_origin(Any)
.allow_methods(Any)
.allow_headers(Any)
);
// 启动服务器
let addr = "0.0.0.0:8080";
println!("🚀 NAC API服务器启动在 http://{}", addr);
let listener = tokio::net::TcpListener::bind(addr).await.unwrap();
axum::serve(listener, app).await.unwrap();
}
async fn root() -> &'static str {
"NAC API Server v1.0.0"
}
async fn health_check() -> Json<HealthResponse> {
Json(HealthResponse {
status: "ok".to_string(),
version: "1.0.0".to_string(),
})
}
#[derive(Serialize)]
struct HealthResponse {
status: String,
version: String,
}