Skip to content

Add a new env var to make the CORS policy permissive #214

@josecelano

Description

@josecelano

If you want to run the Torrust Index for development with the backend (this repo) and the frontend you have to uncomment a line in the router:

https://github.com/torrust/torrust-index-backend/blob/develop/src/web/api/v1/routes.rs#L38

The CorsLayer::permissive() allows the frontend to make requests to the API on a different port.

/// Add all API routes to the router.
#[allow(clippy::needless_pass_by_value)]
pub fn router(app_data: Arc<AppData>) -> Router {
    // code-review: should we use plural for the resource prefix: `users`, `categories`, `tags`?
    // 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()))
        .nest("/user", user::routes::router(app_data.clone()))
        .nest("/about", about::routes::router(app_data.clone()))
        .nest("/category", category::routes::router(app_data.clone()))
        .nest("/tag", tag::routes::router_for_single_resources(app_data.clone()))
        .nest("/tags", tag::routes::router_for_multiple_resources(app_data.clone()))
        .nest("/settings", settings::routes::router(app_data.clone()))
        .nest("/torrent", torrent::routes::router_for_single_resources(app_data.clone()))
        .nest("/torrents", torrent::routes::router_for_multiple_resources(app_data.clone()))
        .nest("/proxy", proxy::routes::router(app_data.clone()));

    Router::new()
        .route("/", get(about_page_handler).with_state(app_data))
        .nest(&format!("/{API_VERSION_URL_PREFIX}"), v1_api_routes)
    // For development purposes only.
    //
    //.layer(CorsLayer::permissive()) // Uncomment this line and the `use` import.
    //
    // It allows calling the API on a different port. For example
    // API: http://localhost:3000/v1
    // 

More info: https://github.com/torrust/torrust-index-frontend/blob/develop/docs/index.md#run-the-backend

@da2ce7 suggested using an env var to avoid changing the code:

TORRUST_IDX_BACK_CORS_PERMISSIVE=true cargo run

Please create a new issue on the frontend after implementing this issue to change the documentation there.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions