Skip to content

Commit

Permalink
Switch
Browse files Browse the repository at this point in the history
  • Loading branch information
Dzejkop committed May 31, 2024
1 parent 930933d commit 9792a8e
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 9 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
71 changes: 71 additions & 0 deletions src/new_server.rs
Original file line number Diff line number Diff line change
@@ -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<LocalAddr>,
pub server_handle: tokio::task::JoinHandle<eyre::Result<()>>,
}

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<App>) -> eyre::Result<ServerHandle> {
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,
})
}
7 changes: 4 additions & 3 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Check failure on line 6 in src/server.rs

View workflow job for this annotation

GitHub Actions / cargo test

unused import: `poem_openapi::OpenApi`
use tower_http::validate_request::ValidateRequestHeaderLayer;

use self::routes::relayer::{
Expand All @@ -20,8 +21,8 @@ mod trace_layer;

pub use self::error::ApiError;

pub async fn serve(app: Arc<App>) -> eyre::Result<()> {
let server = spawn_server(app).await?;
pub async fn _serve(app: Arc<App>) -> eyre::Result<()> {
let server = _spawn_server(app).await?;

tracing::info!("Listening on {}", server.local_addr());

Expand All @@ -30,7 +31,7 @@ pub async fn serve(app: Arc<App>) -> eyre::Result<()> {
Ok(())
}

pub async fn spawn_server(
pub async fn _spawn_server(
app: Arc<App>,
) -> eyre::Result<axum::Server<AddrIncoming, IntoMakeService<Router>>> {
let api_routes = Router::new()
Expand Down
9 changes: 3 additions & 6 deletions src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::tasks;
pub struct Service {
_app: Arc<App>,
local_addr: SocketAddr,
server_handle: JoinHandle<Result<(), hyper::Error>>,
server_handle: JoinHandle<eyre::Result<()>>,
}

impl Service {
Expand Down Expand Up @@ -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?;

Expand Down

0 comments on commit 9792a8e

Please sign in to comment.