Package 'goserveR'

Title: HTTP(S) File Server with Range Requests and Unbounded CORS Using 'go'
Description: Provides a lightweight HTTP(S) file server implemented in Go and exposed to R. It supports range requests, configurable CORS and COOP headers, and optional TLS. The package builds and runs a background server that can be cleanly shut down from R using an OS pipe and C/Go interop. Asynchronous log handling is also supported.
Authors: Sounkou Mahamane Toure [aut, cre]
Maintainer: Sounkou Mahamane Toure <[email protected]>
License: GPL (>= 3)
Version: 0.1.3-0.9000
Built: 2026-10-16 10:18:03 UTC
Source: https://github.com/sounkou-bioinfo/goServeR

Help Index


Create default console log handler

Description

Create default console log handler

Usage

.create_default_log_handler(fd)

Arguments

fd

file descriptor for log pipe

Value

external pointer to log handler


Default log callback - writes to console

Description

Default log callback - writes to console

Usage

.default_log_callback(handler, message, user)

Arguments

handler

external pointer to handler

message

log message

user

user data (unused)


Add Authentication Key

Description

Add an API key to the authentication system

Usage

addAuthKey(server_handle, key)

Arguments

server_handle

External pointer from runServer(blocking=FALSE, auth=TRUE)

key

Character string, the API key to add

Value

Invisible TRUE


Convert server list to data frame

Description

Convert server list to data frame

Usage

## S3 method for class 'server_list'
as.data.frame(x, ...)

Arguments

x

a server_list object

...

additional arguments (ignored)

Value

a data.frame with server information


Clear All Authentication Keys

Description

Remove all API keys from the authentication system

Usage

clearAuthKeys(server_handle)

Arguments

server_handle

External pointer from runServer(blocking=FALSE, auth=TRUE)

Value

Invisible TRUE


Create file log handler

Description

Create file log handler

Usage

createFileLogHandler(fd, logfile = tempfile("goserveR_", fileext = ".log"))

Arguments

fd

file descriptor for log pipe

logfile

path to log file

Value

external pointer to log handler


Create silent log handler (no-op)

Description

Create silent log handler (no-op)

Usage

createSilentLogHandler(fd)

Arguments

fd

file descriptor for log pipe

Value

external pointer to log handler


Generate TLS credentials with 'nanonext'

Description

Generates a self-signed X.509 certificate and private key as PEM using nanonext::write_cert(), then writes them to temporary files accepted by Go's HTTPS server. The private-key file is restricted to the current user on platforms that support Unix-style file modes.

Usage

createTLSCertificate(
  cn = "127.0.0.1",
  valid = "20301231235959",
  directory = tempdir()
)

Arguments

cn

Certificate common name, usually the server host name or IP address.

valid

Certificate expiry in yyyymmddhhmmss format.

directory

Directory in which to create the temporary PEM files.

Value

A goserveR_tls_certificate object containing the in-memory PEM values and the certfile and keyfile paths. Pass the object directly to the tls argument of runServer().


isRunning Check if a background server is still running

Description

isRunning Check if a background server is still running

Usage

isRunning(handle)

Arguments

handle

external pointer returned by runServer(blocking=FALSE)

Value

logical, TRUE if server is running, FALSE otherwise

Examples

## Not run: 
h <- runServer(dir = ".", addr = "127.0.0.1:8080", blocking = FALSE)
isRunning(h) # TRUE
shutdownServer(h)
isRunning(h) # FALSE

## End(Not run)

List Authentication Keys

Description

Get all current API keys in the authentication system

Usage

listAuthKeys(server_handle)

Arguments

server_handle

External pointer from runServer(blocking=FALSE, auth=TRUE)

Value

Character vector of current API keys


listServers List all running background servers with detailed information

Description

listServers List all running background servers with detailed information

Usage

listServers()

Value

a server_list S3 object containing server information


Print method for individual server_info objects

Description

Print method for individual server_info objects

Usage

## S3 method for class 'server_info'
print(x, index = NULL, ...)

Arguments

x

a server_info object

index

server index number for display

...

additional arguments (ignored)


Print method for server_list objects

Description

Print method for server_list objects

Usage

## S3 method for class 'server_list'
print(x, ...)

Arguments

x

a server_list object

...

additional arguments (ignored)


Register a log handler for a file descriptor

Description

Register a log handler for a file descriptor

Usage

registerLogHandler(fd, callback, user = NULL)

Arguments

fd

file descriptor to monitor

callback

R function to call when data is available

user

user data passed to callback

Value

external pointer to log handler


Remove Authentication Key

Description

Remove an API key from the authentication system

Usage

removeAuthKey(server_handle, key)

Arguments

server_handle

External pointer from runServer(blocking=FALSE, auth=TRUE)

key

Character string, the API key to remove

Value

Invisible TRUE


Remove a log handler

Description

Remove a log handler

Usage

removeLogHandler(handler)

Arguments

handler

external pointer to log handler

Value

logical indicating success


