if (FALSE) {
## Set secret key using `keyring` (preferred method)
keyring::key_set_with_value("plumber_api", password = plumber::random_cookie_key())
pr() %>%
pr_cookie(
keyring::key_get("plumber_api"),
name = "counter"
) %>%
pr_get("/sessionCounter", function(req) {
count <- 0
if (!is.null(req$session$counter)){
count <- as.numeric(req$session$counter)
}
req$session$counter <- count + 1
return(paste0("This is visit #", count))
}) %>%
pr_run()
#### -------------------------------- ###
## Save key to a local file
pswd_file <- "normal_file.txt"
cat(plumber::random_cookie_key(), file = pswd_file)
# Make file read-only
Sys.chmod(pswd_file, mode = "0600")
pr() %>%
pr_cookie(
readLines(pswd_file, warn = FALSE),
name = "counter"
) %>%
pr_get("/sessionCounter", function(req) {
count <- 0
if (!is.null(req$session$counter)){
count <- as.numeric(req$session$counter)
}
req$session$counter <- count + 1
return(paste0("This is visit #", count))
}) %>%
pr_run()
}
Run the code above in your browser using DataLab