-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5b2cebf
commit da17b1e
Showing
14 changed files
with
584 additions
and
151 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,45 @@ | ||
use anyhow::Error; | ||
use tokio_util::{sync::CancellationToken, task::TaskTracker}; | ||
use tracing::{error, warn}; | ||
|
||
use crate::cli::Cli; | ||
use crate::{cli::Cli, http::server::Server}; | ||
|
||
pub const SERVICE_NAME: &str = "ic_gateway"; | ||
pub const AUTHOR_NAME: &str = "Boundary Node Team <[email protected]>"; | ||
|
||
pub async fn main(_cli: Cli) -> Result<(), Error> { | ||
pub async fn main(cli: Cli) -> Result<(), Error> { | ||
let token = CancellationToken::new(); | ||
let tracker = TaskTracker::new(); | ||
|
||
// Handle SIGTERM/SIGHUP and Ctrl+C | ||
// Cancelling a token cancels all of its clones too, except the ones from .child_token() | ||
let handler_token = token.clone(); | ||
ctrlc::set_handler(move || handler_token.cancel())?; | ||
|
||
let router = axum::Router::new().route("/", axum::routing::get(|| async { "Hello, World!" })); | ||
|
||
let http_server = Server::new( | ||
cli.http_server.http, | ||
cli.http_server.backlog, | ||
router, | ||
token.child_token(), | ||
None, | ||
); | ||
|
||
tracker.spawn(async move { | ||
match http_server.start().await { | ||
Ok(()) => {} | ||
Err(e) => { | ||
error!("Unable to start server: {e}"); | ||
} | ||
}; | ||
}); | ||
|
||
warn!("Service is running, waiting for the shutdown signal"); | ||
token.cancelled().await; | ||
warn!("Shutdown signal received, cleaning up"); | ||
tracker.close(); | ||
tracker.wait().await; | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
use std::sync::Arc; | ||
|
||
use async_trait::async_trait; | ||
use mockall::automock; | ||
|
||
use crate::{cli, core::SERVICE_NAME, http::dns::Resolver, tls::prepare_rustls_client_config}; | ||
|
||
#[automock] | ||
#[async_trait] | ||
pub trait Client: Send + Sync { | ||
async fn execute(&self, req: reqwest::Request) -> Result<reqwest::Response, reqwest::Error>; | ||
} | ||
|
||
pub struct ReqwestClient(reqwest::Client); | ||
|
||
impl ReqwestClient { | ||
pub fn new(cli: &cli::Cli) -> Result<Self, anyhow::Error> { | ||
let http = &cli.http_client; | ||
|
||
let client = reqwest::Client::builder() | ||
.use_preconfigured_tls(prepare_rustls_client_config()) | ||
.dns_resolver(Arc::new(Resolver::new(&cli.dns))) | ||
.connect_timeout(http.timeout_connect) | ||
.timeout(http.timeout) | ||
.tcp_nodelay(true) | ||
.tcp_keepalive(Some(http.tcp_keepalive)) | ||
.http2_keep_alive_interval(Some(http.http2_keepalive)) | ||
.http2_keep_alive_timeout(http.http2_keepalive_timeout) | ||
.http2_keep_alive_while_idle(true) | ||
.http2_adaptive_window(true) | ||
.user_agent(SERVICE_NAME) | ||
.redirect(reqwest::redirect::Policy::none()) | ||
.no_proxy() | ||
.build()?; | ||
|
||
Ok(Self(client)) | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl Client for ReqwestClient { | ||
async fn execute(&self, req: reqwest::Request) -> Result<reqwest::Response, reqwest::Error> { | ||
self.0.execute(req).await | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,3 @@ | ||
pub mod client; | ||
pub mod dns; | ||
|
||
use std::sync::Arc; | ||
|
||
use async_trait::async_trait; | ||
use mockall::automock; | ||
use reqwest::{Client, Error, Request, Response}; | ||
|
||
use crate::{cli, core::SERVICE_NAME, http::dns::DnsResolver, tls::prepare_rustls_client_config}; | ||
|
||
#[automock] | ||
#[async_trait] | ||
pub trait HttpClient: Send + Sync { | ||
async fn execute(&self, req: Request) -> Result<Response, Error>; | ||
} | ||
|
||
pub struct ReqwestClient(Client); | ||
|
||
impl ReqwestClient { | ||
pub fn new(cli: &cli::Cli) -> Result<Self, anyhow::Error> { | ||
let http = &cli.http_client; | ||
|
||
let client = Client::builder() | ||
.use_preconfigured_tls(prepare_rustls_client_config()) | ||
.dns_resolver(Arc::new(DnsResolver::new(&cli.dns))) | ||
.connect_timeout(http.timeout_connect) | ||
.timeout(http.timeout) | ||
.tcp_nodelay(true) | ||
.tcp_keepalive(Some(http.tcp_keepalive)) | ||
.http2_keep_alive_interval(Some(http.http2_keepalive)) | ||
.http2_keep_alive_timeout(http.http2_keepalive_timeout) | ||
.http2_keep_alive_while_idle(true) | ||
.http2_adaptive_window(true) | ||
.user_agent(SERVICE_NAME) | ||
.redirect(reqwest::redirect::Policy::none()) | ||
.no_proxy() | ||
.build()?; | ||
|
||
Ok(Self(client)) | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl HttpClient for ReqwestClient { | ||
async fn execute(&self, req: Request) -> Result<Response, Error> { | ||
self.0.execute(req).await | ||
} | ||
} | ||
pub mod server; |
Oops, something went wrong.