Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
josecelano opened this issue Jun 22, 2023 · 0 comments · Fixed by #218
Closed

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

josecelano opened this issue Jun 22, 2023 · 0 comments · Fixed by #218
Labels

Comments

@josecelano
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

1 participant