analythium / openfaas-rstats-templates

OpenFaaS templates for R
MIT License
19 stars 1 forks source link

Add FaaS template based on httpuv #26

Closed psolymos closed 3 years ago

psolymos commented 3 years ago

Should modify plumber based template.

# index.R

#!/usr/bin/env Rscript

suppressMessages(library(httpuv))

source("function/handler.R")

# curl http://localhost:5000 -H "Content-Type: application/json" -d '["Friend"]'

httpuv::runServer(
  host = "0.0.0.0",
  port = 5000,
  app = list(
    call = function(req) {
      list(
        status = 200L,
        headers = list(
          'Content-Type' = 'application/json'
        ),
        body = handle(req)
      )
    }
  )
)
# handler.R

handle <- function(req) {
  input <- req[["rook.input"]]
  postdata <- input$read_lines()
  jsonlite::toJSON(
    paste0("Hello ", jsonlite::fromJSON(
      paste(postdata)), "!"))
}

DESCRIPTION to list jsonlite (not a dependency for httpuv)

psolymos commented 3 years ago

PR #27 closed this