-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: scaffolding for new API using Axum
- Test scaffolding - Dummy entrypoint
- Loading branch information
1 parent
901bc34
commit bdf26d9
Showing
10 changed files
with
279 additions
and
69 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,2 @@ | ||
pub mod server; | ||
pub mod routes; |
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,7 @@ | ||
use axum::response::Json; | ||
use serde_json::{json, Value}; | ||
|
||
#[allow(clippy::unused_async)] | ||
pub async fn root() -> Json<Value> { | ||
Json(json!({ "data": 42 })) | ||
} |
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,35 @@ | ||
use std::net::SocketAddr; | ||
use std::sync::Arc; | ||
|
||
use axum::routing::get; | ||
use axum::Router; | ||
use futures::Future; | ||
use warp::hyper; | ||
|
||
use super::routes::root; | ||
use crate::tracker; | ||
|
||
pub fn start(socket_addr: SocketAddr, _tracker: &Arc<tracker::Tracker>) -> impl Future<Output = hyper::Result<()>> { | ||
let app = Router::new().route("/", get(root)); | ||
|
||
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."); | ||
}) | ||
} | ||
|
||
pub fn start_tls( | ||
socket_addr: SocketAddr, | ||
_ssl_cert_path: &str, | ||
_ssl_key_path: &str, | ||
_tracker: &Arc<tracker::Tracker>, | ||
) -> impl Future<Output = hyper::Result<()>> { | ||
let app = Router::new().route("/", get(root)); | ||
|
||
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."); | ||
}) | ||
} |
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 |
---|---|---|
|
@@ -2,3 +2,4 @@ pub mod http_tracker; | |
pub mod torrent_cleanup; | ||
pub mod tracker_api; | ||
pub mod udp_tracker; | ||
pub mod tracker_apis; |
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,54 @@ | ||
use std::sync::Arc; | ||
|
||
use log::info; | ||
use tokio::sync::oneshot; | ||
use tokio::task::JoinHandle; | ||
|
||
use crate::apis::server; | ||
use crate::config::HttpApi; | ||
use crate::tracker; | ||
|
||
#[derive(Debug)] | ||
pub struct ApiServerJobStarted(); | ||
|
||
/// # Panics | ||
/// | ||
/// It would panic if unable to send the `ApiServerJobStarted` notice. | ||
pub async fn start_job(config: &HttpApi, tracker: Arc<tracker::Tracker>) -> JoinHandle<()> { | ||
let bind_addr = config | ||
.bind_address | ||
.parse::<std::net::SocketAddr>() | ||
.expect("Tracker API bind_address invalid."); | ||
let ssl_enabled = config.ssl_enabled; | ||
let ssl_cert_path = config.ssl_cert_path.clone(); | ||
let ssl_key_path = config.ssl_key_path.clone(); | ||
|
||
let (tx, rx) = oneshot::channel::<ApiServerJobStarted>(); | ||
|
||
// Run the API server | ||
let join_handle = tokio::spawn(async move { | ||
if !ssl_enabled { | ||
info!("Starting Torrust APIs server on: http://{}", bind_addr); | ||
let handle = server::start(bind_addr, &tracker); | ||
tx.send(ApiServerJobStarted()).expect("the start job dropped"); | ||
if let Ok(()) = handle.await { | ||
info!("Stopping Torrust APIs server on {} ...", bind_addr); | ||
} | ||
} else if ssl_enabled && ssl_cert_path.is_some() && ssl_key_path.is_some() { | ||
info!("Starting Torrust APIs server on: https://{}", bind_addr); | ||
let handle = server::start_tls(bind_addr, &ssl_cert_path.unwrap(), &ssl_key_path.unwrap(), &tracker); | ||
tx.send(ApiServerJobStarted()).expect("the start job dropped"); | ||
if let Ok(()) = handle.await { | ||
info!("Stopping Torrust APIs server on {} ...", bind_addr); | ||
} | ||
} | ||
}); | ||
|
||
// Wait until the APIs server job is running | ||
match rx.await { | ||
Ok(_msg) => info!("Torrust APIs server started"), | ||
Err(e) => panic!("the apis server was dropped: {e}"), | ||
} | ||
|
||
join_handle | ||
} |
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.