forked from torrust/torrust-tracker
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(http): [torrust#160] scaffolding for HTTP tracker using Axum
We are going to migrate the HTTP tracker from Warp to Axum. This is the basic scaffolding for Axum. Tests have been duplicated to test the new Axum implementation. The setup allows executing both versions: the Warp version on production and both versions (Warp and Axum) on testing env.
- Loading branch information
1 parent
da6f1a7
commit 0dc3050
Showing
12 changed files
with
1,507 additions
and
67 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
use axum::response::Json; | ||
|
||
use super::resources::ok::Ok; | ||
use super::responses::ok_response; | ||
|
||
#[allow(clippy::unused_async)] | ||
pub async fn get_status_handler() -> Json<Ok> { | ||
ok_response() | ||
} |
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,5 @@ | ||
pub mod handlers; | ||
pub mod resources; | ||
pub mod responses; | ||
pub mod routes; | ||
pub mod server; |
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 @@ | ||
pub mod 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,4 @@ | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)] | ||
pub struct 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,10 @@ | ||
// Resource responses | ||
|
||
use axum::Json; | ||
|
||
use super::resources::ok::Ok; | ||
|
||
#[must_use] | ||
pub fn ok_response() -> Json<Ok> { | ||
Json(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,13 @@ | ||
use std::sync::Arc; | ||
|
||
use axum::routing::get; | ||
use axum::Router; | ||
|
||
use super::handlers::get_status_handler; | ||
use crate::tracker::Tracker; | ||
|
||
pub fn router(_tracker: &Arc<Tracker>) -> Router { | ||
Router::new() | ||
// Status | ||
.route("/status", get(get_status_handler)) | ||
} |
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,43 @@ | ||
use std::net::SocketAddr; | ||
use std::sync::Arc; | ||
|
||
use axum_server::tls_rustls::RustlsConfig; | ||
use axum_server::Handle; | ||
use futures::Future; | ||
use log::info; | ||
use warp::hyper; | ||
|
||
use super::routes::router; | ||
use crate::tracker::Tracker; | ||
|
||
pub fn start(socket_addr: SocketAddr, tracker: &Arc<Tracker>) -> impl Future<Output = hyper::Result<()>> { | ||
let app = router(tracker); | ||
|
||
let server = axum::Server::bind(&socket_addr).serve(app.into_make_service()); | ||
|
||
server.with_graceful_shutdown(async move { | ||
tokio::signal::ctrl_c().await.expect("Failed to listen to shutdown signal."); | ||
info!("Stopping Torrust HTTP tracker server on http://{} ...", socket_addr); | ||
}) | ||
} | ||
|
||
pub fn start_tls( | ||
socket_addr: SocketAddr, | ||
ssl_config: RustlsConfig, | ||
tracker: &Arc<Tracker>, | ||
) -> impl Future<Output = Result<(), std::io::Error>> { | ||
let app = router(tracker); | ||
|
||
let handle = Handle::new(); | ||
let shutdown_handle = handle.clone(); | ||
|
||
tokio::spawn(async move { | ||
tokio::signal::ctrl_c().await.expect("Failed to listen to shutdown signal."); | ||
info!("Stopping Torrust HTTP tracker server on https://{} ...", socket_addr); | ||
shutdown_handle.shutdown(); | ||
}); | ||
|
||
axum_server::bind_rustls(socket_addr, ssl_config) | ||
.handle(handle) | ||
.serve(app.into_make_service()) | ||
} |
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
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
Oops, something went wrong.