From 9792a8ef901476ca82b950669a4e2765d04bb1d6 Mon Sep 17 00:00:00 2001 From: Dzejkop Date: Fri, 31 May 2024 16:31:48 +0200 Subject: [PATCH] Switch --- Cargo.lock | 7 +++++ Cargo.toml | 1 + src/lib.rs | 1 + src/new_server.rs | 71 +++++++++++++++++++++++++++++++++++++++++++++++ src/server.rs | 7 +++-- src/service.rs | 9 ++---- 6 files changed, 87 insertions(+), 9 deletions(-) create mode 100644 src/new_server.rs diff --git a/Cargo.lock b/Cargo.lock index 7672964..9072438 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5608,6 +5608,7 @@ dependencies = [ "tracing-subscriber", "url", "uuid 0.8.2", + "version", ] [[package]] @@ -5773,6 +5774,12 @@ version = "0.2.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" +[[package]] +name = "version" +version = "3.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3a449064fee414fcc201356a3e6c1510f6c8829ed28bb06b91c54ebe208ce065" + [[package]] name = "version_check" version = "0.9.4" diff --git a/Cargo.toml b/Cargo.toml index c511470..5affdfd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -84,6 +84,7 @@ tracing-subscriber = { version = "0.3", default-features = false, features = [ ] } url = "2.4.1" uuid = { version = "0.8", features = ["v4"] } +version = "3.0.0" [dev-dependencies] indoc = "2.0.3" diff --git a/src/lib.rs b/src/lib.rs index abb94b0..b3786a7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,6 +8,7 @@ pub mod db; pub mod keys; pub mod serde_utils; pub mod server; +pub mod new_server; pub mod service; pub mod shutdown; pub mod task_runner; diff --git a/src/new_server.rs b/src/new_server.rs new file mode 100644 index 0000000..cd35525 --- /dev/null +++ b/src/new_server.rs @@ -0,0 +1,71 @@ +use std::net::SocketAddr; +use std::sync::Arc; + +use poem::listener::{Acceptor, Listener, TcpListener}; +use poem::web::LocalAddr; +use poem::Route; +use poem_openapi::{OpenApi, OpenApiService}; + +use crate::app::App; + +struct AdminApi; + +#[OpenApi] +impl AdminApi {} + +struct ConsumerApi; + +#[OpenApi] +impl ConsumerApi {} + +struct ServiceApi; + +#[OpenApi] +impl ServiceApi {} + +pub struct ServerHandle { + pub local_addrs: Vec, + pub server_handle: tokio::task::JoinHandle>, +} + +impl ServerHandle { + pub fn local_addr(&self) -> SocketAddr { + self.local_addrs + .iter() + .filter_map(|addr| addr.as_socket_addr()) + .next() + .cloned() + .expect("Not bound to any address") + } +} + +pub async fn spawn_server(app: Arc) -> eyre::Result { + let api_service = OpenApiService::new( + (AdminApi, ConsumerApi, ServiceApi), + "Tx Sitter", + version::version!(), + ); + + let router = Route::new() + .nest("/explorer", api_service.rapidoc()) + .nest("/schema.json", api_service.spec_endpoint()) + .nest("/schema.yml", api_service.spec_endpoint_yaml()) + .nest("/", api_service); + + let listener = TcpListener::bind(app.config.server.host); + let acceptor = listener.into_acceptor().await?; + + let local_addrs = acceptor.local_addr(); + + let server = poem::Server::new_with_acceptor(acceptor); + + let server_handle = tokio::spawn(async move { + server.run(router).await?; + Ok(()) + }); + + Ok(ServerHandle { + local_addrs, + server_handle, + }) +} diff --git a/src/server.rs b/src/server.rs index 3a9b9ac..6b7a935 100644 --- a/src/server.rs +++ b/src/server.rs @@ -3,6 +3,7 @@ use std::sync::Arc; use axum::routing::{get, post, IntoMakeService}; use axum::Router; use hyper::server::conn::AddrIncoming; +use poem_openapi::OpenApi; use tower_http::validate_request::ValidateRequestHeaderLayer; use self::routes::relayer::{ @@ -20,8 +21,8 @@ mod trace_layer; pub use self::error::ApiError; -pub async fn serve(app: Arc) -> eyre::Result<()> { - let server = spawn_server(app).await?; +pub async fn _serve(app: Arc) -> eyre::Result<()> { + let server = _spawn_server(app).await?; tracing::info!("Listening on {}", server.local_addr()); @@ -30,7 +31,7 @@ pub async fn serve(app: Arc) -> eyre::Result<()> { Ok(()) } -pub async fn spawn_server( +pub async fn _spawn_server( app: Arc, ) -> eyre::Result>> { let api_routes = Router::new() diff --git a/src/service.rs b/src/service.rs index 22537d1..326b72d 100644 --- a/src/service.rs +++ b/src/service.rs @@ -13,7 +13,7 @@ use crate::tasks; pub struct Service { _app: Arc, local_addr: SocketAddr, - server_handle: JoinHandle>, + server_handle: JoinHandle>, } impl Service { @@ -43,12 +43,9 @@ impl Service { Self::spawn_chain_tasks(&task_runner, chain_id)?; } - let server = crate::server::spawn_server(app.clone()).await?; + let server = crate::new_server::spawn_server(app.clone()).await?; let local_addr = server.local_addr(); - let server_handle = tokio::spawn(async move { - server.await?; - Ok(()) - }); + let server_handle = server.server_handle; initialize_predefined_values(&app).await?;