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

Runtime environment variable substitution #52

Merged
merged 23 commits into from
Sep 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
b123a2c
build: Add init script to substitute env variables at runtime
redmushie Sep 24, 2023
64d883a
build: Consolidate production/development configurations to generaliz…
redmushie Sep 24, 2023
ae939b8
Merge branch 'master' of github.com:Shock-Link/WebUI into feature/run…
redmushie Sep 24, 2023
ce22866
build: Make startup.sh executable
redmushie Sep 24, 2023
935ca38
build: Inject SHOCKLINK_WEBUI_DOMAIN env variable
redmushie Sep 24, 2023
3336332
build: Pipe find into sed when substituting env vars
redmushie Sep 24, 2023
c9a954e
build: Fix find/sed env var substitution
redmushie Sep 24, 2023
c320f1a
ci: Rename build-ui to build, build-container to containerize, publis…
redmushie Sep 24, 2023
ea0a475
fix(build): Fix search path for find/sed environment variable substit…
redmushie Sep 24, 2023
5ea6322
fix(build): Use plus to reference all files when sed replacing
redmushie Sep 24, 2023
468138f
fix(build): Finally truly fix the problem with environment variable s…
redmushie Sep 24, 2023
a3ae161
ci: Re-add dev/prod configurations to not break existing CI/CD
redmushie Sep 24, 2023
03f2d7e
build: Change env var name suffixes to _URL
redmushie Sep 24, 2023
7d3dbd1
docs: Add docker-compose.yml
redmushie Sep 24, 2023
a8dca7d
build: Include full URL in env variables
redmushie Sep 24, 2023
872c521
build: Also apply new env names in startup.sh
redmushie Sep 24, 2023
d0d5cae
build: Change sed delimiter to not conflict with URLs
redmushie Sep 24, 2023
528bf02
docs: Update docker-compose.yml
redmushie Sep 24, 2023
13c871d
chore: Clean up ci-tag.yml
redmushie Sep 24, 2023
dc071ba
chore: Remove inlined docker-compose.yml from README and reference fi…
redmushie Sep 24, 2023
dba8882
docs: Update container name reference
redmushie Sep 24, 2023
d8a1282
fix: main.js was accidentally autoformatted
redmushie Sep 24, 2023
9a13047
build: Fix default share URL containing one specific path
redmushie Sep 24, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .github/workflows/ci-develop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ env:
REGISTRY: ghcr.io
ARTIFACT_NAME: shocklink-webui.zip
IMAGE_NAME: ${{ github.repository_owner }}/webui
TARGET_ENV: development
TARGET_ENV: container
NODE_ENV: development

jobs:

build-ui:
build:
runs-on: ubuntu-latest

steps:
Expand Down Expand Up @@ -58,9 +58,9 @@ jobs:
path: ${{ github.workspace }}/${{ env.ARTIFACT_NAME }}
retention-days: 1

build-container:
containerize:
runs-on: ubuntu-latest
needs: build-ui
needs: build

steps:
- uses: actions/checkout@v4
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/ci-master.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ env:
REGISTRY: ghcr.io
ARTIFACT_NAME: shocklink-webui.zip
IMAGE_NAME: ${{ github.repository_owner }}/webui
TARGET_ENV: production
TARGET_ENV: container
NODE_ENV: production

jobs:

build-ui:
build:
runs-on: ubuntu-latest

steps:
Expand Down Expand Up @@ -61,9 +61,9 @@ jobs:
path: ${{ github.workspace }}/${{ env.ARTIFACT_NAME }}
retention-days: 1

build-container:
containerize:
runs-on: ubuntu-latest
needs: build-ui
needs: build

steps:
- uses: actions/checkout@v4
Expand Down
44 changes: 22 additions & 22 deletions .github/workflows/ci-tag.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ env:
REGISTRY: ghcr.io
ARTIFACT_NAME: shocklink-webui.zip
IMAGE_NAME: ${{ github.repository_owner }}/webui
TARGET_ENV: production
TARGET_ENV: container
NODE_ENV: production

