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

wip: new route matching #53

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
^dev$
^\.here$
^README\.html$
^\.vscode$
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
.RData
inst/cache/
README.html
.vscode/
50 changes: 50 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "R-Debugger",
"name": "Launch R-Workspace",
"request": "launch",
"debugMode": "workspace",
"workingDirectory": "${workspaceFolder}"
},
{
"type": "R-Debugger",
"name": "Debug R-File",
"request": "launch",
"debugMode": "file",
"workingDirectory": "${workspaceFolder}",
"file": "${file}"
},
{
"type": "R-Debugger",
"name": "Debug R-Function",
"request": "launch",
"debugMode": "function",
"workingDirectory": "${workspaceFolder}",
"file": "${file}",
"mainFunction": "main",
"allowGlobalDebugging": false
},
{
"type": "R-Debugger",
"name": "Debug R-Package",
"request": "launch",
"debugMode": "workspace",
"workingDirectory": "${workspaceFolder}",
"includePackageScopes": true,
"loadPackages": [
"."
]
},
{
"type": "R-Debugger",
"request": "attach",
"name": "Attach to R process",
"splitOverwrittenOutput": true
}
]
}
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ License: MIT + file LICENSE
URL: https://github.com/colinfay/brochure
Imports:
attempt (>= 0.3.1),
R6,
rlang (>= 0.4.12),
shiny (>= 1.6.0)
Suggests:
Expand Down
5 changes: 5 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# Generated by roxygen2: do not edit by hand

export(Route)
export(brochureApp)
export(get_cookies)
export(get_params)
export(golem_hook)
export(new_page)
export(page)
Expand All @@ -10,7 +12,10 @@ export(redirect)
export(remove_cookie)
export(server_redirect)
export(set_cookie)
export(set_params)
importFrom(R6,R6Class)
importFrom(rlang,as_function)
importFrom(shiny,httpResponse)
importFrom(shiny,shinyApp)
importFrom(shiny,tagList)
importFrom(stats,setNames)
10 changes: 2 additions & 8 deletions R/brochure-fns.R
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,8 @@ brochure <- function(
stop("You must specify a root page (one with `href = '/')`.")
}

# Saving all the UIs
x <- lapply(
pages,
function(x, extra = extra) {
...multipage[[x$href]]$ui <- x$ui
...multipage[[x$href]]$server <- x$server
}
)
# Saving all the pages
...multipage$pages <- build_routes(pages)
}

#' A Brochure Page
Expand Down
68 changes: 44 additions & 24 deletions R/brochureApp.R
Original file line number Diff line number Diff line change
Expand Up @@ -60,43 +60,52 @@ brochureApp <- function(
# Extract the correct UI, wrap it
# and add the redirect from brochure
# REGEX for path should be handled here

ui <- ...multipage[[
rm_backslash(request$PATH_INFO)
]]$ui
ui <- match_route(
request$PATH_INFO,
...multipage$pages
)$ui

if (is.function(ui)) {
ui <- ui(request)
}

...multipage_opts$wrapped(
tagList(
shiny::includeScript(
system.file(
"redirect.js",
package = "brochure"
)
),
...multipage_opts$extra,
ui
shiny::tags$head(
tagList(
shiny::includeScript(
system.file(
"redirect.js",
package = "brochure"
)
),
...multipage_opts$extra,
ui
)
)

)
},
server = function(input, output, session) {
# Same logic as the UI, we look for the correct
# server function
# REGEX for path should be handled here

path <- rm_backslash(
gsub(
"websocket/",
"",
session$request$PATH_INFO
)
)
...multipage[[
path
]]$server(input, output, session)
brochure_server <- match_route(
path,
...multipage$pages
)

set_params(
brochure_server$extract(),
session = session
)

brochure_server$server(input, output, session)
},
onStart = onStart,
options = options,
Expand All @@ -107,23 +116,25 @@ brochureApp <- function(
# We're keeping the old `httpHandler`
old_httpHandler <- res$httpHandler

# This is where the magic happens
# We're overwriting the `httpHandler` of the shinyApp
# to manage multiple pages

res$httpHandler <- function(req) {
# Handling the app level req_handlers
app_req_handlers <- get_req_handlers_app()


if (length(app_req_handlers)) {
for (i in app_req_handlers) {
req <- i(req)
for (app_req_handler in app_req_handlers) {
req <- app_req_handler(req)
# If any req_handlers return an 'httpResponse', return it directly without doing
# anything else.
if ("httpResponse" %in% class(req)) {
return(req)
}
}
}
# REGEX for path should be handled here

req$PATH_INFO <- rm_backslash(req$PATH_INFO)

# Handle redirect
if (req$PATH_INFO %in% ...multipage_opts$redirect$from) {
Expand All @@ -133,7 +144,15 @@ brochureApp <- function(
}

# Returning a 404 if the page doesn't exist
if (!req$PATH_INFO %in% names(...multipage)) {

if (
isFALSE(
match_route(
req$PATH_INFO,
...multipage$pages
)
)
) {
return(make_404(content_404))
}

Expand Down Expand Up @@ -168,5 +187,6 @@ brochureApp <- function(
}
return(res)
}

return(res)
}
53 changes: 53 additions & 0 deletions R/build_routes.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# WARNING - Generated by {fusen} from /dev/flat_handler.Rmd: do not edit by hand

#' Build routes from a list
#'
#' @return A list of routes
#'
#' @noRd
#' @importFrom stats setNames
#' @examples
#' routes <- build_routes(
#' list(
#' list(
#' href = "/users/",
#' ui = list("ui"),
#' server = function(input, output, session) {
#' print("server")
#' }
#' ),
#' list(
#' href = "/users/:id/posts/:postid",
#' ui = list("ui"),
#' server = function(input, output, session) {
#' print("server")
#' }
#' ),
#' list(
#' href = "/users/posts/.*",
#' ui = list("ui"),
#' server = function(input, output, session) {
#' print("server")
#' }
#' )
#' )
#' )
build_routes <- function(routes){
built_routes <- lapply(routes, function(route){
Route$new(
href = route$href,
ui = route$ui,
server = route$server
)
})
setNames(
built_routes,
vapply(
built_routes,
function(route){
route$href
},
character(1)
)
)
}
23 changes: 23 additions & 0 deletions R/match_route.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# WARNING - Generated by {fusen} from /dev/flat_handler.Rmd: do not edit by hand

#' Match a route
#'
#' @return The matched route or FALSE
#'
#' @noRd
#' @examples
#' match_route(
#' "/users/aaa/posts/aaa",
#' routes
#' )
match_route <- function(
href,
routes
) {
for (route in routes) {
if (route$matches(href)) {
return(route)
}
}
return(FALSE)
}
36 changes: 36 additions & 0 deletions R/params.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# WARNING - Generated by {fusen} from /dev/flat_handler.Rmd: do not edit by hand

#' Manipulate the path params
#'
#' Set or get the path params
#'
#' @param object The object to set the params on
#' @param session The shiny session to set the params on or get from
#'
#' @return The param list as a named character vector, or NULL for set
#' @rdname params
#' @export
#' @examples
#' session <- shiny::MockShinySession$new()
#' set_params(
#' list(params = c(id = "aaa", postid = "aaa")),
#' session = session
#' )
#' get_params(
#' session
#' )
set_params <- function(
object,
session = shiny::getDefaultReactiveDomain()
){
session$userData$brochure_params <- object$params
NULL
}

#' @rdname params
#' @export
get_params <- function(
session = shiny::getDefaultReactiveDomain()
){
session$userData$brochure_params
}
Loading