Skip to content

Commit

Permalink
feat(api): the new Axum api uses the URL prefix /api too.
Browse files Browse the repository at this point in the history
Initially we were using a ddifferent URl to avoid conflicts with the
Warp implementation but sicne we are using different ports that is not
a problem anymore. This change simplifies switching to the new Axum API,
since we only have to start using the new implementation in the
port set in the configuration (1212), instead of the temporarily created
port for the Axum implementation (1313).
  • Loading branch information
josecelano committed Jan 11, 2023
1 parent 5d9dd9d commit 504cb9e
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 116 deletions.
42 changes: 24 additions & 18 deletions src/apis/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,34 +20,37 @@ pub fn start(socket_addr: SocketAddr, tracker: &Arc<tracker::Tracker>) -> impl F
// todo: duplicate routes definition. See `start_tls` function.
let app = Router::new()
// Stats
.route("/stats", get(get_stats_handler).with_state(tracker.clone()))
.route("/api/stats", get(get_stats_handler).with_state(tracker.clone()))
// Torrents
.route("/torrent/:info_hash", get(get_torrent_handler).with_state(tracker.clone()))
.route("/torrents", get(get_torrents_handler).with_state(tracker.clone()))
.route(
"/api/torrent/:info_hash",
get(get_torrent_handler).with_state(tracker.clone()),
)
.route("/api/torrents", get(get_torrents_handler).with_state(tracker.clone()))
// Whitelisted torrents
.route(
"/whitelist/:info_hash",
"/api/whitelist/:info_hash",
post(add_torrent_to_whitelist_handler).with_state(tracker.clone()),
)
.route(
"/whitelist/:info_hash",
"/api/whitelist/:info_hash",
delete(delete_torrent_from_whitelist_handler).with_state(tracker.clone()),
)
// Whitelist command
.route(
"/whitelist/:info_hash",
"/api/whitelist/:info_hash",
get(reload_whitelist_handler).with_state(tracker.clone()),
)
// Keys
.route(
"/key/:seconds_valid_or_key",
"/api/key/:seconds_valid_or_key",
post(generate_auth_key_handler)
.with_state(tracker.clone())
.delete(delete_auth_key_handler)
.with_state(tracker.clone()),
)
// Key command
.route("/keys/reload", get(reload_keys_handler).with_state(tracker.clone()))
// Keys command
.route("/api/keys/reload", get(reload_keys_handler).with_state(tracker.clone()))
.layer(middleware::from_fn_with_state(tracker.config.clone(), auth));

let server = axum::Server::bind(&socket_addr).serve(app.into_make_service());
Expand All @@ -66,34 +69,37 @@ pub fn start_tls(
// todo: duplicate routes definition. See `start` function.
let app = Router::new()
// Stats
.route("/stats", get(get_stats_handler).with_state(tracker.clone()))
.route("/api/stats", get(get_stats_handler).with_state(tracker.clone()))
// Torrents
.route("/torrent/:info_hash", get(get_torrent_handler).with_state(tracker.clone()))
.route("/torrents", get(get_torrents_handler).with_state(tracker.clone()))
.route(
"/api/torrent/:info_hash",
get(get_torrent_handler).with_state(tracker.clone()),
)
.route("/api/torrents", get(get_torrents_handler).with_state(tracker.clone()))
// Whitelisted torrents
.route(
"/whitelist/:info_hash",
"/api/whitelist/:info_hash",
post(add_torrent_to_whitelist_handler).with_state(tracker.clone()),
)
.route(
"/whitelist/:info_hash",
"/api/whitelist/:info_hash",
delete(delete_torrent_from_whitelist_handler).with_state(tracker.clone()),
)
// Whitelist command
.route(
"/whitelist/:info_hash",
"/api/whitelist/:info_hash",
get(reload_whitelist_handler).with_state(tracker.clone()),
)
// Keys
.route(
"/key/:seconds_valid_or_key",
"/api/key/:seconds_valid_or_key",
post(generate_auth_key_handler)
.with_state(tracker.clone())
.delete(delete_auth_key_handler)
.with_state(tracker.clone()),
)
// Key command
.route("/keys/reload", get(reload_keys_handler).with_state(tracker.clone()))
// Keys command
.route("/api/keys/reload", get(reload_keys_handler).with_state(tracker.clone()))
.layer(middleware::from_fn_with_state(tracker.config.clone(), auth));

let handle = Handle::new();
Expand Down
8 changes: 2 additions & 6 deletions tests/api/client.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use reqwest::Response;

use super::connection_info::ConnectionInfo;
use super::Version;

pub struct Client {
connection_info: ConnectionInfo,
Expand Down Expand Up @@ -68,13 +67,10 @@ impl From<QueryParam> for ReqwestQueryParam {
}

impl Client {
pub fn new(connection_info: ConnectionInfo, version: &Version) -> Self {
pub fn new(connection_info: ConnectionInfo) -> Self {
Self {
connection_info,
base_path: match version {
Version::Warp => "/api/".to_string(),
Version::Axum => "/".to_string(),
},
base_path: "/api/".to_string(),
}
}

Expand Down
Loading

0 comments on commit 504cb9e

Please sign in to comment.