[go: up one dir, main page]

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

Allow a form of /page/:id: à la express #3

Open
ColinFay opened this issue Nov 20, 2020 · 4 comments
Open

Allow a form of /page/:id: à la express #3

ColinFay opened this issue Nov 20, 2020 · 4 comments

Comments

@ColinFay
Copy link
Owner
ColinFay commented Nov 20, 2020

Something that would allow to write :

ui <- function(request){
  brochure(
    page(
      href = "/profile/:id:",
      ui = tagList(
        h1("This is my first page"),
        h2(paste("Hello", {:id:}) 
      )
    )
  )
}
@ColinFay
Copy link
Owner Author

Idea:

  • Add a page as /this/{id}/
  • is turned in the name to /this/([^/])/
  • is extracted by handlers and UI and server

How does express.JS does that under the hood? I feel like I'm missing something.

Basically the idea is to define profile/{id} inside the page() function, then have profile/colin and profile/matilda both root to that page, and the id is available to the ui & server.

@abelborges
Copy link
Contributor
abelborges commented Mar 20, 2022

Hey, Colin! Great work here with this module.

On this issue, let me know what you think about the direction below.

This first part would be called once, as part of a "compilation" step:

compile_route <- function(route) {
  stopifnot(is.character(route) && length(route) == 1)

  params <- stringr::str_match_all(route, "\\{(\\w+)\\}")[[1]][,2]
  extractor <- stringr::str_replace_all(route, "\\{\\w+\\}", "([^//]+)")[[1]]

  list(
    template = route,
    parse = function(path) {
      parsed <- stringr::str_match_all(path, extractor)[[1]]
      if (!nrow(parsed)) return(NULL)
      setNames(parsed[1,-1], params)
    }
  )
}

route <- compile_route("/prefix/{arg1}/{arg2}-argsuffix/suffix")

And then in request time:

# NULL
route$parse("/blabla")
route$parse("/prefix")
route$parse("/prefix/1")
route$parse("/prefix/1/2-argsuffix")
route$parse("/prefix/1/almost/there-argsuffix/suffix")

# named vectors
route$parse("/prefix/1/2-argsuffix/suffix")
route$parse("/prefix/first/second with spaces-argsuffix/suffix")

Some timings, just for reference (8GB RAM, 2.7 GHz i7 CPU):

Unit: milliseconds
                                                             expr     min        lq
 route$parse("/prefix/first/second with spaces-argsuffix/suffix") 0.01958 0.0199325
        mean    median        uq      max neval
 0.020986522 0.0200675 0.0202895 0.117091  1000

Maybe this is missing some more validation in order to become user facing. I didn't test more weird inputs

@ColinFay
Copy link
Owner Author

Hey @abelborges,

Thanks a lot for this! Awesome.

I love the approach of returning a list with a function and all, smart.
Let me take a closer look into that and see how it could work inside brochure 🤔

@ColinFay
Copy link
Owner Author

Ok back to this.

Internally, express uses https://www.npmjs.com/package/path-to-regexp, so maybe we should have something in the same spirit, that turns path into regex, then does a grepl on the route table 🤔

ColinFay added a commit that referenced this issue Mar 22, 2023
This is a work in progress.
The goal is to allow for route handling via regex.
This will allow for more flexibility in the routing of requests.

Issue #3
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