Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No calls to outside API's using httr2 in shinyserver. #80

Open
Phantom140220 opened this issue Sep 17, 2024 · 0 comments
Open

No calls to outside API's using httr2 in shinyserver. #80

Phantom140220 opened this issue Sep 17, 2024 · 0 comments

Comments

@Phantom140220
Copy link

I'm having an issue with making calls to external services within the shiny-server container. This function worked a couple of months ago but now is failing without any logging.

This the function that is failing:

  get_api_data <- function() {
  # Create the url for the api-call
  api_url <- "https://catfact.ninja/fact" #example API

  tryCatch(
    {
      # Buidling of the rest request
      # set the url
      req <- request(api_url)
      req <- req %>% req_retry(max_tries = 1)
      # set method to get
      req <- req %>% req_method("GET")
      # execute request
      resp <- req_perform(req)
      print(resp)
      # response handling
      # if the call was successfull (20x code the function returns true and the body)
      if ((resp_status(resp) >= 200) && ((resp_status(resp) < 300))) {
        data <- resp_body_json(resp)
        return(list(data = data$data, success = TRUE))
      # if the call has a 401 returned the user is redirected to the launcpad
      } else if (resp_status(resp) == 401) {
        # runjs(paste0('window.location.href = "', redirectLink, '";'))
      # if another reponse is created this will be logged
      } else {
        print(paste("Error: Unable to fetch data from", response$url))
        print(resp_body_raw(resp))
        return(list(success = FALSE))
      }
    },
    # If an error happens in this process this will be logged
    error = function(cond) {
      print(paste("error getting", api_url))
      # if the error contains an error 401 the user will be redirected
      if (grepl("HTTP 401", conditionMessage(cond), fixed = TRUE)) {
        # runjs(paste0('window.location.href = "', redirectLink, '";'))
      }
      print(conditionMessage(cond))
      return(list(success = FALSE))
    }
  )
}

This function works perfectly in Rstudio. I get the bodies of the requests, even if they fail. But when i want to deploy it in the container, the static sites loads but no call are made (checked the backend service).

This is the dockerfile that is used:

# checkov:skip=CKV_DOCKER_2:Healthcheck instructions have not been added to container images
# checkov:skip=CKV_DOCKER_3:"Ensure that a user for the container has been created"
# hadolint global ignore=DL3008

ARG r=4.4.0
FROM rocker/r-ver:4

ARG shinyserver=1.5.20.1002
ENV SHINY_SERVER_VERSION=${shinyserver}
ENV PANDOC_VERSION=default
RUN /rocker_scripts/install_shiny_server.sh

ENV STRINGI_DISABLE_PKG_CONFIG=true \
  AWS_DEFAULT_REGION=eu-west-1 \
  TZ=Etc/UTC \
  LC_ALL=C.UTF-8

WORKDIR /srv/shiny-server

# Cleanup shiny-server dir
RUN rm -rf ./*

# Make sure the directory for individual app logs exists
RUN mkdir -p /var/log/shiny-server

# Ensure Python venv is installed (used by reticulate).
RUN apt-get update -y && \
  apt-get install -y \
  python3 \
  python3-pip \
  python3-venv \
  python3-dev \
  libxml2-dev \
  libssl-dev \
  libudunits2-dev \
  libgdal-dev \
  libgeos-dev \
  libproj-dev\
  gdal-bin \
  git \
  libssl-dev \
  libsqlite3-dev \
  python3-boto \
  xtail \
  gettext-base \
  software-properties-common

# APT Cleanup
RUN apt-get clean && rm -rf /var/lib/apt/lists/

COPY app/ .
RUN Rscript packages.R

RUN git clone https://github.com/ministryofjustice/analytical-platform-rshiny-open-source-base.git
RUN cp analytical-platform-rshiny-open-source-base/shiny-server.sh /usr/bin/shiny-server.sh
RUN cp analytical-platform-rshiny-open-source-base/gather_env_vars.py .

# Patch the shiny server to allow custom headers
RUN sed -i 's/createWebSocketClient(pathInfo)/createWebSocketClient(pathInfo, conn.headers)/' /opt/shiny-server/lib/proxy/sockjs.js
RUN sed -i "s/'referer'/'referer', 'cookie', 'user_email', 'extra-token'/" /opt/shiny-server/node_modules/sockjs/lib/transport.js
# Shiny runs as 'shiny' user, adjust app directory permissions
RUN groupmod -g 998 shiny
RUN usermod -u 998 -g 998 shiny
RUN chown -R 998:998 .
RUN chown -R 998:998 /etc/shiny-server
RUN chown -R 998:998 /var/lib/shiny-server

RUN chown -R 998:998 /opt/shiny-server
RUN chown -R 998:998 /var/log/shiny-server
RUN chown -R 998:998 /etc/init.d/shiny-server
RUN chown -R 998:998 /usr/local/lib/R/etc
RUN chown -R 998:998 /usr/local/lib/R/site-library
RUN chown 998:998 /usr/bin/shiny-server.sh
RUN chmod +x /usr/bin/shiny-server.sh

RUN chown 998:998 /etc/profile
ENV ENV_URL URL

COPY shiny-server.conf.template /etc/shiny-server/

EXPOSE 8080

CMD envsubst '$ENV_URL'< /etc/shiny-server/shiny-server.conf.template > /etc/shiny-server/shiny-server.conf ; /usr/bin/shiny-server.sh

This is the shiny config file

# Instruct Shiny Server to run applications as the user "shiny"
run_as shiny;

app_init_timeout 600;

# Define a server that listens on port 9999
server {
  listen 8080;

  # whitelist_headers x-ms-token-aad-id-token;

  # Define a location at the base URL
  location /${ENV_URL}/ {

    # Host the directory of Shiny Apps stored in this directory
    site_dir /srv/shiny-server;

    # Log all Shiny output to files in this directory
    log_dir /var/log/shiny-server;
  }
}

Everything of this code snippets worked perfectly a couple of months ago. But when building a new image, no calls are made from the container.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant