Skip to content

Commit

Permalink
Replace poem with axum and impl block API. (#247)
Browse files Browse the repository at this point in the history
  • Loading branch information
pochingchen authored Apr 6, 2024
1 parent 8f33f3a commit b826aa2
Show file tree
Hide file tree
Showing 26 changed files with 1,494 additions and 1,765 deletions.
916 changes: 225 additions & 691 deletions docs/api_v2.md

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions explorer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,12 @@ ethereum-types = "0.14.1"
ethereum = { version = "0.15.0", default-features = false, features = ["with-serde"] }
sha3 = { version = "0.10.4", default-features = false }
hex = "0.4.3"
axum = "0.7.5"
tower-http = { version = "0.5.2", features = ["cors"] }
rustc-hex = "2.1.0"

zei = { git = "https://github.com/FindoraNetwork/zei", branch = "stable-main" }
module = { path = "../module" }
scanner = {path = "../scanner"}
url = "2.5.0"

4 changes: 2 additions & 2 deletions explorer/src/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

[postgres]
account = "postgres"
password = "123456"
password = "csq2400306"
addr = "localhost"
database = "test"
database = "postgres"

[rpc]
tendermint = "https://prod-mainnet.prod.findora.org:26657/"
Expand Down
69 changes: 27 additions & 42 deletions explorer/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
mod service;
use crate::service::api::Api;
use crate::service::v2::block::{get_block_by_hash, get_block_by_num, get_blocks};
use anyhow::Result;
use axum::http::Method;
use axum::routing::get;
use axum::Router;
use log::info;
use poem::middleware::Cors;
use poem::{listener::TcpListener, EndpointExt, Route, Server};
use poem_openapi::OpenApiService;
use scanner::rpc::TendermintRPC;
use sqlx::pool::PoolOptions;
use sqlx::{Pool, Postgres};
use std::time::Duration;
use tokio::sync::Mutex;
use sqlx::{PgPool, Pool, Postgres};
use std::sync::Arc;
use tower_http::cors::{Any, CorsLayer};

struct AppState {
pub pool: PgPool,
}

#[tokio::main]
async fn main() -> Result<()> {
Expand All @@ -25,48 +29,29 @@ async fn main() -> Result<()> {
);

let pool: Pool<Postgres> = PoolOptions::new()
.max_connections(10)
.max_connections(50)
.connect(&postgres_config)
.await
.unwrap();
info!("Connecting DB...ok");
// tendermint rpc
let tendermint_rpc_client = TendermintRPC::new(
Duration::from_secs(60),
config.rpc.tendermint.to_string().parse().unwrap(),
);
let platform_rpc_client = TendermintRPC::new(
Duration::from_secs(60),
config.rpc.platform.to_string().parse().unwrap(),
);
let platform_server_rpc_client = TendermintRPC::new(
Duration::from_secs(60),
config.rpc.platform_server.to_string().parse().unwrap(),
);

let api = Api {
platform: platform_rpc_client,
platform_server: platform_server_rpc_client,
tendermint: tendermint_rpc_client,
storage: Mutex::new(pool),
};

let server_config = format!("http://{}:{}/api", config.server.addr, config.server.port);

let api_service = OpenApiService::new(api, "explorer", "1.0").server(server_config);
let ui = api_service.swagger_ui();
let app_state = Arc::new(AppState { pool });
let cors = CorsLayer::new()
.allow_methods([Method::GET, Method::POST, Method::OPTIONS])
.allow_origin(Any)
.allow_headers(Any);
let addr = format!("{}:{}", config.server.addr, config.server.port);
let app = Router::new()
.route("/api/v2/block/number", get(get_block_by_num))
.route("/api/v2/block/hash", get(get_block_by_hash))
.route("/api/v2/blocks", get(get_blocks))
.layer(cors)
.with_state(app_state);
let listener = tokio::net::TcpListener::bind(&addr).await.unwrap();

let server_addr = format!("{}:{}", config.server.addr, config.server.port);
let cors = Cors::new();
info!("Listening at: {}", addr);
info!("Starting server...ok");
Server::new(TcpListener::bind(server_addr))
.run(
Route::new()
.nest("/api", api_service)
.nest("/", ui)
.with(cors),
)
.await?;
axum::serve(listener, app).await.unwrap();

Ok(())
}
Loading

0 comments on commit b826aa2

Please sign in to comment.