2

I've checked GitHub Repo and doc but still couldn't figure out how to get client IP in Plumber.

Here is the implementation I tried, I want to add IP addresses for all requests into the log file,

#' @post /v1/rl
rl_v1 <- function(a, b, c){
  request='rl'
  start_time <- as.numeric(as.POSIXct(Sys.time()))
  log_record <- paste(NULL, Sys.time(), request, "requested", NULL, NULL, 
                  sep=",")
  cat(paste(log_record, "\n", sep=""), file=log_file_name, append=T)

  lhs <- data.frame(a=unlist(a),
                b=unlist(b),
                c=unlist(c))

  pairs <- custom_function(lhs, rhs, m_w = 0.98,
                                ext_blk_field=c(12), international=T,
                                fasterWcoBlock=T, preprocessedData2=T)
  input_records=nrow(lhs)
  matches=nrow(pairs)
  query_time <- as.numeric(as.POSIXct(Sys.time())) - start_time
  status <- data.frame(query_time=query_time, 
                   request=request, 
                   type='POST',
                   api_version=api_version_v1)

  log_record <- paste(NULL, Sys.time(), request, "responded", 
                  round(matches/input_records*100, 2), 
                  paste0(matches, '/', input_records, ' in ', query_time), 
                  sep=",")
  cat(paste(log_record, "\n", sep=""), file=log_file_name, append=T)

  return(list(data=pairs, status=status))
}

Any help is highly appreciated.

Dogan Askan
  • 1,160
  • 9
  • 22
  • What exactly are you talking about here? Where are you trying to get an IP? Please try to prepare a minimal [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) that shows some code that you have and describes what you need. – MrFlick Apr 07 '17 at 15:19
  • 2
    Since `plumber` uses [`httpuv`](https://github.com/rstudio/httpuv), it's possible you can reach the [`req$REMOTE_ADDR`](https://github.com/rstudio/httpuv/search?utf8=%E2%9C%93&q=remote_addr&type=) property of the request handle. – r2evans Apr 07 '17 at 15:53
  • 1
    Thanks @r2evans that worked... – Dogan Askan Apr 07 '17 at 16:47

1 Answers1

6

To close out the question, I'll restate the comment:

Since plumber uses httpuv, it's possible you can reach the req$REMOTE_ADDR property of the request handle.

r2evans
  • 141,215
  • 6
  • 77
  • 149