From 25858a61c33df3a4746b2915450e3f97b9a98bc5 Mon Sep 17 00:00:00 2001
From: Richard Petersen <richard.petersen@open-xchange.com>
Date: Tue, 8 Feb 2022 17:03:28 +0100
Subject: [PATCH] Refactor configuration

Fixes #6
---
 .gitlab-ci/values.yaml                        |  4 +--
 .../templates/configMap.yaml                  |  6 ++--
 .../templates/deployment.yaml                 |  2 +-
 helm/core-manifest-service/values.yaml        |  2 +-
 helm/values/develop.yaml                      |  2 +-
 spec/file-depencies_test.js                   |  8 ++---
 spec/file_caching_test.js                     |  6 ++--
 spec/headers_test.js                          |  8 ++---
 spec/meta_test.js                             |  6 ++--
 spec/server_test.js                           | 35 +++++++++++--------
 src/config.js                                 |  4 +--
 src/manifests.js                              |  9 +++--
 12 files changed, 48 insertions(+), 44 deletions(-)

diff --git a/.gitlab-ci/values.yaml b/.gitlab-ci/values.yaml
index 1337231..3b6a056 100644
--- a/.gitlab-ci/values.yaml
+++ b/.gitlab-ci/values.yaml
@@ -2,8 +2,8 @@ replicaCount: 1
 
 containerPort: 8080
 
-manifests:
-  - http://main-core-ui.appsuite-stack-1494-main.svc.cluster.local/manifest.json
+baseUrls:
+  - http://main-core-ui.appsuite-stack-1494-main.svc.cluster.local
 
 ingress:
   enabled: false
diff --git a/helm/core-manifest-service/templates/configMap.yaml b/helm/core-manifest-service/templates/configMap.yaml
index 94bcef7..c6c6a7d 100644
--- a/helm/core-manifest-service/templates/configMap.yaml
+++ b/helm/core-manifest-service/templates/configMap.yaml
@@ -4,7 +4,7 @@ kind: ConfigMap
 metadata:
   name: {{ include "ox-common.names.fullname" . }}
 data:
-  urls.yaml: |
-    manifests:
-      {{- toYaml .Values.manifests | nindent 6 }}
+  config.yaml: |
+    baseUrls:
+      {{- toYaml .Values.baseUrls | nindent 6 }}
 {{- end }}
diff --git a/helm/core-manifest-service/templates/deployment.yaml b/helm/core-manifest-service/templates/deployment.yaml
index dd7d259..a3f173a 100644
--- a/helm/core-manifest-service/templates/deployment.yaml
+++ b/helm/core-manifest-service/templates/deployment.yaml
@@ -49,7 +49,7 @@ spec:
             {{- toYaml .Values.resources | nindent 12 }}
           volumeMounts:
             - name: manifest-config
-              mountPath: /app/config/manifests
+              mountPath: /app/config/
       volumes:
         - name: manifest-config
           configMap:
diff --git a/helm/core-manifest-service/values.yaml b/helm/core-manifest-service/values.yaml
index 83eb51d..ff2ea44 100644
--- a/helm/core-manifest-service/values.yaml
+++ b/helm/core-manifest-service/values.yaml
@@ -101,5 +101,5 @@ probe:
 
 cacheTTL: 30000
 logLevel: info
-manifests: []
+baseUrls: []
 appRoot: '/'
diff --git a/helm/values/develop.yaml b/helm/values/develop.yaml
index 7fe143d..dd0afd7 100644
--- a/helm/values/develop.yaml
+++ b/helm/values/develop.yaml
@@ -1,6 +1,6 @@
 replicaCount: 1
 containerPort: 8080
-manifests:
+baseUrls:
   - https://manifest-service-dummy.k3s.os.oxui.de/manifest.json
 ingress:
   enabled: true
