Skip to content

Commit

Permalink
fix: [#457] fix API about pages with subfolder
Browse files Browse the repository at this point in the history
When the API is istalled using a subfolder links don't work.

For exmaple:

https://index.torrust-demo.com/api/

License links goes to:

https://index.torrust-demo.com/v1/about/license

instead of:

https://index.torrust-demo.com/api/v1/about/license

This change uses relative URLs and also an explicit redirecting to the
abnout pages, intead of showing the same content for other pages like

- https://index.torrust-demo.com/api/
- https://index.torrust-demo.com/api/v1
- https://index.torrust-demo.com/api/v1/about/
  • Loading branch information
josecelano committed Feb 8, 2024
1 parent e9c0642 commit 90a2035
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
16 changes: 6 additions & 10 deletions src/services/about.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//! Templates for "about" static pages.
use crate::web::api::server::v1::routes::API_VERSION_URL_PREFIX;

#[must_use]
pub fn index_page() -> String {
Expand All @@ -8,8 +7,7 @@ pub fn index_page() -> String {

#[must_use]
pub fn page() -> String {
format!(
r#"
r#"
<html>
<head>
<title>About</title>
Expand All @@ -22,17 +20,16 @@ pub fn page() -> String {
<p>Hi! This is a running <a href="https://github.com/torrust/torrust-index">torrust-index</a>.</p>
</body>
<footer style="padding: 1.25em 0;border-top: dotted 1px;">
<a href="/{API_VERSION_URL_PREFIX}/about/license">license</a>
<a href="./about/license">license</a>
</footer>
</html>
"#
)
.to_string()
}

#[must_use]
pub fn license_page() -> String {
format!(
r#"
r#"
<html>
<head>
<title>Licensing</title>
Expand All @@ -55,9 +52,8 @@ pub fn license_page() -> String {
<p>If you want to read more about all the licenses and how they apply please refer to the <a href="https://github.com/torrust/torrust-index/blob/develop/licensing/contributor_agreement_v01.md">contributor agreement</a>.</p>
</body>
<footer style="padding: 1.25em 0;border-top: dotted 1px;">
<a href="/{API_VERSION_URL_PREFIX}/about">about</a>
<a href="../about">about</a>
</footer>
</html>
"#
)
"#.to_string()
}
2 changes: 1 addition & 1 deletion src/web/api/server/v1/contexts/about/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
//! <p>Hi! This is a running <a href="https://github.com/torrust/torrust-index">torrust-index</a>.</p>
//! </body>
//! <footer style="padding: 1.25em 0; border-top: dotted 1px;">
//! <a href="/v1/about/license">license</a>
//! <a href="v1/about/license">license</a>
//! </footer>
//! </html>
//! ```
Expand Down
10 changes: 7 additions & 3 deletions src/web/api/server/v1/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ use std::env;
use std::sync::Arc;

use axum::extract::DefaultBodyLimit;
use axum::response::Redirect;
use axum::routing::get;
use axum::{Json, Router};
use serde_json::{json, Value};
use tower_http::compression::CompressionLayer;
use tower_http::cors::CorsLayer;

use super::contexts::about::handlers::about_page_handler;
use super::contexts::{about, category, proxy, settings, tag, torrent, user};
use crate::bootstrap::config::ENV_VAR_CORS_PERMISSIVE;
use crate::common::AppData;
Expand All @@ -23,7 +23,7 @@ pub fn router(app_data: Arc<AppData>) -> Router {
// See: https://stackoverflow.com/questions/6845772/should-i-use-singular-or-plural-name-convention-for-rest-resources

let v1_api_routes = Router::new()
.route("/", get(about_page_handler).with_state(app_data.clone()))
.route("/", get(redirect_to_about))
.nest("/user", user::routes::router(app_data.clone()))
.nest("/about", about::routes::router(app_data.clone()))
.nest("/category", category::routes::router(app_data.clone()))
Expand All @@ -35,7 +35,7 @@ pub fn router(app_data: Arc<AppData>) -> Router {
.nest("/proxy", proxy::routes::router(app_data.clone()));

let router = Router::new()
.route("/", get(about_page_handler).with_state(app_data.clone()))
.route("/", get(redirect_to_about))
.route("/health_check", get(health_check_handler).with_state(app_data))
.nest(&format!("/{API_VERSION_URL_PREFIX}"), v1_api_routes);

Expand All @@ -52,3 +52,7 @@ pub fn router(app_data: Arc<AppData>) -> Router {
async fn health_check_handler() -> Json<Value> {
Json(json!({ "status": "Ok" }))
}

async fn redirect_to_about() -> Redirect {
Redirect::permanent(&format!("/{API_VERSION_URL_PREFIX}/about"))
}

0 comments on commit 90a2035

Please sign in to comment.