thomasp85 / fiery

A flexible and lightweight web server
https://fiery.data-imaginist.com
Other
240 stars 12 forks source link

Can you give an example? #29

Open sunbjt opened 6 years ago

sunbjt commented 6 years ago

This package is great! Thanks very much for your excellent work. I'm confused for passing value from outside into Fiery. The example may be like this:

curl http://127.0.0.1:8080/predict?val=10

The output depends on the input val=10, I want to get(10*2):

{
  "v": 20,
}

Thank you very much for your help.

sunbjt commented 6 years ago

I got the example from https://www.data-imaginist.com/2017/introducing-reqres/. Like this:

library(fiery)
library(reqres)

app <- Fire$new()

app$on('request', function(server, request, ...) {
    response <- request$respond()
    res <- request$query$val
    message(sprintf("Input: %s", res))
    response$body <- jsonlite::toJSON(res*2, auto_unbox = TRUE, pretty = TRUE)
    response$status <- 200L
    response$type <- 'html'
    response
})

app$ignite(showcase = FALSE)

When I run http://127.0.0.1:8080/predict?val=10, the response is []. But when I run http://127.0.0.1:8080/predict?x&val=3, the response is right. It may be a bug.

platform       x86_64-w64-mingw32          
arch           x86_64                      
os             mingw32                     
system         x86_64, mingw32             
status                                     
major          3                           
minor          4.3                         
year           2017                        
month          11                          
day            30                          
svn rev        73796                       
language       R                           
version.string R version 3.4.3 (2017-11-30)
nickname       Kite-Eating Tree  
LalZzy commented 6 years ago

Hi, Sizhe. When you run http://127.0.0.1:8080/predict?val=10, the url delivered to the program is not always true. I think this is a bug. When the url contains '?', it will become '??' in your program. In your example, I simply add a line:

library(fiery)
library(reqres)

app <- Fire$new()

app$on('request', function(server, request, ...) {
    response <- request$respond()

    ##print the url it accept
    message(request$url)

    res <- request$query$val
    message(sprintf("Input: %s", res))
    response$body <- jsonlite::toJSON(res*2, auto_unbox = TRUE, pretty = TRUE)
    response$status <- 200L
    response$type <- 'html'
    response
})

app$ignite(showcase = FALSE)

When I run http://127.0.0.1:8080/predict?val=10, it shows that

message: http://127.0.0.1:8080/predict??val=3
message: 

so you can see that the input is not correct, which is the answer why you can not get 3*2 = 6