Skip to content

Commit b39b6d3

Browse files
committed
rest server concept
1 parent 65672dd commit b39b6d3

File tree

8 files changed

+378
-1
lines changed

8 files changed

+378
-1
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ export(request)
8989
export(send)
9090
export(send_aio)
9191
export(serial_config)
92+
export(server)
9293
export(socket)
9394
export(stat)
9495
export(status_code)

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#### New Features
44

5+
* Adds `server()`, an HTTP REST server for evaluation of R expressions (experimental).
56
* Adds 'recvAio' method for `promises::as.promise()` and `promises::is.promising()` to enable 'recvAio' promises.
67

78
#### Updates

R/ncurl.R

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,3 +272,46 @@ as.promise.ncurlAio <- function(x) {
272272
#' @exportS3Method promises::is.promising
273273
#'
274274
is.promising.ncurlAio <- function(x) TRUE
275+
276+
#' Start REST Server
277+
#'
278+
#' Creates an instance of an HTTP REST server which evaluates R expressions sent
279+
#' to it [EXPERIMENTAL]. As arbitrary R expressions are evaluated, this
280+
#' should only be deployed on the local machine (using the 127.0.0.1
281+
#' loopback address) in a trusted environment.
282+
#'
283+
#' @param url full http address including hostname, port and path at which to
284+
#' host the server.
285+
#'
286+
#' @details Query the API with an HTTP client using the \sQuote{POST} method,
287+
#' with the request data being the R expression as a text string. The
288+
#' received response body will consist of the serialized evaluation result.
289+
#' Unserialize to return an R object.
290+
#'
291+
#' Use only in a new session. Use \sQuote{ctrl + \\} to forcibly quit
292+
#' when finished as the function blocks with no means of interruption.
293+
#'
294+
#' Currently still experimental as the server lacks error handling. Sending
295+
#' an invalid R expression will cause the server to exit.
296+
#'
297+
#' @return This function never returns.
298+
#'
299+
#' @examples
300+
#' if (interactive()) {
301+
#'
302+
#' # run in a new session:
303+
#' # nanonext::server()
304+
#'
305+
#' res <- ncurl("http://127.0.0.1:5555/api/rest",
306+
#' convert = FALSE,
307+
#' method = "POST",
308+
#' data = "Sys.time()")
309+
#'
310+
#' if (!is_error_value(res$data)) unserialize(res$data)
311+
#'
312+
#' }
313+
#'
314+
#' @export
315+
#'
316+
server <- function(url = "http://127.0.0.1:5555/api/rest")
317+
.Call(rnng_rest_server, url)

man/server.Rd

Lines changed: 49 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/init.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ static const R_CallMethodDef callMethods[] = {
165165
{"rnng_recv", (DL_FUNC) &rnng_recv, 4},
166166
{"rnng_recv_aio", (DL_FUNC) &rnng_recv_aio, 6},
167167
{"rnng_request", (DL_FUNC) &rnng_request, 7},
168+
{"rnng_rest_server", (DL_FUNC) &rnng_rest_server, 1},
168169
{"rnng_send", (DL_FUNC) &rnng_send, 4},
169170
{"rnng_send_aio", (DL_FUNC) &rnng_send_aio, 5},
170171
{"rnng_serial_config", (DL_FUNC) &rnng_serial_config, 4},

src/nanonext.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ int nano_matchargs(const SEXP);
245245
SEXP nano_PreserveObject(const SEXP);
246246
void nano_ReleaseObject(SEXP);
247247
void raio_invoke_cb(void *);
248+
void nano_printf(int, const char *, ...);
248249

249250
SEXP rnng_advance_rng_state(void);
250251
SEXP rnng_aio_call(SEXP);
@@ -296,6 +297,7 @@ SEXP rnng_reap(SEXP);
296297
SEXP rnng_recv(SEXP, SEXP, SEXP, SEXP);
297298
SEXP rnng_recv_aio(SEXP, SEXP, SEXP, SEXP, SEXP, SEXP);
298299
SEXP rnng_request(SEXP, SEXP, SEXP, SEXP, SEXP, SEXP, SEXP);
300+
SEXP rnng_rest_server(SEXP);
299301
SEXP rnng_send(SEXP, SEXP, SEXP, SEXP);
300302
SEXP rnng_send_aio(SEXP, SEXP, SEXP, SEXP, SEXP);
301303
SEXP rnng_serial_config(SEXP, SEXP, SEXP, SEXP);

0 commit comments

Comments
 (0)