# Bigscreen Cloud

This is a monorepo for the Arch5 "cloud" Server applications.

It is split into three sections:

1. Yarn workspaces for 12 different server apis and applications.  These are found in many locations in this repo.  The yarn workspace structure is explained further on in the doc.
2. Web applications, contained in the [webapps](/webapps) folder.  This is all the node js web applications, such as the website, `arda`, `rohan`, and `fabricator`. [See the README in root of the webapps folder for more](/webapps).
3. Integration tests, in the [tests](/tests) folder.  See the section [Running Tests](#running-tests) for more.

# Prerequisites

All the following steps need to be followed to setup your local development environment for the cloud repo.

## Redis 6.2.x

A redis server with version 6.2.x must be accessible. The current local development environment has redis 6.2.x running on Ubuntu 18.04.

To install redis 6.2.x on Windows with Ubuntu 18.04:
1. This is a good walk through for setting up Ubuntu with WSL on Windows 11 (there's a similar one for windows 10): https://ubuntu.com/tutorials/install-ubuntu-on-wsl2-on-windows-11-with-gui-support#1-overview
2. Install windows terminal: https://docs.microsoft.com/en-us/windows/terminal/install
3. Connect to the local ubutnu virtual machine, using windows terminal.
4. Run the following to install redis (this is with the LTS version at the time of writing, 6.2.6):
```
sudo apt-get install -y tcl build-essential pkg-config libssl-dev
wget https://download.redis.io/releases/redis-6.2.6.tar.gz
tar xvf redis-6.2.6.tar.gz
cd redis-6.2.6
make
sudo make install
```
Then, to setup and run redis as a service. The `install_server` command will ask a lot of questions for your local redis setup - hit enter to just set the default on all of them:
```
cd utils
sudo ./install_server.sh
```
5. Check that it all works
```
sudo /usr/local/bin/redis-cli -v
```
You should see the following:
```
redis-cli 6.2.6
```


## NodeJS and Typescript

The "meat" of the RDC is written in Typescript, and **Visual Studio Code** is "Bigscreen recommended" Typescript editor.  Visual Studio Code also has a built-in `bash` terminal, which is used to execute a lot of the commands below.

If you're not going to go with **Visual Studio Code** on Windows, you'll need to install a command line tool that is compatible with unix/bash style commands (e.g. cmder).

**The commands below assume you have administrator access on Windows.**

Here are the steps to setup your local environment for development on Windows:
1. Install Volta (Node.js version manager). Download and run the installer from: https://volta.sh/
2. Once Volta is installed, restart your terminal and install Node.js v18:
```
$ volta install node@18
```
3. Install TypeScript, ts-node, and ts-mocha using Volta:
```
$ volta install typescript ts-node ts-mocha yarn
```
4. Verify the installation:
```
$ node -v && npm -v
```
You should see something like:
```
v18.x.x
x.x.x
```
5. Install yarn v3.x from command line - follow the instructions here: https://yarnpkg.com/getting-started/install


## Python 3.10.11

Python 3.10.11 is required to run Fabricator code and run various installation steps.

On windows, install Python 3.10.11 (available here: https://www.python.org/downloads/release/python-31011/), and then follow the `pip3` instructions in `api/src/fabricator/preprocessor/README.md`.

*Note: the verion must match exactly.*

## Install python preprocessor

See `api/src/fabricator/preprocessor/README.md` for instructions - preprocessor won't work without this step.


## Generating environmnent files

WIP:

* Clone devops (https://github.com/BigscreenVR/devops)
* Run the env builder
```
    node app_v6.js \
        --default-config-file "/c/Users/DavidPetrie/Bigscreen/server_keys/default_config.json" \
        --fleet-config-file "/c/Users/DavidPetrie/Bigscreen/server_keys/dev/dev_config.json" \
        --allowed-apps-file "/c/Users/DavidPetrie/Bigscreen/server_keys/dev/dev_allowed_apps.json" \
        --output-folder-path ./.out
```
* Copy the environment files.
* For redis, add this to the bottom of each .env file.
```
REDIS_SERVER_HOST="127.0.0.1"
```


## Postgres setup

The cloud apis assume access to a postgres database.  To set one up on an Ubuntu WSL instance follow these instructions (generated by ChatGPT):

1. **Add the PostgreSQL Repository:**

   You may need to add the PostgreSQL repository that contains the desired version. You can do this by creating a repository file. Use a text editor to create a file in the `/etc/apt/sources.list.d/` directory. For example:

   ```bash
   sudo nano /etc/apt/sources.list.d/pgdg.list
   ```

   Add the following line for PostgreSQL 15 if it's available:

   ```
   deb http://apt.postgresql.org/pub/repos/apt/ bionic-pgdg main
   ```

   Save the file and exit the text editor.

2. **Add the PostgreSQL Repository Key:**

   To ensure the integrity of the packages, you should also add the repository's GPG key:

   ```bash
   wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
   ```

3. **Update the Package List:**

   Update the local package list to include the PostgreSQL packages from the repository:

   ```bash
   sudo apt update
   ```

4. **Install PostgreSQL:**

   You can now install PostgreSQL. Replace `15` with the desired version number:

   ```bash
   sudo apt install postgresql-15
   ```

   This command will install PostgreSQL and its dependencies.

5. **Start and Enable PostgreSQL:**

   By default, PostgreSQL should start automatically after installation. However, you can ensure it's running and set to start on boot with the following commands:

   ```bash
   sudo systemctl start postgresql
   sudo systemctl enable postgresql
   ```

6. **Set a Password for the PostgreSQL Superuser:**

   By default, PostgreSQL installs with a user called "postgres." You should set a password for this user:

   ```bash
   sudo -u postgres psql
   ```

   Inside the PostgreSQL prompt, set a password for the "postgres" user:

   ```sql
   ALTER USER postgres PASSWORD 'postgres_admin_password';
   \q
   ```

   Replace `'your-password'` with your desired password.

7. **Create the database:**

    ```sql
    create database 'localv1db';
    \q
    ```


# Installation

Currently, the Cloud repo functions as a monorepo with four different server applications. They run independantly of one another, and share information using AWS SQS message queues.  Because of this, setup requires several steps.

From the root folder of your Cloud repo, run the following:

```
yarn install
yarn workspace @bigscreen/lib install
yarn workspace @bigscreen/auth install
yarn workspace @bigscreen/api install
yarn workspace @bigscreen/cloud install
yarn workspace bigscreen_admin_api install
```

## Debugging locally.

To run any specific application locally, you can `cd` to the folder, and the run `yarn dev`.

## Running tests

`ts-mocha` is used to run tests - see the previous installation steps.

To ensure that long running tests dont time out and fail, use a `timeout` value:
```
$ ts-mocha --bail --exit --timeout=50000 tests/0*.ts
```

Before pushing an update, make sure all tests pass.

## Media Server on Windows

For local windows development, mediasoup can be installed in the following way:
1. Clone the git repo (https://github.com/versatica/mediasoup/).
2. In a terminal, `cd` to where ever you cloned the mediasoup repo to, and then `npm install`.
3. If there are Visual Studio errors, you can open up the file `mediasoup-worker.sln` and allow Visual Studio to automatically fix the problems, and the start a Visual Studio build under release. `mediasoup-worker.sln` should be in the `worker` folder of where ever you cloned mediasoup to. 
4. Copy the mediasoup folder to your global `node_modules` directory.  On windows, this is normally found in `C:\Users\$USERNAME_HERE\AppData\Roaming\npm\node_modules\`
5. Finally, `cd` back to the `cloud` folder, and run `npm link mediasoup` so the cloud project can find the `mediasoup` folder.


# Webapps

`arda` is the main web app in the `arda` folder.  It is a simple node webserver that forwards requests to the more powerful api servers, and servers a single-page React 0.16 webapp.  Continuous development is supported via webpack 5.90.

## Developing Locally

### Prerequisites

All the previous steps are required. In addition, install webpack globally:

```
npm install --global webpack webpack-cli
```

### Continuous development for the react front end
```
cd ./arda
webpack --mode=development
```

### Running the webserver

To run the arda webserver, a .env file is needed in the arda folder with the following minimum entries (with an example of serveers running on the `dev` network):
```
NETWORK_NAME="dev"
SERVER_FLEET_NAME="dev_local"
PORT=3010
BIGSCREEN_API_URL='https://dev-fire-api.bigscreencloud.com'
BIGSCREEN_ADMIN_API_URL='https://dev-fire-admin-api.bigscreencloud.com'
BIGSCREEN_CLOUD_API_URL='https://dev-fire-cloud-api.bigscreencloud.com'
BIGSCREEN_API_KEY='API_KEY_GOES_HERE'
```
The server addresses can be found on the jenkins build server.  If you need an API key for a specific network, contact David on discord.

To run arda:
```
cd arda
node arda.js
```

## Building with NPX

If you have a different local environment you can use npx to target a specific version of webpack.  Here's an example:
```
cd ./arda && npx --package=webpack-cli@5.1.4 --package=@webpack-cli/generators --package=webpack@5.95.0 webpack build
```

## Building for production

Jenkins will build a new bundle if an update is needed over what is in the repo. However, this is very time consuming on the build server because dependencies must all be installed first.  It usually much faster to just build the bundle file locally and push the result into the repo.  The Jenkins builder will just copy this bundle file to the webserver.  To do this, you just trigger webpack without the `development` flag:
```
cd ./arda
webpack
```
After that, push the result to the repo.