rstudio / plumber

Turn your R code into a web API.
https://www.rplumber.io
Other
1.39k stars 256 forks source link

Swagger UI returns 404 error with pm2 #173

Closed jeffkeller87 closed 6 years ago

jeffkeller87 commented 7 years ago

I am hosting an API with pm2 on AWS but the Swagger UI page (http://hostname:port/__swagger_/) returns a 404 error. The Swagger UI shows up just fine when run locally with r$run(). Is this a limitation of pm2 hosting? Section 9.4 of the plumber documentation is unfortunately incomplete so I didn't get far very in trying to figure this out :(

trestletech commented 7 years ago

Single underscore on the end? I believe it should be /__swagger__/ (two underscores on either side)

jeffkeller87 commented 7 years ago

Unfortunately, that was just a typo here on GitHub. http://hostname:port/__swagger__/ also throws a 404 error.

jeffkeller87 commented 7 years ago

To clarify, running the contents of plumb.R interactively in RStudio Server on the AWS EC2 instance works. Here's the set up using the base example:

# myfile.R

#* @get /mean
normalMean <- function(samples=10){
  data <- rnorm(samples)
  mean(data)
}

#* @post /sum
addTwo <- function(a, b){
  as.numeric(a) + as.numeric(b)
}
# plumb.R
library(plumber)
r <- plumb("myfile.R")  # Where 'myfile.R' is the location of the file shown above
r$run(port = 8000, host = "0.0.0.0")

pm2 service (no swagger @ http://hostname:port/__swagger__/)

pm2 start --interpreter="Rscript" plumb.R
david-waterworth commented 7 years ago

Try r$run(port = 8000, host = "0.0.0.0", swagger=TRUE)

I had a similar issue with my service working in Rstudio but not in docker. I looked through the code and the default for swagger is swagger=interactive() so the swagger endpoint is only created if you explicitly request it except when running from an interactive session

Regards Dave

trestletech commented 7 years ago

Oh, great point @WaterworthD . You'll want to set swagger=TRUE if you want the swagger interface to be available in your production environment. (The motivation here is that we don't want anyone to be surprised by the fact that they just leaked all their endpoints with documentation to their clients).

jeffkeller87 commented 6 years ago

Thanks @WaterworthD, That did it! @trestletech are there plans to flesh out the swagger documentation (https://www.rplumber.io/docs/tips-tricks.html#swagger)?