jobs:

build-ui:
build:
runs-on: ubuntu-latest

steps:
Expand Down Expand Up @@ -55,27 +55,9 @@ jobs:
path: ${{ github.workspace }}/${{ env.ARTIFACT_NAME }}
retention-days: 1

publish-artifacts:
containerize:
runs-on: ubuntu-latest
needs: build-ui

steps:
- name: Download internal artifacts
uses: actions/download-artifact@v3
with:
name: ${{ env.ARTIFACT_NAME }}

- name: Upload artifacts to tag
uses: svenstaro/upload-release-action@v2
with:
repo_token: ${{ secrets.GITHUB_TOKEN }}
file: ${{ env.ARTIFACT_NAME }}
asset_name: ${{ env.ARTIFACT_NAME }}
tag: ${{ github.ref }}

build-container:
runs-on: ubuntu-latest
needs: build-ui
needs: build

steps:
- uses: actions/checkout@v4
Expand Down Expand Up @@ -128,3 +110,21 @@ jobs:
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}

publish:
runs-on: ubuntu-latest
needs: containerize

steps:
- name: Download internal artifacts
uses: actions/download-artifact@v3
with:
name: ${{ env.ARTIFACT_NAME }}

- name: Upload artifacts to tag
uses: svenstaro/upload-release-action@v2
with:
repo_token: ${{ secrets.GITHUB_TOKEN }}
file: ${{ env.ARTIFACT_NAME }}
asset_name: ${{ env.ARTIFACT_NAME }}
tag: ${{ github.ref }}
16 changes: 16 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,2 +1,18 @@
FROM nginx:1-alpine

ENV SHOCKLINK_API_URL=https://api.shocklink.net
ENV SHOCKLINK_WEBUI_URL=https://shocklink.net/#/
ENV SHOCKLINK_SHARE_URL=https://shockl.ink/

# Copy release artifacts (static JS and CSS bundles)
COPY dist /usr/share/nginx/html

# Copy custom startup script.
# This script performs environment variable substitution!
COPY startup.sh .
RUN ["chmod", "+x", "/startup.sh"]

# Start up nginx using the alternative entrypoint, but with the default CMD.
# See: https://github.com/nginxinc/docker-nginx/blob/master/mainline/debian/Dockerfile#L113-L119
ENTRYPOINT ["/startup.sh"]
CMD ["nginx", "-g", "daemon off;"]
26 changes: 12 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,36 +16,34 @@ This is the ShockLink Web UI. It is a single-page application that communicates

# Configuring

