Skip to content

Commit

Permalink
update missing package and fix logging error
Browse files Browse the repository at this point in the history
  • Loading branch information
JfrAziz committed Apr 28, 2023
1 parent e575248 commit 83bbb8a
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ RUN Rscript -e "install.packages('pak', repos = sprintf('https://r-lib.github.io
RUN Rscript -e "pak::pkg_install('rstudio/plumber@main')"

# install required R packages
RUN Rscript -e "pak::pkg_install(c('logger','tictoc', 'fs', 'promises', 'future'))"
RUN Rscript -e "pak::pkg_install(c('logger','tictoc', 'fs', 'promises', 'future', 'fastmap'))"

# install testing packages
RUN Rscript -e "pak::pkg_install(c('testthat', 'httr'))"
Expand Down
19 changes: 14 additions & 5 deletions helpers/logging.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ log_dir <- "logs"
if (!fs::dir_exists(log_dir)) fs::dir_create(log_dir)
log_appender(appender_tee(tempfile(paste0("plumber_", Sys.time(), "_"), log_dir, ".log")))

convert_empty <- function(string) {
if (string == "") {
return("-")
}
# transoform empty value to -
convert_empty <- function(string = "") {
if (is.null(string)) return("-")
if (string == "") return("-")
return(string)
}

Expand All @@ -19,5 +19,14 @@ pre_route_logging <- function(req) {

post_route_logging <- function(req, res) {
end <- tictoc::toc(quiet = TRUE) # nolint
log_info('{convert_empty(req$REMOTE_ADDR)} "{convert_empty(req$HTTP_USER_AGENT)}" {convert_empty(req$HTTP_HOST)} {convert_empty(req$REQUEST_METHOD)} {convert_empty(end$msg)} {convert_empty(res$status)} {round(end$toc - end$tic, digits = getOption("digits", 5))}') # nolint

log_info(sprintf('%s "%s" %s %s %s %s %s',
convert_empty(req$REMOTE_ADDR),
convert_empty(req$HTTP_USER_AGENT),
convert_empty(req$HTTP_HOST),
convert_empty(req$REQUEST_METHOD),
convert_empty(end$msg),
convert_empty(res$status),
round(end$toc - end$tic, digits = getOption("digits", 5))
)) # nolint
}
1 change: 1 addition & 0 deletions helpers/parallel.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
library(future)
library(promises)

future::plan(future::multisession(workers = WORKERS))

Expand Down
10 changes: 5 additions & 5 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ This repository is a boilerplate to setup a new project with R plumber. You can
| Dynamic Filter/Miiddleware || Add custom filter / middleware each mounted route from `routes` dir |
| Request Validation || Simple validation mechanism to check incoming request from request body / params, such as required fields, check type (number, boolean, array), check the value in given array, etc. |
| Docker || Simplifying apps with docker, for better development, deployment, dependencies management, and scaling |
| Parallel Processing | Not Yet | R only run a request at a time, make it process in parallel with `promises` and `future` packages. |
| Parallel Processing | | R only run a request at a time, make it process in parallel with `promises` and `future` packages. |
| Testing || Testing for endpoints / routes and helper functions with `testthat` and `httr` packages, also use Docker and docker-compose for setting up automated testing. For running in CI / CD, an example also provided. |

This template comes with built in Environment Variables that you can edit when running it.

| ENV | Default | Description |
| -------- | :---------: | ---------------------------------------------------------------------------------------------- |
| `HOST` | `127.0.0.1` | Host to run Rplumber, use `0.0.0.0` when running it in Docker |
| `PORT` | `8000` | Which port Rplumber will run |
| ENV | Default | Description |
| --------- | :---------: | ---------------------------------------------------------------------------------------------- |
| `HOST` | `127.0.0.1` | Host to run Rplumber, use `0.0.0.0` when running it in Docker |
| `PORT` | `8000` | Which port Rplumber will run |
| `WORKERS` | `3` | Number of worker (Rsession) to run parallel processing in Rplumber (including the main worker) |

## How to use it
Expand Down

0 comments on commit 83bbb8a

Please sign in to comment.