| 1 |
% Generated by roxygen2: do not edit by hand |
| 2 |
% Please edit documentation in R/server.R |
| 3 |
\name{http_server} |
| 4 |
\alias{http_server} |
| 5 |
\title{Create HTTP/WebSocket Server} |
| 6 |
\usage{ |
| 7 |
http_server(url, handlers = list(), tls = NULL) |
| 8 |
} |
| 9 |
\arguments{ |
| 10 |
\item{url}{URL to listen on (e.g., "http://127.0.0.1:8080").} |
| 11 |
|
| 12 |
\item{handlers}{A handler or list of handlers created with \code{\link[=handler]{handler()}}, \code{\link[=handler_ws]{handler_ws()}}, etc.} |
| 13 |
|
| 14 |
\item{tls}{TLS configuration for HTTPS/WSS, created via \code{\link[=tls_config]{tls_config()}}.} |
| 15 |
} |
| 16 |
\value{ |
| 17 |
A nanoServer object with methods: |
| 18 |
\itemize{ |
| 19 |
\item \verb{$start()} - Start accepting connections |
| 20 |
\item \verb{$close()} - Stop and release all resources |
| 21 |
\item \verb{$serve()} - Start and block, processing requests via the \pkg{later} |
| 22 |
event loop until interrupted (e.g., Ctrl+C). The server is automatically |
| 23 |
closed on exit |
| 24 |
\item \verb{$url} - The server URL |
| 25 |
} |
| 26 |
} |
| 27 |
\description{ |
| 28 |
Creates a server that can handle HTTP requests and WebSocket connections. |
| 29 |
} |
| 30 |
\details{ |
| 31 |
This function leverages NNG's shared HTTP server architecture. When both |
| 32 |
HTTP handlers and WebSocket handlers are provided, they share the same |
| 33 |
underlying server and port. WebSocket handlers automatically handle |
| 34 |
the HTTP upgrade handshake and all WebSocket framing (RFC 6455). |
| 35 |
|
| 36 |
WebSocket and streaming callbacks are executed on R's main thread via the |
| 37 |
\pkg{later} package. To process callbacks, you must run the event loop |
| 38 |
(e.g., using \code{later::run_now()} in a loop), or use \verb{$serve()} which |
| 39 |
handles this automatically. |
| 40 |
|
| 41 |
Requires the \pkg{later} package. |
| 42 |
} |
| 43 |
\examples{ |
| 44 |
\dontshow{if (interactive() && requireNamespace("later", quietly = TRUE)) withAutoprint(\{ # examplesIf} |
| 45 |
# Simple HTTP server |
| 46 |
server <- http_server( |
| 47 |
url = "http://127.0.0.1:8080", |
| 48 |
handlers = list( |
| 49 |
handler("/", function(req) { |
| 50 |
list(status = 200L, body = "Hello, World!") |
| 51 |
}), |
| 52 |
handler("/api/data", function(req) { |
| 53 |
list( |
| 54 |
status = 200L, |
| 55 |
headers = c("Content-Type" = "application/json"), |
| 56 |
body = '{"value": 42}' |
| 57 |
) |
| 58 |
}) |
| 59 |
) |
| 60 |
) |
| 61 |
server$start() |
| 62 |
# Run event loop: repeat later::run_now(Inf) |
| 63 |
server$close() |
| 64 |
|
| 65 |
# HTTP + WebSocket server |
| 66 |
server <- http_server( |
| 67 |
url = "http://127.0.0.1:8080", |
| 68 |
handlers = list( |
| 69 |
handler("/", function(req) { |
| 70 |
list(status = 200L, body = "<html>...</html>") |
| 71 |
}), |
| 72 |
handler_ws("/ws", function(ws, data) { |
| 73 |
ws$send(data) # Echo |
| 74 |
}, textframes = TRUE) |
| 75 |
) |
| 76 |
) |
| 77 |
|
| 78 |
# Multiple WebSocket endpoints |
| 79 |
server <- http_server( |
| 80 |
url = "http://127.0.0.1:8080", |
| 81 |
handlers = list( |
| 82 |
handler_ws("/echo", function(ws, data) ws$send(data)), |
| 83 |
handler_ws("/upper", function(ws, data) ws$send(toupper(data)), textframes = TRUE) |
| 84 |
) |
| 85 |
) |
| 86 |
|
| 87 |
# HTTPS server with self-signed certificate |
| 88 |
cert <- write_cert(cn = "127.0.0.1") |
| 89 |
cfg <- tls_config(server = cert$server) |
| 90 |
server <- http_server( |
| 91 |
url = "https://127.0.0.1:8443", |
| 92 |
handlers = list( |
| 93 |
handler("/", function(req) list(status = 200L, body = "Secure!")) |
| 94 |
), |
| 95 |
tls = cfg |
| 96 |
) |
| 97 |
server$start() |
| 98 |
|
| 99 |
# Send async request and run event loop |
| 100 |
aio <- ncurl_aio( |
| 101 |
"https://127.0.0.1:8443/", |
| 102 |
tls = tls_config(client = cert$client), |
| 103 |
timeout = 2000 |
| 104 |
) |
| 105 |
while (unresolved(aio)) later::run_now(0.1) |
| 106 |
|
| 107 |
aio$status |
| 108 |
aio$data |
| 109 |
|
| 110 |
server$close() |
| 111 |
\dontshow{\}) # examplesIf} |
| 112 |
} |