-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.R
179 lines (159 loc) · 6.08 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# Define server logic required
#==========================================================================================
server <- function(input, output, session) {
waiting_screen<-tagList(
h3("Initializing..."),
spin_flower()
)
waiter_show(html = waiting_screen)
#global variables / environment
#---------------------------------------------------------------------------------------
jwt <- Sys.getenv("SHINYPROXY_OIDC_ACCESS_TOKEN")
if(jwt == ""){
waiter_hide()
shiny::showModal(
shiny::modalDialog(
title = "Error",
shiny::tagList(
tags$span("No JWT token available from Shiny Proxy!")
)
)
)
stop("Application has stopped!")
}
#D4S components
#---------------------------------------------------------------------------------------
PROFILE <- try(loadProfile(jwt))
if(is(PROFILE, "try-error")){
waiter_update(html = tagList(
h4("Error while loading profile!", style = "color:red;"),
spin_flower()
))
Sys.sleep(2)
waiter_hide()
shiny::showModal(
shiny::modalDialog(
title = "Error",
shiny::tagList(
PROFILE,
br(),
sprintf("Token: %s", jwt)
)
)
)
stop("Application has stopped!")
}
#COMPONENTS
waiter_update(html = tagList(
h3("Loading components..."),
spin_flower()
))
COMPONENTS <- try(loadComponents(profile = PROFILE, sdi = FALSE))
if(is(COMPONENTS, "try-error")){
waiter_hide()
shiny::showModal(shiny::modalDialog(title = "Error", COMPONENTS[1]))
stop("Application has stopped!")
}
#TODO current config from file, next to get from Workspace URL inherited from ICPROXY
#---------------------------------------------------------------------------------------
#default config_file path for DEPLOYMENT (hidden file)
config_file <- COMPONENTS$STORAGEHUB$downloadItemByPath("dcf-shiny-config/config.yml", wd = tempdir())
#local configuration
#If you are an R developer, you need to create a .REnviron file (no file extension) in /dcf-shiny dir
#The file should include the local path for your shiny config file in that way:
#DCF_SHINY_CONFIG=<your config path>
waiter_update(html = tagList(
h3("Loading configuration..."),
spin_flower()
))
local_config_file <- Sys.getenv("DCF_SHINY_CONFIG")
if(nzchar(local_config_file)) config_file <- local_config_file
CONFIG <- read_dcf_config(file = config_file)
print("STEP CONFIG")
waiter_update(html = tagList(
h3(paste0("Welcome to ",CONFIG$dcf$name)),
spin_flower()
))
#reporting entities config options
#---------------------------------------------------------------------------------------
if(!is.null(CONFIG$dcf$reporting_entities)){
if(is.null(CONFIG$dcf$reporting_entities$icon)) CONFIG$dcf$reporting_entities$icon = ""
}
#VRULE config options
#---------------------------------------------------------------------------------------
if(!is.null(CONFIG$vrule)){
if(!is.null(CONFIG$vrule$parallel)) vrule::setVruleOptions(parallel = CONFIG$vrule$parallel)
if(!is.null(CONFIG$vrule$cores)) vrule::setVruleOptions(cores = min(CONFIG$vrule$cores, parallel::detectCores()))
}
#DBI component to add
#---------------------------------------------------------------------------------------
waiter_update(html = tagList(
h3(paste0("Welcome to ",CONFIG$dcf$name)),
spin_flower(),
div("Connecting to database ...")
))
pool <- loadDBI(config = CONFIG)
COMPONENTS$POOL <- pool
print("STEP POOL")
#User full profile (roles) initialization
#---------------------------------------------------------------------------------------
waiter_update(html = tagList(
h3(paste0("Welcome to ",CONFIG$dcf$name)),
spin_flower(),
div("User identification ...")
))
print("STEP PROFILE")
PROFILE <- fetchProfileRoles(pool = COMPONENTS$POOL, profile = PROFILE)
if("admin" %in% PROFILE$shiny_app_roles){
COMPONENTS <- loadComponents(profile = PROFILE, sdi = TRUE)
COMPONENTS$POOL <- pool
}
waiter_update(html = tagList(
h3(paste0("Welcome to ",CONFIG$dcf$name)),
spin_flower(),
h4(paste0("Welcome ",PROFILE$name," !")),
))
#INITIALIZATION
#---------------------------------------------------------------------------------------
print("STEP INITIALIZATION")
#initAppWorkspace
CONFIG$dcf$user_workspace <- sprintf("%s-%s", CONFIG$dcf$workspace, PROFILE$preferred_username)
CONFIG$workspace_id <- initAppWorkspace(config = CONFIG, profile = PROFILE, components = COMPONENTS)
#initAppDataspace
CONFIG$dataspace_id <- initAppDataspace(config = CONFIG, profile = PROFILE, components = COMPONENTS)
#enrich profile with reporting entities
PROFILE$reporting_entities <- getDBUserReportingEntities(profile = PROFILE, pool = COMPONENTS$POOL)
#in case of expired data calls we automatically close them
closeExpiredDataCalls(pool=COMPONENTS$POOL,config=CONFIG,profile = PROFILE)
#in case of started data calls we automatically open them
openStartedDataCalls(pool=COMPONENTS$POOL,config=CONFIG,profile = PROFILE)
#render UI
output$header <- renderUI({
tags$span(CONFIG$dcf$name)
})
output$sidebar <- renderUI({
sidebarMenuFromModules(config = CONFIG, profile = PROFILE)
})
output$body <- renderUI({
do.call("tabItems",c(
loadModuleUIs(config = CONFIG, profile = PROFILE),
loadPluginUIs(config = CONFIG, profile = PROFILE)
))
})
reloader<-reactiveVal(NULL)
initialized<-reactiveVal(FALSE)
observeEvent(reloader(),{
req(!is.null(initialized()))
if(!initialized()){
loadModuleServers(parent.session = session, config = CONFIG, profile = PROFILE, components = COMPONENTS,reloader)
loadPluginServers(parent.session = session, config = CONFIG, profile = PROFILE, components = COMPONENTS)
initialized<-initialized(TRUE)
waiter_hide()
}else{
req(!is.null(reloader()))
#load module servers
loadModuleServers(parent.session = session, config = CONFIG, profile = PROFILE, components = COMPONENTS,reloader)
reloader<-reloader(NULL)
}
},ignoreNULL = F)
}