|
@@ -1,6 +1,9 @@
|
|
|
-use actix_web::{error::InternalError, middleware::Logger, web, App, HttpResponse, HttpServer};
|
|
|
+use axum::{
|
|
|
+ routing::{get, post},
|
|
|
+ Router,
|
|
|
+};
|
|
|
use serde::Serialize;
|
|
|
-use std::sync::Arc;
|
|
|
+use std::{net::SocketAddr, sync::Arc};
|
|
|
|
|
|
#[async_trait::async_trait]
|
|
|
pub trait Handler {
|
|
@@ -18,11 +21,12 @@ mod subscribe;
|
|
|
mod tx;
|
|
|
mod update;
|
|
|
|
|
|
+#[derive(Clone)]
|
|
|
pub struct Context {
|
|
|
ledger: Arc<verax::Ledger<verax::storage::Cache<verax::storage::SQLite>>>,
|
|
|
}
|
|
|
|
|
|
-#[actix_web::main]
|
|
|
+#[tokio::main]
|
|
|
async fn main() -> std::io::Result<()> {
|
|
|
if std::env::var_os("RUST_LOG").is_none() {
|
|
|
std::env::set_var("RUST_LOG", "actix_web=info");
|
|
@@ -47,30 +51,18 @@ async fn main() -> std::io::Result<()> {
|
|
|
let storage = verax::storage::Cache::new(storage);
|
|
|
let ledger = verax::Ledger::new(storage.into());
|
|
|
|
|
|
- HttpServer::new(move || {
|
|
|
- let ledger = ledger.clone();
|
|
|
+ let app = Router::new()
|
|
|
+ .route("/balance/:account", get(balance::handler))
|
|
|
+ .route("/deposit", post(deposit::handler))
|
|
|
+ .route("/lock", post(lock::handler))
|
|
|
+ .route("/subscribe", post(subscribe::handler))
|
|
|
+ .route("/tx", post(tx::handler))
|
|
|
+ .route("/:id", post(update::handler))
|
|
|
+ .route("/:id", get(get::handler))
|
|
|
+ .with_state(Context { ledger: ledger });
|
|
|
|
|
|
- App::new()
|
|
|
- .wrap(Logger::default())
|
|
|
- .app_data(web::Data::new(Context { ledger: ledger }))
|
|
|
- .app_data(web::JsonConfig::default().error_handler(|err, _req| {
|
|
|
- InternalError::from_response(
|
|
|
- "",
|
|
|
- HttpResponse::BadRequest()
|
|
|
- .content_type("application/json")
|
|
|
- .body(format!(r#"{{"error":"{}"}}"#, err)),
|
|
|
- )
|
|
|
- .into()
|
|
|
- }))
|
|
|
- .service(subscribe::handler)
|
|
|
- .service(deposit::handler)
|
|
|
- .service(balance::handler)
|
|
|
- .service(lock::handler)
|
|
|
- .service(tx::handler)
|
|
|
- .service(update::handler)
|
|
|
- .service(get::handler)
|
|
|
- })
|
|
|
- .bind("127.0.0.1:8080")?
|
|
|
- .run()
|
|
|
- .await
|
|
|
+ let addr = SocketAddr::from(([127, 0, 0, 1], 8080));
|
|
|
+ let listener = tokio::net::TcpListener::bind(addr).await.unwrap();
|
|
|
+ axum::serve(listener, app).await?;
|
|
|
+ Ok(())
|
|
|
}
|