inst/template/plumber.R

#' @title
#' Application API
#'
#' @description
#' The Application API that underlies
#'
#' @export
app_api <- function() {
    pr() %>%
        pr_get(
            path = "/health",
            handler = function(req, res) {
                "Application is healthy"
            }
        )
}

#' @title
#' Run Development App
#'
#' @description
#' Runs the Plumber API, bundles the static site, and then serves it using
#' the RStudio static file server. This only works within RStudio.
run_dev <- function(..., app = dev_pkg_inst("app"), docs = FALSE) {
    stopifnot(rstudioapi::isAvailable())
    app_api()  %>%
        pr_set_docs(TRUE) %>%
        pr_set_docs_callback(
            callback = function(
                docs_url,
                api_url = get("api_url", envir = parent.frame())
            ) {
                if (!isFALSE(docs)) {
                    on.exit(browseURL(docs_url), add = TRUE)
                }
                if (dir.exists(app)) {
                    Sys.setenv("PLUMBER_BACKEND_API_URL" = rstudioapi::translateLocalUrl(
                        api_url,
                        absolute = TRUE
                    ))
                    packer::bundle_dev()
                    on.exit(browseURL(paste0(app, "/")), add = TRUE)
                }
            }
        ) %>%
        pr_run(...)
}

#' @title
#' Run Application API
#'
#' @description
#' Runs the Plumber API; designed for production use.
#'
#' @export
run_api <- function(..., docs = FALSE) {
    app_api() %>%
        pr_set_docs(docs) %>%
        pr_run(...)
}
tjpalanca/tjutils documentation built on Jan. 20, 2021, 2:01 p.m.