RestRserve
router
end point as a module
#203
-
TLDR: how to create a router add an end point to it with the logic, and then add it to the I'm looking to do something like this:
So in index.R I would mount/add a single router, which in turn groups together other routes (by routes I mean end points) I'm learning My minimal example: #!/usr/bin/env Rscript
box::use(RestRserve)
app <- RestRserve$Application$new()
app$add_get("/echo", \(.req, .res) {
.res$set_body(list(paste0("The message is: '", .req$get_param_query("msg"), "'")))
.res$set_content_type("application/json")
})
backend <- RestRserve$BackendRserve$new()
backend$start(app, http_port = 8080) I was hoping in a separate file called box::use(RestRserve)
echo <- RestRserve$Router$new()
echo$add_get("/echo", \(.req, .res) {
.res$set_body(list(paste0("The message is: '", .req$get_param_query("msg"), "'")))
.res$set_content_type("application/json")
})
#' @export
echo <- echo
If I can do this then I would be able to import into my main.R and use it as such I hope: box::use(RestRserve)
app <- RestRserve$Application$new()
box::use(./echo[ echo ])
app$mount("/", echo)
backend <- RestRserve$BackendRserve$new()
backend$start(app, http_port = 8080) I know this can be done using |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
I was able to make something work, but I feel this is a hack. I would appreciate input from the developers of I would like to see the ability to create a main.R #!/usr/bin/env Rscript
box::use(RestRserve)
app <- RestRserve$Application$new()
box::use(./user_routes/index[ user_routes ])
user_routes(app)
backend <- RestRserve$BackendRserve$new()
backend$start(app, http_port = 8080) user_routes/index.R box::use(RestRserve)
box::use(./user_route_1[ user_route_1 ])
box::use(./user_route_2[ user_route_2 ])
#' @export
user_routes <- function(app) {
user_route_1(app)
user_route_2(app)
invisible(app)
} user_routes/user_route_1.R box::use(RestRserve)
#' @export
user_route_1 <- function(app) {
app$add_get("/user_route_1", function(.req, .res) {
.res$set_body("This is user route 1.")
.res$set_content_type("text/plain")
})
} user_routes/user_route_2.R box::use(RestRserve)
#' @export
user_route_2 <- function(app) {
app$add_get("/user_route_2", function(.req, .res) {
.res$set_body("This is user route 2.")
.res$set_content_type("text/plain")
})
} |
Beta Was this translation helpful? Give feedback.
I was able to make something work, but I feel this is a hack. I would appreciate input from the developers of
RestRserve
for an official way of creating these modules, @dselivanov.I would like to see the ability to create a
Router
and import that intoApplication
. I will continue reading the source code to see if I can find any better way.main.R
user_routes/index.R