diff --git a/spec/file-depencies_test.js b/spec/file-depencies_test.js
index 15a619f..f3b4f21 100644
--- a/spec/file-depencies_test.js
+++ b/spec/file-depencies_test.js
@@ -11,9 +11,9 @@ describe('JS files with dependencies contain events', () => {
 
   beforeAll(() => {
     mockfs({
-      './config/manifests': {
-        'urls.yaml': `manifests:
-          - http://localhost:${port}/api/manifest.json`
+      './config': {
+        'config.yaml': `baseUrls:
+          - http://localhost:${port}`
       }
     })
     app = createApp()
@@ -26,7 +26,7 @@ describe('JS files with dependencies contain events', () => {
   beforeEach(async () => {
     mockserver = await createMockServer({ port })
     mockserver.respondWith({
-      '/api/manifest.json': generateSimpleViteManifest({
+      '/manifest.json': generateSimpleViteManifest({
         'example.js': {},
         'main.css': {},
         'index.html': {
diff --git a/spec/file_caching_test.js b/spec/file_caching_test.js
index 65359a8..0fd09b8 100644
--- a/spec/file_caching_test.js
+++ b/spec/file_caching_test.js
@@ -15,9 +15,9 @@ describe('File caching service', () => {
 
   beforeAll(() => {
     mockfs({
-      './config/manifests': {
-        'urls.yaml': `manifests:
-          - http://localhost:${port}/manifest.json`
+      './config': {
+        'config.yaml': `baseUrls:
+          - http://localhost:${port}/`
       }
     })
     app = createApp()
diff --git a/spec/headers_test.js b/spec/headers_test.js
index 93f572c..e923357 100644
--- a/spec/headers_test.js
+++ b/spec/headers_test.js
@@ -11,9 +11,9 @@ describe('Responses contain custom headers', () => {
 
   beforeAll(() => {
     mockfs({
-      './config/manifests': {
-        'urls.yaml': `manifests:
-          - http://localhost:${port}/api/manifest.json`
+      './config': {
+        'config.yaml': `baseUrls:
+          - http://localhost:${port}`
       }
     })
     app = createApp()
@@ -26,7 +26,7 @@ describe('Responses contain custom headers', () => {
   beforeEach(async () => {
     mockserver = await createMockServer({ port })
     mockserver.respondWith({
-      '/api/manifest.json': generateSimpleViteManifest({
+      '/manifest.json': generateSimpleViteManifest({
         'example.js': {},
         'main.css': {},
         'index.html': {
diff --git a/spec/meta_test.js b/spec/meta_test.js
index c866b84..42b6f1c 100644
--- a/spec/meta_test.js
+++ b/spec/meta_test.js
@@ -11,9 +11,9 @@ describe('Responses contain custom headers', () => {
 
   beforeAll(() => {
     mockfs({
-      './config/manifests': {
-        'urls.yaml': `manifests:
-          - http://localhost:${port}/manifest.json`
+      './config': {
+        'config.yaml': `baseUrls:
+          - http://localhost:${port}`
       }
     })
     app = createApp()
diff --git a/spec/server_test.js b/spec/server_test.js
index 6079386..662b28b 100644
--- a/spec/server_test.js
+++ b/spec/server_test.js
@@ -6,14 +6,15 @@ import { createMockServer, generateSimpleViteManifest, getRandomPort } from './u
 
 describe('Manifest service', () => {
   let app
-  let mockserver
+  let mockserver, mockserver2
   const port = getRandomPort()
+  const port2 = getRandomPort()
 
   beforeAll(() => {
     mockfs({
-      './config/manifests': {
-        'urls.yaml': `manifests:
-          - http://localhost:${port}/api/manifest.json`
+      './config': {
+        'config.yaml': `baseUrls:
+          - http://localhost:${port}`
       }
     })
     app = createApp()
@@ -26,13 +27,14 @@ describe('Manifest service', () => {
   beforeEach(async () => {
     mockserver = await createMockServer({ port })
     mockserver.respondWith({
-      '/api/manifest.json': generateSimpleViteManifest({ 'example.js': 'test' }),
+      '/manifest.json': generateSimpleViteManifest({ 'example.js': 'test' }),
       '/example.js': ''
     })
   })
 
   afterEach(() => {
-    mockserver.close()
+    mockserver?.close()
+    mockserver2?.close()
     process.env.CACHE_TTL = 30000
   })
 
@@ -58,7 +60,7 @@ describe('Manifest service', () => {
     mockserver.close()
     mockserver = await createMockServer({ port })
     mockserver.respondWith({
-      '/api/manifest.json': generateSimpleViteManifest({ 'example.js': 'other' }),
+      '/manifest.json': generateSimpleViteManifest({ 'example.js': 'other' }),
       '/example.js': ''
     })
 
@@ -80,7 +82,7 @@ describe('Manifest service', () => {
     mockserver.close()
     mockserver = await createMockServer({ port })
     mockserver.respondWith({
-      '/api/manifest.json': generateSimpleViteManifest({ 'example.js': 'other' }),
+      '/manifest.json': generateSimpleViteManifest({ 'example.js': 'other' }),
       '/example.js': ''
     })
 
@@ -94,19 +96,22 @@ describe('Manifest service', () => {
 
   it('can load multiple configurations', async () => {
     mockfs({
-      './config/manifests': {
-        'urls.yaml': `manifests:
-          - http://localhost:${port}/api/manifest.json
-          - http://localhost:${port}/api/no2/manifest.json`
+      './config': {
+        'config.yaml': `baseUrls:
+          - http://localhost:${port}
+          - http://localhost:${port2}`
       }
     })
 
     mockserver.close()
     mockserver = await createMockServer({ port })
     mockserver.respondWith({
-      '/api/manifest.json': generateSimpleViteManifest({ 'example1.js': 'other' }),
-      '/api/no2/manifest.json': generateSimpleViteManifest({ 'example2.js': 'thing' }),
-      '/example1.js': '',
+      '/manifest.json': generateSimpleViteManifest({ 'example1.js': 'other' }),
+      '/example1.js': ''
+    })
+    mockserver2 = await createMockServer({ port: port2 })
+    mockserver2.respondWith({
+      '/manifest.json': generateSimpleViteManifest({ 'example2.js': 'thing' }),
       '/example2.js': ''
     })
 
diff --git a/src/config.js b/src/config.js
index 2747ac6..b6e21d9 100644
--- a/src/config.js
+++ b/src/config.js
@@ -3,8 +3,8 @@ import yaml from 'js-yaml'
 
 class Config {
   async load () {
-    const urlsSource = await fs.readFile('./config/manifests/urls.yaml', 'utf8')
-    this._urls = yaml.load(urlsSource).manifests
+    const urlsSource = await fs.readFile('./config/config.yaml', 'utf8')
+    this._urls = yaml.load(urlsSource).baseUrls
   }
 
   get urls () {
diff --git a/src/manifests.js b/src/manifests.js
index c5d6545..cc608c6 100644
--- a/src/manifests.js
+++ b/src/manifests.js
@@ -15,17 +15,16 @@ export const loadViteManifests = (() => {
     await config.load()
     // vite manifests contains a set of objects with the vite-manifests
     // from the corresponding registered services
-    const viteManifests = await Promise.all(config.urls.map(async url => {
-      const { origin } = new URL(url)
+    const viteManifests = await Promise.all(config.urls.map(async baseUrl => {
       // fetch the manifests
-      const result = await fetch(url)
+      const result = await fetch(new URL('/manifest.json', baseUrl))
       if (!result.ok) throw new Error(`Failed to load manifest for url ${result.url} (Status: ${result.status}: ${result.statusText})`)
       try {
         const manifest = await result.json()
         for (const file in manifest) {
-          logger.debug(`retrieved ${file} from ${url}`)
+          logger.debug(`retrieved ${file} from ${baseUrl}`)
           manifest[file].meta = manifest[file].meta || {}
-          manifest[file].meta.baseUrl = origin
+          manifest[file].meta.baseUrl = baseUrl
         }
         return manifest
       } catch (err) {
-- 
GitLab