Skip to content

Commit

Permalink
Add pr_static(pr, path, direc) (#657)
Browse files Browse the repository at this point in the history
Co-authored-by: Barret Schloerke <schloerke@gmail.com>
Co-authored-by: Carson Sievert <cpsievert1@gmail.com>
  • Loading branch information
3 people authored Aug 21, 2020
1 parent e6f27c8 commit 33751f1
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 0 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export(pr_set_parsers)
export(pr_set_serializer)
export(pr_set_ui)
export(pr_set_ui_callback)
export(pr_static)
export(random_cookie_key)
export(register_parser)
export(register_serializer)
Expand Down
23 changes: 23 additions & 0 deletions R/pr.R
Original file line number Diff line number Diff line change
Expand Up @@ -495,3 +495,26 @@ pr_run <- function(pr,
pr$run(host = host,
port = port)
}


#' Add a static route to the `plumber` object
#'
#' @template param_pr
#' @param path The mounted path location of the static folder
#' @param direc The local folder to be served statically
#'
#' @examples
#' \dontrun{
#' pr() %>%
#' pr_static("/path", "./my_folder/location") %>%
#' pr_run()
#' }
#'
#' @export
pr_static <- function(
pr,
path,
direc
) {
pr_mount(pr, path, PlumberStatic$new(direc))
}
26 changes: 26 additions & 0 deletions man/pr_static.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pkgdown/_pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ reference:
- 'pr_head'
- 'pr_handle'
- 'pr_mount'
- 'pr_static'

- title: Router Hooks
contents:
Expand Down
9 changes: 9 additions & 0 deletions tests/testthat/test-tidy-plumber.R
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ test_that("pr_mount mounts router", {
expect_equal(length(p2$mounts), 1)
})

test_that("pr_static mounts a static router", {
p1 <- pr() %>%
pr_static("/ex", system.file("plumber", package = "plumber"))

expect_equal(length(p1$mounts), 1)
expect_equal(names(p1$mounts), "/ex/")
expect_s3_class(p1$mounts[[1]], "PlumberStatic")
})

test_that("pr_hooks registers hooks", {
p <- pr() %>%
pr_hook("preroute", function() print("Pre-route hook")) %>%
Expand Down

0 comments on commit 33751f1

Please sign in to comment.