-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebmice.R
60 lines (51 loc) · 1.72 KB
/
webmice.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
library(RestRserve)
library(mice, warn.conflicts = FALSE)
library(jsonlite)
library(readr)
library(digest)
library(stringr)
#' Parameters
#' Code location in Docker, set through bash variable
base_folder <- Sys.getenv("WEBMICE_LOC")
if (base_folder == "") {
base_folder <- getwd()
print('No base folder for webmice set (export WEBMICE_LOC="directory").')
print(paste("Set to: ", base_folder))
}
#' Imports
source(file.path(base_folder, "webmice_handlers.R"))
source(file.path(base_folder, "webmice_functions.R"))
source(file.path(base_folder, "sanitize_input.R"))
#' Data upload location
data_uploads <- file.path(base_folder, "data_uploads")
if (!file.exists(data_uploads)) {
dir.create(data_uploads)
}
#' Application
webmice <- Application$new()
#' Endpoints
webmice$add_post(
path = "/data",
FUN = function(request, response) {
cnt <- request$get_file("csvfile") # 'csv' from the upload form field
# parse CSV
dt <- read_csv(cnt)
hash <- md5_string(request$parameters_body$csvfile)
tmp <- file.path(data_uploads, hash)
write_csv(dt, tmp)
# set output body
response$append_header("data_token", hash)
}
)
webmice$add_get(path = "/version", FUN = mice_version_handler)
webmice$add_get(path = "/exampledata", FUN = example_data_handler)
webmice$add_get(path = "/long", FUN = impute_longfmt_handler)
webmice$add_get(path = "/fit", FUN = fit_handler)
webmice$add_get(path = "/pool", FUN = pool_handler)
#' Swagger
yaml_file <- file.path(base_folder, "openapi.yaml")
webmice$add_openapi(path = "/openapi.yaml", file_path = yaml_file)
webmice$add_swagger_ui(path = "/doc", path_openapi = "/openapi.yaml",
use_cdn = TRUE)
backend <- BackendRserve$new()
backend$start(webmice, http_port = 8080)