Skip to content

Instantly share code, notes, and snippets.

@fclesio

fclesio/endpoint.r

Created Apr 30, 2020
Embed
What would you like to do?
endpoint-rest-api-plumber
library(plumber)
library('logger')
ROOT_DIR <- getwd()
PROJECT_DIR <-
'r-api-data-hackers'
API_DIR <- 'api'
LOGS_DIR <- 'logs'
api_path_object <-
file.path(ROOT_DIR,
PROJECT_DIR,
API_DIR,
"api.R")
logging_file_path <-
file.path(ROOT_DIR,
PROJECT_DIR,
LOGS_DIR,
"automl_predictions.log")
log_appender(appender_file(logging_file_path))
log_layout(layout_glue_colors)
log_threshold(DEBUG)
convert_empty <- function(string) {
if (string == "") {
"-"
} else {
string
}
}
r <- plumb(api_path_object)
r$registerHooks(
list(
preroute = function() {
# Start timer for log info
tictoc::tic()
},
postroute = function(req, res) {
end <- tictoc::toc(quiet = TRUE)
log_info('REMOTE_ADDR: {convert_empty(req$REMOTE_ADDR)}, HTTP_USER_AGENT: "{convert_empty(req$HTTP_USER_AGENT)}", HTTP_HOST: {convert_empty(req$HTTP_HOST)}, REQUEST_METHOD: {convert_empty(req$REQUEST_METHOD)}, PATH_INFO: {convert_empty(req$PATH_INFO)}, request_status: {convert_empty(res$status)}, RESPONSE_TIME: {round(end$toc - end$tic, digits = getOption("digits", 5))}')
}
)
)
r
r$run(host="127.0.0.1", port=8000, swagger=TRUE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.