-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.R
129 lines (111 loc) · 3.38 KB
/
server.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# install libraries if not already installed
renv::restore()
# libraries: httpuv, rmarkdown
library(urltools)
library(stringr)
# FUTURE:
# We should be able to return the params metadata structure via a custom
# HTTP header using something like this:
# jsonlite::toJSON(rmarkdown::metadata$params, auto_unbox = TRUE)
readFile = function(
fileName
) {
invisible(readChar(fileName, file.info(fileName)$size))
}
createServer = function(
host = '127.0.0.1',
port = 5001,
app
) {
port = as.integer(port)
server = NULL
list(
host = host,
port = port,
startServer = function() {
id = httpuv::startServer(host, port, app)
server <<- id
print(paste0('server started on port ',port))
invisible(id)
},
stopServer = function() {
if (is.null(server)) stop('The server has not been started yet.')
try(httpuv::stopServer(server))
}
)
}
httd = function(...) {
# httpuv uses Rook-style request handling:
# https://github.com/jeffreyhorner/Rook
app <- list(call = function(env){
if (env$REQUEST_METHOD == "OPTIONS") {
list(
status = 200L,
headers = list(
'Access-Control-Allow-Origin' = "*",
'Access-Control-Allow-Methods' = "GET, POST, PUT, UPDATE, DELETE",
'Access-Control-Allow-Headers' = "",
'Cache-Control' = 'max-age=3600',
'Content-Type' = "text/html; charset=utf-8"
),
body = ''
)
} else if (env$REQUEST_METHOD == "GET") {
public_root <- "./public"
base_file_name <- env$PATH_INFO
returnParams <- FALSE
if (str_detect(base_file_name, "^/params/")) {
base_file_name <- str_replace(base_file_name, "^/params", "")
returnParams <- TRUE
}
file_name <- paste(public_root, base_file_name, sep = "")
if (!file.exists(file_name)) {
file_name <- paste(public_root, base_file_name, ".Rmd", sep = "")
}
if (file.exists(file_name)) {
if (returnParams) {
params <- rmarkdown::yaml_front_matter(file_name)$params
list(
status = 200L,
headers = list(
'Content-Type' = 'application/json',
'Cache-Control' = 'max-age=3600',
'Access-Control-Allow-Origin' = '*'
),
body = jsonlite::toJSON(params)
)
} else {
.params <- as.list(param_get(env$QUERY_STRING))
# render to temp file
output_file_name <- paste(stringi::stri_rand_strings(1,20),'.html', sep = "")
rmarkdown::render(file_name, params=.params, output_file=output_file_name)
# read temp file
output_file_path <- paste(public_root, output_file_name, sep="/")
body <- readFile(output_file_path)
# delete the temp file
unlink(output_file_path)
list(
status = 200L,
headers = list(
'Content-Type' = 'text/html',
'Cache-Control' = 'max-age=3600',
'Access-Control-Allow-Origin' = '*'
),
body = body
)
}
} else {
list(
status = 404L
)
}
}
})
server <- createServer(app = app, ...)
server$startServer()
invisible(server)
}
try(if (!is.null(.GlobalEnv$runningServer)) {
.GlobalEnv$runningServer$stopServer()
}, silent=TRUE)
.GlobalEnv$runningServer <- httd()