The [shocklink-webui](https://github.com/Shock-Link/WebUI/pkgs/container/shocklink-webui) container unfortunately does not support any configuration beyond nginx configuration, as it is currently based on `nginx:alpine`.
The [webui](https://github.com/Shock-Link/WebUI/pkgs/container/webui) container supports configuration via environment variables.

If you wish to change the API that the WebUI communicates with, locally modify the [configuration file](src/globals/config/config.production.js) and build the container manually.
|Variable|Default|Description|
|-|-|-|
|`SHOCKLINK_API_URL`|`https://api.shocklink.net/`| URL of the API. |
|`SHOCKLINK_WEBUI_URL`|`https://shocklink.net/#/`| URL of the ShockLink WebUI. |
|`SHOCKLINK_SHARE_URL`|`https://shockl.ink/s/`| URL to prefix share links with. When visited, should redirect to `${SHOCKLINK_WEBUI_URL}/public/proxy/shares/links/{ID}`. |

# Deployment

This documentation describes how to self-host the WebUI container. This might not be of interest to you if you are content using [ShockLink.net](https://shocklink.net).

## Using Docker
Assuming you are running on `localhost`, with [the API](https://github.com/Shock-Link/API) running on port `5001`:

```bash
$ docker run \
-p 80:80/tcp \
-p 5002:80/tcp \
-e SHOCKLINK_API_URL=http://localhost:5001/ \
-e SHOCKLINK_WEBUI_URL=http://localhost:5002/#/ \
-e SHOCKLINK_SHARE_URL=http://localhost:5002/#/public/proxy/shares/links/ \
--name shocklink-webui \
ghcr.io/shocklink/webui:latest
```

## Using `docker-compose`
At the time of writing, the [ShockLink API](https://github.com/Shock-Link/API) is not yet readily available. This example contains **only** the WebUI.

```yml
version: '3.9'

services:
webui:
image: ghcr.io/shock-link/webui:latest
container_name: shocklink-webui
ports:
- "80:80/tcp"
```
See [docker-compose.yml](docker-compose.yml).

# Development
Contributions are welcome! We're eager to see what you come up with. Make sure to [join us on Discord](https://discord.gg/AHcCbXbEcF).
Expand Down
43 changes: 43 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
version: '3.9'

services:

# Database. Only Postgres is currently supported.
db:
image: postgres:16
ports:
- "5432:5432/tcp"
environment:
- POSTGRES_PASSWORD=postgres

# Redis with Redisearch.
redis:
image: redis/redis-stack-server:latest
volumes:
- /opt/docker/shocklink/redis/redis-stack.conf:/redis-stack.conf

# The API.
# Check https://github.com/Shock-Link/API for the latest configuration settings.
api:
image: ghcr.io/shock-link/api:latest
depends_on:
- db
- redis
ports:
- "5001:80/tcp"
environment:
- SHOCKLINK__DB=Host=db;Port=5432;Database=postgres;Username=postgres;Password=postgres;Search Path=ShockLink
- SHOCKLINK__REDIS__HOST=redis
- SHOCKLINK__FRONTENDBASEURL=http://local:5002

# The Web UI (this repository).
webui:
image: ghcr.io/redmushie/webui:master
depends_on:
- api
ports:
- "5002:80/tcp"
environment:
- SHOCKLINK_API_URL=http://local:5001/
- SHOCKLINK_WEBUI_URL=http://local:5002/#/
- SHOCKLINK_SHARE_URL=http://local:5002/#/public/proxy/shares/links/
1 change: 1 addition & 0 deletions redis-stack.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
notify-keyspace-events KEA
7 changes: 7 additions & 0 deletions src/globals/config/config.container.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const config = {
apiUrl: "SHOCKLINK_API_URL",
webUiUrl: "SHOCKLINK_WEBUI_URL",
shortUrl: "SHOCKLINK_SHARE_URL"
}

global.config = config;
36 changes: 36 additions & 0 deletions startup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/bin/sh

# Variables for startup script
SERVE_PATH=/usr/share/nginx/html
NAME=ShockLink

require() {
if [ -z "$1" ]; then
echo "Env var '$1' is required. Specify it and restart the container."
exit 1
fi
}

# Method that accepts one argument, the name of the environment variable to inject.
# Will substitute said variable as a literal string
inject() {
if [ -z "$1" ] && [ -z "$2" ]; then
echo "[$NAME] Usage: inject <variable> <value>"
fi

# Ensure the environment variable has a value specified.
require $1

echo "[$NAME] Injecting variable: $1 = $2"
find $SERVE_PATH -name "*.js" -exec sed -i "s|$1|$2|g" {} +
}

# Inject our variables.
inject SHOCKLINK_API_URL $SHOCKLINK_API_URL
inject SHOCKLINK_WEBUI_URL $SHOCKLINK_WEBUI_URL
inject SHOCKLINK_SHARE_URL $SHOCKLINK_SHARE_URL

# Start nginx as normal.
# If something breaks, see: https://github.com/nginxinc/docker-nginx/blob/master/mainline/debian/Dockerfile
echo "[$NAME] Starting nginx"
./docker-entrypoint.sh "$@"