Skip to content

Commit

Permalink
remove shinyproxy
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffreyhanson authored Aug 5, 2021
1 parent 54b4e76 commit ebbf662
Show file tree
Hide file tree
Showing 22 changed files with 339 additions and 264 deletions.
5 changes: 2 additions & 3 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ pkgdown
.dockerignore
Dockerfile
docker-compose.yml
.env.dev
.env.prod
.lintr
.env
restart.txt
inst/log
logs
14 changes: 14 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# change this variable to import projects from a different location
PROJECT_DIRECTORY=./inst/extdata/projects

# change this variable to save application logs to a different location
LOG_DIRECTORY=./logs

# change this variable to specify a different application mode
# available options are: "project", "beginner", "advanced"
R_CONFIG_ACTIVE=project

# change to this variable to specify if
# true: the default projects distributed with the application should be used
# false: the projects located in PROJECT_DIRECTORY should be used
FORCE_DEFAULT_PROJECTS=true
5 changes: 0 additions & 5 deletions .env.dev

This file was deleted.

5 changes: 0 additions & 5 deletions .env.prod

This file was deleted.

2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ check.log
spell.log
gp.log
examples.log
restart.txt
inst/log/*
*.log

# system files
.directory
Expand Down
35 changes: 12 additions & 23 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# source image
# base image
FROM rocker/shiny:4.1.0 AS base

## remove example apps
Expand Down Expand Up @@ -65,37 +65,26 @@ WORKDIR /app
## run app
CMD Rscript -e "rsconnect::setAccountInfo(name=Sys.getenv('SHINYAPPS_USER'), token=Sys.getenv('SHINYAPPS_TOKEN'),secret=Sys.getenv('SHINYAPPS_SECRET'));rsconnect::deployApp('.', appName=Sys.getenv('SHINYAPPS_APPNAME'))"

# main image
FROM base AS main

## allow shiny server to spawn processes for different users
RUN cd /srv/shiny-server && \
touch restart.txt && \
chmod 666 restart.txt
# shiny image
FROM base AS shiny

## set user
USER shiny

## select port
EXPOSE 3838

## copy shiny server config file
COPY inst/shiny-server.conf /etc/shiny-server/shiny-server.conf

## prepare log directory
RUN mkdir -p /var/log/shiny-server && \
chown shiny.shiny /var/log/shiny-server && \
chmod -R 666 /var/log/shiny-server

## copy app file for shiny server
COPY --chown=shiny:shiny app.R /srv/shiny-server

## configure shiny
## store environmental variables
ENV R_CONFIG_ACTIVE=production
RUN env | grep R_CONFIG_ACTIVE > /home/shiny/.Renviron
ENV R_SHINY_PORT=3838
ENV R_SHINY_HOST=0.0.0.0
RUN env | grep R_SHINY_PORT > /home/shiny/.Renviron && \
env | grep R_SHINY_HOST >> /home/shiny/.Renviron

## set working directory
WORKDIR /home/shiny
RUN mkdir /home/shiny/app
COPY app.R /home/shiny/app/app.R
WORKDIR /home/shiny/app

## run app
CMD ["/usr/bin/shiny-server"]
CMD ["/usr/local/bin/Rscript", "/home/shiny/app/app.R"]
46 changes: 11 additions & 35 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -68,56 +68,32 @@ quick-debug:

## launch local version inside Docker container
demo:
docker-compose --env-file ./.env.dev up --build
docker-compose up --build

demo-kill:
docker-compose --env-file ./.env.dev down
docker-compose down

## launch released version inside Docker container
launch:
docker run -dp 3939:3838 --name wheretowork -it naturecons/wheretowork
docker run -dp 3838:3838 --name wheretowork -it naturecons/wheretowork

launch-kill:
docker rm --force wheretowork

## deploy Docker swarm for debugging application
## view it at: http://127.0.0.1:3939/
deploy:
docker swarm init --advertise-addr 127.0.0.1 --listen-addr 0.0.0.0 && \
set -a; . ./.env.dev; set +a && \
docker stack deploy wheretoworkapp -c docker-compose.yml

deploy-kill:
docker stack rm wheretoworkapp
docker swarm leave --force

## deploy Docker swarm for production
## view it at: http://localhost:3939/
prod:
docker swarm init && \
set -a; . ./.env.prod; set +a && \
docker stack deploy wheretoworkapp -c docker-compose.yml

prod-kill:
docker stack rm wheretoworkapp
docker swarm leave --force

prod-update:
echo "TODO"

## force kill networks
network-kill:
docker network disconnect -f wheretoworkapp_default wheretoworkapp_default-endpoint || docker network disconnect -f wheretoworkapp_app wheretoworkapp_app-endpoint
docker network prune -f

## deploy app on shinyapps.io
shinyapps:
R -e "rsconnect::deployApp(getwd(), appName = 'wheretowork', launch.browser = TRUE)"

# Docker commands
## create local Docker image
## create local image and push to docker
image:
docker build -t wheretowork-image .
docker build -t naturecons/wheretowork:latest .
docker push naturecons/wheretowork:latest

## delete all local containers and images
reset:
docker rm $(docker ps -aq) || \
docker rmi -f $(docker images -aq)

# renv commands
## snapshot R package dependencies
Expand Down
16 changes: 14 additions & 2 deletions R/app_global.R
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,22 @@ app_global <- quote({
)

# find built-in projects
project_dir <- wheretowork::get_golem_config("projects")
if (identical(project_dir, "default")) {
# if environmental variable "FORCE_DEFAULT_PROJECTS=true":
# then use built-in projects
# if "projects: default" in golem-config.yml:
# then use built-in projects
# else:
# then import projects from location specified in golem-config.yml
if (!identical(Sys.getenv("FORCE_DEFAULT_PROJECTS"), "true")) {
project_dir <- wheretowork::get_golem_config("projects")
if (identical(project_dir, "default")) {
project_dir <- system.file("extdata", "projects", package = "wheretowork")
}
} else {
project_dir <- system.file("extdata", "projects", package = "wheretowork")
}

# import projects
project_data <- wheretowork::find_projects(project_dir)

})
3 changes: 0 additions & 3 deletions R/app_server.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
app_server <- function(input, output, session) {

# initialization
## spawn new process so that users in different process
# try(system("touch restart.txt"), silent = TRUE)

## initialize app
eval(server_initialize_app)

Expand Down
75 changes: 6 additions & 69 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -33,96 +33,33 @@ The application is available as an online service provided by the [Nature Conser

### Using Docker

To run the application using this method, you will need to install the [Docker Engine](https://www.docker.com/) software ([see here for instructions](https://docs.docker.com/get-docker/)). After completing this step, you can install the application from the [DockerHub repository](https://hub.docker.com/repository/docker/naturecons/wheretowork). Specifically, please use the following system command to install the latest official version of the application:
To run the application using this method, you will need to install the [Docker Engine](https://www.docker.com/) software ([see here for instructions](https://docs.docker.com/get-docker/)). After completing this step, you can install the application from the [DockerHub repository](https://hub.docker.com/repository/docker/naturecons/wheretowork). Specifically, please use the following system command:

```{bash, eval = FALSE}
docker run -dp 3939:3939 --name wheretowork -it naturecons/wheretowork:latest
docker run -dp 3838:3838 --name wheretowork -it naturecons/wheretowork:latest
```

You can then view the application by opening the following link in [Google Chrome](https://www.google.com/chrome/): http://localhost:3939. After you have finished using the application, you can terminate it using the following system command. **Note that if you don't terminate the application once you are finished using it, then it will continue running in the background.**
You can then view the application by opening the following link in [Google Chrome](https://www.google.com/chrome/): http://localhost:3838. After you have finished using the application, you can terminate it using the following system command. **Note that if you don't terminate the application once you are finished using it, then it will continue running in the background.**

```{bash, eval = FALSE}
docker rm -f wheretowork
```

Additionally, the latest development version can be installed using the following system command. Please note that while developmental versions may contain additional features not present in the official version, they may also contain defects.

```{bash, eval = FALSE}
docker run -dp 3939:3939 --name wheretowork -it naturecons/wheretowork:devel
```

Similar to the official version, you can access the developmental version of the application by opening the following link in [Google Chrome](https://www.google.com/chrome/): http://localhost:3939. Also note that you should terminate the developmental version of the application once you are finished using it (using the same code above for the official version).

### Using R

To run the application using this method, you will need to install the [R statistical computing environment](https://www.r-project.org/). After completing the installation, you can install the latest official version of the application by using the following R code:
To run the application using this method, you will need to install the [R statistical computing environment](https://www.r-project.org/). After completing the installation, you can install the application using the following R code:

```{r, eval = FALSE}
if (!require(remotes)) install.packages("remotes")
remotes::install_github("NCC-CNC/wheretowork@*release")
remotes::install_github("NCC-CNC/wheretowork")
```

You can then use the following R code to run the application and automatically open it in your web browser:
You can then use the following R code to start the application and open it in your web browser:

```{r, eval = FALSE}
wheretowork::run_app()
```

Additionally, the latest development version can be installed using the following R code. Please note that while developmental versions may contain additional features not present in the official version, they may also contain defects.

```{r, eval = FALSE}
if (!require(remotes)) install.packages("remotes")
remotes::install_github("NCC-CNC/wheretowork")
```

## Deployment

This application can be deployed using a [nginx web server](https://www.nginx.com/). To achieve this, you first need to install and configure the web server ([see here for instructions](https://www.digitalocean.com/community/tutorials/how-to-install-nginx-on-ubuntu-18-04)). After setting up the web server, you need to update the firewall rules to permit access to port 3939. This is because port 3939 is to serve the application:

```{bash, eval = FALSE}
sudo ufw allow 3939/tcp
```


```
##
# wheretowork application
##
location /wheretowork/ {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://localhost:3939;
proxy_read_timeout 20d;
proxy_buffering off;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_http_version 1.1;
proxy_redirect http://localhost:3939/ $scheme://$host/wheretowork;
}
```

Next, restart the web server using the following command. This is important so that it will use the updated configuration file:

```{bash, eval = FALSE}
sudo nginx -s reload
```

Finally, enter the command `sudo make prod` to launch the application. The application can now be accessed at `http://mywebsite.com/wheretowork`, where `mywebsite.com` is the domain of your website.


```
http://nature-vm01.carleton.ca:3939
https://ncc.carleton.ca//wheretowork
sudo nginx -s reload
```


## Contributing

The application is a [Shiny web application](https://mastering-shiny.org/) developed using the [R statistical computing environment](https://www.r-project.org/). Specifically, it uses the [`golem` framework](https://thinkr-open.github.io/golem/). This means that the application is effectively an [R package](https://r-pkgs.org/) that contains code for defining and launching the application (see [here](https://engineering-shiny.org/) for more details). The R code files (located in the `./R` directory) are organized the following system of naming conventions:
Expand Down
45 changes: 9 additions & 36 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ Canada](https://natureconservancy.ca/en/).
## Usage

The application is [available
online](http://nature3939-vm01.carleton.ca/wheretowork). **Please note
that you must use [Google Chrome](https://www.google.com/chrome/) for it
to work.**.
online](https://ncc.carleton.ca//wheretowork). **Please note that you
must use [Google Chrome](https://www.google.com/chrome/) for it to
work.**.

## Installation

Expand All @@ -35,8 +35,7 @@ To run the application using this method, you will need to install the
instructions](https://docs.docker.com/get-docker/)). After completing
this step, you can install the application from the [DockerHub
repository](https://hub.docker.com/repository/docker/naturecons/wheretowork).
Specifically, please use the following system command to install the
latest official version of the application:
Specifically, please use the following system command:

``` bash
docker run -dp 3838:3838 --name wheretowork -it naturecons/wheretowork:latest
Expand All @@ -53,51 +52,25 @@ it will continue running in the background.**
docker rm -f wheretowork
```

Additionally, the latest development version can be installed using the
following system command. Please note that while developmental versions
may contain additional features not present in the official version,
they may also contain defects.

``` bash
docker run -dp 3838:3838 --name wheretowork -it naturecons/wheretowork:devel
```

Similar to the official version, you can access the developmental
version of the application by opening the following link in [Google
Chrome](https://www.google.com/chrome/): <http://localhost:3838>. Also
note that you should terminate the developmental version of the
application once you are finished using it (using the same code above
for the official version).

### Using R

To run the application using this method, you will need to install the
[R statistical computing environment](https://www.r-project.org/). After
completing the installation, you can install the latest official version
of the application by using the following R code:
completing the installation, you can install the application using the
following R code:

``` r
if (!require(remotes)) install.packages("remotes")
remotes::install_github("NCC-CNC/wheretowork@*release")
remotes::install_github("NCC-CNC/wheretowork")
```

You can then use the following R code to run the application and
automatically open it in your web browser:
You can then use the following R code to start the application and open
it in your web browser:

``` r
wheretowork::run_app()
```

Additionally, the latest development version can be installed using the
following R code. Please note that while developmental versions may
contain additional features not present in the official version, they
may also contain defects.

``` r
if (!require(remotes)) install.packages("remotes")
remotes::install_github("NCC-CNC/wheretowork")
```

## Contributing

The application is a [Shiny web
Expand Down
1 change: 1 addition & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ reference:
contents:
- leaflet_map
- importModal
- helpModal
- solutionResultsModal
- horizontalPickerInput

Expand Down
Loading

0 comments on commit ebbf662

Please sign in to comment.