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

Support mounting using pr_mount() without a trailing slash #952

Open
gacolitti opened this issue Apr 28, 2024 · 2 comments
Open

Support mounting using pr_mount() without a trailing slash #952

gacolitti opened this issue Apr 28, 2024 · 2 comments

Comments

@gacolitti
Copy link

I want to be able to mount routers without a trailing slash. As far as I can tell, this is not supported.

Here is an example:

library(plumber)
pr1 <- pr_get(pr(), "/", function() {})

pr() |> 
  pr_mount("/api/test", pr1) |> 
  pr_run(port = 8322)

I want the endpoint to be /api/test, but instead it becomes /api/test/ and I'm not sure how to remove the trailing slash.

I tried doing:

pr1 <- pr_get(pr(), "", function() {})

but that returns the same endpoint as before.

Maybe there can be an additional argument to pr_mount() to control this behavior:

pr_mount(ignore_trailing_slashes = TRUE)
@thomasp85
Copy link
Collaborator

thomasp85 commented Jan 23, 2025

Mounting without a trailing slash is indeed possible as long as some path is mounted, e.g.

library(plumber)
pr1 <- pr_get(pr(), "/test", function() {})

pr() |> 
  pr_mount("/api", pr1) |> 
  pr_run(port = 8322)

does give you an endpoint at /api/test

So this seems more about being able to mount an empty router which I can see good reasons why shouldn't be allows (but I can also see use cases for it)

@gacolitti
Copy link
Author

I'm not sure if I'm using best practices, but I was doing something like this:

transactions <- pr("api/endpoints/transactions.R")

pr |>
    add_spec() |>
    pr_mount("/api/v1/transactions", transactions) |>

and inside transactions.R I would define the transactions-specific endpoints. But some of these endpoints should be like GET /api/v1/transactions and POST /api/v1/transactions.

So I was defining endpoints like this:

# Get transactions
pr_get(
  path = "",
  handler = function() {get_transactions(conn)},
  tags = "Transactions",
  comments = "Get transactions",
  responses = list(
    "200" = list(description = "All transactions")
  )
)

where path = "" but this produces /api/v1/transactions/ instead of what I was going for which is /api/v1/transactions.

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

No branches or pull requests

2 participants