diff --git a/NAMESPACE b/NAMESPACE index 47934588..cbf2de8e 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,4 +1,4 @@ -# Generated by roxygen2 (4.0.2): do not edit by hand +# Generated by roxygen2 (4.1.0): do not edit by hand export(decodeURI) export(decodeURIComponent) @@ -6,6 +6,7 @@ export(encodeURI) export(encodeURIComponent) export(interrupt) export(rawToBase64) +export(runPipeServer) export(runServer) export(service) export(startDaemonizedServer) diff --git a/R/httpuv.R b/R/httpuv.R index dcc1eeaf..9b4db5b2 100644 --- a/R/httpuv.R +++ b/R/httpuv.R @@ -460,6 +460,44 @@ runServer <- function(host, port, app, } } +#' Run a server on a unix socket +#' +#' This is a convenience function that provides a simple way to call +#' \code{\link{startPipeServer}}, \code{\link{service}}, and +#' \code{\link{stopServer}} in the correct sequence. It does not return unless +#' interrupted or an error occurs. +#' +#' If you have multiple hosts and/or ports to listen on, call the individual +#' functions instead of \code{runPipeServer}. +#' +#' @param name A string that indicates the path for the domain socket (on +#' Unix-like systems) or the name of the named pipe (on Windows). +#' @param mask If non-\code{NULL} and non-negative, this numeric value is used +#' to temporarily modify the process's umask while the domain socket is being +#' created. To ensure that only root can access the domain socket, use +#' \code{strtoi("777", 8)}; or to allow owner and group read/write access, use +#' \code{strtoi("117", 8)}. If the value is \code{NULL} then the process's +#' umask is left unchanged. (This parameter has no effect on Windows.) +#' @param app A collection of functions that define your application. See +#' \code{\link{startServer}}. +#' @param interruptIntervalMs How often to check for interrupt. The default +#' should be appropriate for most situations. +#' +#' @seealso \code{\link{startPipeServer}}, \code{\link{service}}, +#' \code{\link{stopServer}} +#' @export +runPipeServer <- function(name, mask, app, + interruptIntervalMs = ifelse(interactive(), 100, 1000)) { + server <- startPipeServer(name, mask, app) + on.exit(stopServer(server)) + + .globals$stopped <- FALSE + while (!.globals$stopped) { + service(interruptIntervalMs) + Sys.sleep(0.001) + } +} + #' Interrupt httpuv runloop #' #' Interrupts the currently running httpuv runloop, meaning diff --git a/demo/socket-hello.R b/demo/socket-hello.R new file mode 100755 index 00000000..32981e45 --- /dev/null +++ b/demo/socket-hello.R @@ -0,0 +1,33 @@ +#!/usr/bin/env Rscript + +library(httpuv) + +app <- list( + call = function(req) { + list( + status = 200L, + headers = list( + 'Content-Type' = 'text/html' + ), + body = paste( + sep = "\r\n", + "", + "", + "", + '', + "", + "", + '

Hello World

', + "", + "" + ) + ) + } +) + +runPipeServer("/tmp/httpuv-hello-demo", strtoi("022", 8), app, 250) +# echo -e "GET / HTTP/1.1\r\n" | socat unix-connect:/tmp/httpuv-hello-demo STDIO + diff --git a/man/runPipeServer.Rd b/man/runPipeServer.Rd new file mode 100644 index 00000000..0029cecf --- /dev/null +++ b/man/runPipeServer.Rd @@ -0,0 +1,41 @@ +% Generated by roxygen2 (4.1.0): do not edit by hand +% Please edit documentation in R/httpuv.R +\name{runPipeServer} +\alias{runPipeServer} +\title{Run a server on a unix socket} +\usage{ +runPipeServer(name, mask, app, interruptIntervalMs = ifelse(interactive(), + 100, 1000)) +} +\arguments{ +\item{name}{A string that indicates the path for the domain socket (on +Unix-like systems) or the name of the named pipe (on Windows).} + +\item{mask}{If non-\code{NULL} and non-negative, this numeric value is used +to temporarily modify the process's umask while the domain socket is being +created. To ensure that only root can access the domain socket, use +\code{strtoi("777", 8)}; or to allow owner and group read/write access, use +\code{strtoi("117", 8)}. If the value is \code{NULL} then the process's +umask is left unchanged. (This parameter has no effect on Windows.)} + +\item{app}{A collection of functions that define your application. See +\code{\link{startServer}}.} + +\item{interruptIntervalMs}{How often to check for interrupt. The default + should be appropriate for most situations.} +} +\description{ +This is a convenience function that provides a simple way to call +\code{\link{startPipeServer}}, \code{\link{service}}, and +\code{\link{stopServer}} in the correct sequence. It does not return unless +interrupted or an error occurs. +} +\details{ +If you have multiple hosts and/or ports to listen on, call the individual +functions instead of \code{runPipeServer}. +} +\seealso{ +\code{\link{startPipeServer}}, \code{\link{service}}, + \code{\link{stopServer}} +} +