Skip to content

Commit

Permalink
Merge pull request #26 from trenz-gmbh/feature/capsule-config
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardoboss authored Oct 19, 2022
2 parents ea3d9e2 + 6964fa2 commit 1fcf997
Show file tree
Hide file tree
Showing 18 changed files with 241 additions and 54 deletions.
3 changes: 3 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
**/.git
.github/
node_modules/
.env.local
*.local.json
Dockerfile*
README.md
5 changes: 0 additions & 5 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,5 +0,0 @@
VUE_APP_API_BASE=https://localhost:7262/api/
VUE_APP_PRIMARY_COLOR='#8af'
VUE_APP_PRIMARY_FOREGROUND_COLOR='#000'
VUE_APP_NAME='My Wiki'
VUE_APP_USE_AUTH=false
39 changes: 39 additions & 0 deletions .github/scripts/docker-image-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/bin/bash

# exit on error
set -e

# copy webapp settings
cp public/webapp-settings.example.json webapp-settings.local.json

# start server
docker-compose up -d

# test if the server is running and serves files
requested_settings=$(curl http://localhost:5000/webapp-settings.json)

# compare served file with actual file (a 404 or 50x error - or a timeout - would fail here)
actual_settings=$(cat webapp-settings.local.json)
if [ "$actual_settings" != "$requested_settings" ]; then
echo "Could not verify server functionality. Received settings:"
echo "$requested_settings"
exit 1
fi

echo "Received settings:"
echo "$requested_settings"

# test if homepage gets served without errors
requested_homepage=$(curl http://localhost:5000/)
echo "Received homepage:"
echo "$requested_homepage"

echo "Server seems to respond correctly."

# shutdown server
docker-compose down

# cleanup
rm webapp-settings.local.json

exit 0
41 changes: 41 additions & 0 deletions .github/workflows/docker-image.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: Docker Image CI

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

jobs:
build:
name: Build Docker Image

runs-on: ubuntu-latest

permissions:
packages: write

steps:
- uses: actions/checkout@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Login to GitHub Container Registry
uses: docker/login-action@v2
if: github.event_name != 'pull_request'
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Test docker-compose.yml
run: chmod +x ./.github/scripts/docker-image-test.sh && ./.github/scripts/docker-image-test.sh
shell: bash
- name: Build and push image
uses: docker/build-push-action@v3
if: github.event_name != 'pull_request'
with:
context: .
platforms: linux/amd64
push: true
tags: |
ghcr.io/${{ github.repository }}:latest
ghcr.io/${{ github.repository }}:${{ github.sha }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ node_modules
.env.local
.env.*.local
appsettings.local.json
public/webapp-settings.json

# Log files
npm-debug.log*
Expand Down
5 changes: 1 addition & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@ FROM node:lts-alpine as build

WORKDIR /app

ARG VUE_APP_API_BASE
ENV VUE_APP_API_BASE $VUE_APP_API_BASE

# add node binaries to path
ENV PATH /app/node_modules/.bin:$PATH

# install dependencies and build tools
COPY package*.json ./
RUN npm install --silent
RUN npm ci

# build app
COPY . .
Expand Down
74 changes: 61 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,73 @@
[![Node.js CI](https://github.com/trenz-gmbh/trenz-docs/actions/workflows/node.js.yml/badge.svg)](https://github.com/trenz-gmbh/trenz-docs/actions/workflows/node.js.yml)

## Project setup

```
npm install
```

### Compiles and hot-reloads for development

```
npm run serve
```

### Compiles and minifies for production

```
npm run build
```

### Lints and fixes files

```
npm run lint
```

### Customize configuration
See [Configuration Reference](https://cli.vuejs.org/config/).

## Deployment

To deploy a trenz-docs wiki, follow these steps:

1. clone this repository to the target server:
```bash
git clone https://github.com/trenz-gmbh/trenz-docs my-wiki
cd my-wiki
1. create a `docker-compose.yml` or copy the one from this repository and replace the ports:
```docker-compose.yml
version: '3.4'
services:
frontend:
image: ghcr.io/trenz-gmbh/trenz-docs:latest
ports:
- "<FRONTEND PORT>:80"
restart: unless-stopped
depends_on:
- api
volumes:
- ./webapp-settings.local.json:/usr/share/nginx/html/webapp-settings.json
api:
image: ghcr.io/trenz-gmbh/trenz-docs-api:latest
environment:
- ASPNETCORE_ENVIRONMENT=${ASPNETCORE_ENVIRONMENT:-Production}
- Meilisearch__Url=http://meilisearch:7700
- Meilisearch__ApiKey=${MEILISEARCH_API_KEY:-masterKey}
ports:
- "<API PORT>:80"
restart: unless-stopped
depends_on:
- meilisearch
volumes:
- ./appsettings.local.json:/app/appsettings.local.json
meilisearch:
image: getmeili/meilisearch:latest
ports:
- "7700:7700"
environment:
- MEILI_MASTER_KEY=${MEILISEARCH_API_KEY:-masterKey}
- MEILI_NO_ANALYTICS=true
restart: unless-stopped
volumes:
- meili_data:/data
volumes:
meili_data:
```

2. add a `appsettings.local.json` file to specify which repositories to use as sources:
Expand All @@ -50,16 +87,27 @@ To deploy a trenz-docs wiki, follow these steps:
}
```

3. optionally: add a `.env` (or `.env.local`) file:
```env
VUE_APP_API_BASE=https://localhost:7262/api/
VUE_APP_PRIMARY_COLOR='#8af'
VUE_APP_PRIMARY_FOREGROUND_COLOR='#000'
3. add a `webapp-settings.local.json` and add your customer-facing api endpoint:
```json
{
"name": "<YOUR DOCS NAME>",
"theme": {
"primary": "80,120,200",
"primary-foreground": "255,255,255"
},
"api": {
"baseUrl": "https://<YOUR API DOMAIN>/api/"
},
"useAuth": false
}
```

4. run:
```bash
docker-compose up -d
```

5. access your new wiki at [localhost:5050](http://localhost:5050/)
5. access your new wiki hosted at:
```bash
http://localhost:<FRONTEND PORT>
```
4 changes: 2 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ services:
build:
context: .
target: final
args:
- VUE_APP_API_BASE=http://localhost:5001/api/
ports:
- "5000:80"
restart: unless-stopped
depends_on:
- api
volumes:
- ./webapp-settings.local.json:/usr/share/nginx/html/webapp-settings.json
api:
image: ghcr.io/trenz-gmbh/trenz-docs-api:latest
environment:
Expand Down
11 changes: 11 additions & 0 deletions public/webapp-settings.example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "My Docs",
"theme": {
"primary": "80,120,200",
"primary-foreground": "255,255,255"
},
"api": {
"baseUrl": "https://localhost:7262/api/"
},
"useAuth": false
}
15 changes: 12 additions & 3 deletions src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,14 @@ import {defineComponent} from 'vue'
import NavTreeNode from "@/components/NavTreeNode.vue";
import {VTextField} from "vuetify/components";
import TrenzDocsLogo from "@/components/TrenzDocsLogo.vue";
import ApiClient from "@/api/ApiClient";
import {mapGetters} from "vuex";
import * as api from "@/api";
export default defineComponent({
name: 'App',
inject: ['$settings'],
components: {
TrenzDocsLogo,
NavTreeNode,
Expand All @@ -151,6 +152,14 @@ export default defineComponent({
});
},
mounted() {
let allThemed = document.querySelectorAll<HTMLElement>('.v-theme--light, .v-theme--dark');
allThemed.forEach(el => {
el.style.setProperty('--v-theme-primary', this.$settings.theme.primary);
el.style.setProperty('--v-theme-on-primary', this.$settings.theme["primary-foreground"]);
})
},
unmounted() {
window.removeEventListener('keydown', this.handleKeyDown)
},
Expand Down Expand Up @@ -255,11 +264,11 @@ export default defineComponent({
},
loginUrl() {
return ApiClient.getBaseUrl() + "auth/transfer?returnUrl=" + encodeURI(window.location.origin + this.$route.fullPath);
return this.$settings.api.baseUrl + "auth/transfer?returnUrl=" + encodeURI(window.location.origin + this.$route.fullPath);
},
logoutUrl() {
return ApiClient.getBaseUrl() + "auth/signout?returnUrl=" + encodeURI(window.location.origin + this.$route.fullPath);
return this.$settings.api.baseUrl + "auth/signout?returnUrl=" + encodeURI(window.location.origin + this.$route.fullPath);
},
},
})
Expand Down
11 changes: 11 additions & 0 deletions src/WebappSettings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export interface WebappSettings {
name: string;
theme: {
primary: string;
'primary-foreground': string;
}
api: {
baseUrl: string;
}
useAuth: boolean;
}
28 changes: 9 additions & 19 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,15 @@ import App from './App.vue'
import router from './router'
import store from './store'
import vuetify from './plugins/vuetify'
import {loadFonts} from './plugins/webfontloader'
import ApiClient from "@/api/ApiClient";

if (process.env.VUE_APP_API_BASE) {
ApiClient.setBaseUrl(process.env.VUE_APP_API_BASE);
} else {
alert('Please set the VUE_APP_API_BASE environment variable (e.g. VUE_APP_API_BASE=https://localhost:7262/api)');

throw new Error('VUE_APP_API_BASE is not set');
}

if (process.env.VUE_APP_USE_AUTH && process.env.VUE_APP_USE_AUTH === 'true') {
ApiClient.useAuth = true;
}
import { loadFonts } from './plugins/webfontloader'

loadFonts()

createApp(App)
.use(router)
.use(store)
.use(vuetify)
.mount('#app')
store.dispatch('loadWebAppSettings').then(settings => {
createApp(App)
.provide('$settings', settings)
.use(router)
.use(store)
.use(vuetify)
.mount('#app')
})
10 changes: 6 additions & 4 deletions src/plugins/vuetify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,19 @@ export default createVuetify({
theme: {
defaultTheme: 'light',
themes: {
// The primary and on-primary values get overridden by the values in public/webapp-settings.json at runtime.
// They may be visible until the settings got loaded and applied.
light: {
colors: {
primary: process.env.VUE_APP_PRIMARY_COLOR ?? '#ddd',
'on-primary': process.env.VUE_APP_PRIMARY_FOREGROUND_COLOR ?? '#000',
primary: '#ddd',
'on-primary': '#000',
}
} as ThemeDefinition,
dark: {
dark: true,
colors: {
primary: process.env.VUE_APP_PRIMARY_COLOR ?? '#222',
'on-primary': process.env.VUE_APP_PRIMARY_FOREGROUND_COLOR ?? '#fff',
primary: '#222',
'on-primary': '#fff',
}
} as ThemeDefinition,
},
Expand Down
Loading

0 comments on commit 1fcf997

Please sign in to comment.