Skip to content

Commit 7237dcb

Browse files
committed
rest server concept
1 parent 78d0d40 commit 7237dcb

File tree

8 files changed

+379
-1
lines changed

8 files changed

+379
-1
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ export(request)
9191
export(send)
9292
export(send_aio)
9393
export(serial_config)
94+
export(server)
9495
export(set_promise_context)
9596
export(socket)
9697
export(stat)

NEWS.md

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

55
* Adds support for threaded dispatcher in `mirai`.
66
* Adds 'recvAio' method for `promises::as.promise()` and `promises::is.promising()` to enable 'recvAio' promises.
7+
* Adds `server()`, an HTTP REST server for evaluation of R expressions (experimental).
78

89
#### Updates
910

R/ncurl.R

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

305305
void pipe_cb_signal(nng_pipe, nng_pipe_ev, void *);
306+
void nano_printf(const int, const char *, ...);
306307

307308
SEXP rnng_advance_rng_state(void);
308309
SEXP rnng_aio_call(SEXP);
@@ -356,6 +357,7 @@ SEXP rnng_reap(SEXP);
356357
SEXP rnng_recv(SEXP, SEXP, SEXP, SEXP);
357358
SEXP rnng_recv_aio(SEXP, SEXP, SEXP, SEXP, SEXP, SEXP);
358359
SEXP rnng_request(SEXP, SEXP, SEXP, SEXP, SEXP, SEXP, SEXP);
360+
SEXP rnng_rest_server(SEXP);
359361
SEXP rnng_send(SEXP, SEXP, SEXP, SEXP);
360362
SEXP rnng_send_aio(SEXP, SEXP, SEXP, SEXP, SEXP);
361363
SEXP rnng_serial_config(SEXP, SEXP, SEXP, SEXP);

0 commit comments

Comments
 (0)