Delete generated TLS credential files

Description

Delete generated TLS credential files

Usage

removeTLSCertificate(credentials)

Arguments

credentials

An object returned by createTLSCertificate().

Value

Invisibly, TRUE if neither credential file remains.


runServer

Description

Run the go http server (blocking or background)

Usage

runServer(
  dir = getwd(),
  addr = "0.0.0.0:8181",
  prefix = "",
  blocking = TRUE,
  cors = FALSE,
  coop = FALSE,
  tls = FALSE,
  certfile = "cert.pem",
  keyfile = "key.pem",
  silent = FALSE,
  log_handler = NULL,
  auth_keys = c(),
  auth = FALSE,
  initial_keys = c(),
  mustWork = FALSE,
  ...
)

Arguments

dir

character vector of directories to serve

addr

address

prefix

character vector of server prefixes (must have same length as dir)

blocking

logical, if FALSE runs in background and returns a handle

cors

logical, enable CORS headers

coop

logical, enable COOP/COEP headers

tls

Logical to enable TLS (HTTPS), or an object returned by createTLSCertificate() to enable TLS with generated PEM credentials.

certfile

path to TLS certificate file

keyfile

path to TLS key file

silent

logical, suppress server logs

log_handler

function, custom log handler function(handler, message, user)

auth_keys

character vector of API keys for authentication. Default c() = no auth

auth

logical, enable dynamic authentication system (non-blocking mode only)

initial_keys

character vector of initial API keys for dynamic auth system

mustWork

logical, if TRUE and non-blocking, will check if server actually started and throw error if it failed (default FALSE for backward compatibility)

...

additional arguments passed to the server

Value

NULL (if blocking) or an external pointer (if non-blocking)

Examples

## Not run: 
# Start a blocking server (will block the R session)
# runServer(dir = ".", addr = "0.0.0.0:8080")

# Start a background server (returns a handle)
h <- runServer(dir = ".", addr = "0.0.0.0:8080", blocking = FALSE)

# Start a server with static auth keys (backward compatible)
h <- runServer(
  dir = ".", addr = "0.0.0.0:8080", blocking = FALSE,
  auth_keys = c("secret123", "token456")
)

# Start a server with dynamic auth system
h <- runServer(
  dir = ".", addr = "0.0.0.0:8080", blocking = FALSE,
  auth = TRUE, initial_keys = c("secret123")
)

# Manage auth keys dynamically (only with auth=TRUE)
auth <- attr(h, "auth")
addAuthKey(auth, "new_key_456")
removeAuthKey(auth, "secret123")
listAuthKeys(auth)

# Start a server serving multiple directories
h <- runServer(
  dir = c("./data", "./docs", "."),
  prefix = c("/api/data", "/docs", "/files"),
  addr = "0.0.0.0:8080",
  blocking = FALSE
)

# Start a server with custom log handler
logfile <- tempfile("server_", fileext = ".log")
h <- runServer(
  dir = ".", addr = "0.0.0.0:8080", blocking = FALSE,
  log_handler = function(handler, message, user) {
    cat("[CUSTOM]", message, file = logfile, append = TRUE)
  }
)

# List all running background servers
listServers()

# Get a summary view
summary(listServers())

# Shutdown a background server
shutdownServer(h)

## End(Not run)

shutdownServer Shutdown a background server

Description

shutdownServer Shutdown a background server

Usage

shutdownServer(handle)

Arguments

handle

external pointer returned by runServer(blocking=FALSE)


StartServer (advanced/manual use) Start a server (C-level, advanced)

Description

StartServer (advanced/manual use) Start a server (C-level, advanced)

Usage

StartServer(
  dir,
  addr,
  prefix,
  blocking,
  cors = FALSE,
  coop = FALSE,
  tls = FALSE,
  certfile = "cert.pem",
  keyfile = "key.pem",
  silent = FALSE,
  log_handler = NULL,
  auth_keys = c()
)

Arguments

dir

character vector of directories to serve

addr

address

prefix

character vector of server prefixes (must have same length as dir)

blocking

logical, if FALSE runs in background and returns a handle

cors

logical, enable CORS headers

coop

logical, enable COOP/COEP headers

tls

logical, enable TLS (HTTPS)

certfile

path to TLS certificate file

keyfile

path to TLS key file

silent

logical, suppress server logs

log_handler

function, custom log handler function(handler, message, user)

auth_keys

character vector of API keys for authentication


Summary method for server_list objects

Description

Summary method for server_list objects

Usage

## S3 method for class 'server_list'
summary(object, ...)

Arguments

object

a server_list object

...

additional arguments (ignored)


Update an Authentication Key

Description

Replaces an existing API key while keeping the replacement valid throughout the operation by adding the new key before removing the old key.

Usage

updateAuthKey(server_handle, old_key, new_key)

Arguments

server_handle

External pointer from runServer(blocking = FALSE, auth = TRUE).

old_key

Existing API key.

new_key

Replacement API key.

Value

Invisible TRUE.