Skip to content

Commit

Permalink
refactor(api): [#198] Axum API, root endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
josecelano committed Jun 15, 2023
1 parent 9a277e8 commit 878bb7b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/web/api/v1/routes.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
//! Route initialization for the v1 API.
use std::sync::Arc;

use axum::routing::get;
use axum::Router;

//use tower_http::cors::CorsLayer;
use super::contexts::about;
use super::contexts::about::handlers::about_page_handler;
use super::contexts::{category, user};
use crate::common::AppData;

/// Add all API routes to the router.
#[allow(clippy::needless_pass_by_value)]
pub fn router(app_data: Arc<AppData>) -> Router {
let api_routes = Router::new()
let v1_api_routes = Router::new()
.route("/", get(about_page_handler).with_state(app_data.clone()))
.nest("/user", user::routes::router(app_data.clone()))
.nest("/about", about::routes::router(app_data.clone()))
.nest("/category", category::routes::router(app_data));
.nest("/category", category::routes::router(app_data.clone()));

Router::new().nest("/v1", api_routes)
Router::new()
.route("/", get(about_page_handler).with_state(app_data))
.nest("/v1", v1_api_routes)

// For development purposes only.
// It allows calling the API on a different port. For example
Expand Down
21 changes: 21 additions & 0 deletions tests/e2e/contexts/root/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,24 @@ async fn it_should_load_the_about_page_at_the_api_entrypoint() {
assert_text_ok(&response);
assert_response_title(&response, "About");
}

mod with_axum_implementation {
use torrust_index_backend::web::api;

use crate::common::asserts::{assert_response_title, assert_text_ok};
use crate::common::client::Client;
use crate::e2e::environment::TestEnv;

#[tokio::test]
async fn it_should_load_the_about_page_at_the_api_entrypoint() {
let mut env = TestEnv::new();
env.start(api::Implementation::Axum).await;

let client = Client::unauthenticated(&env.server_socket_addr().unwrap());

let response = client.root().await;

assert_text_ok(&response);
assert_response_title(&response, "About");
}
}

0 comments on commit 878bb7b

Please sign in to comment.