From 3c3083f991dfe756506a1eb7700bdfa4e1484f2f Mon Sep 17 00:00:00 2001
From: Paul Pestov <pestov@sub.uni-goettingen.de>
Date: Wed, 21 Jul 2021 16:43:41 +0200
Subject: [PATCH] feat: add env variables for URLs

BREAKING CHANGE: Pass the base URL of API server to the ApiService class and inject the viewer base
URL at search result list items instead of hardcoded strings.

closes #75
---
 .env                                          |  2 ++
 .gitignore                                    |  1 +
 package-lock.json                             | 17 +++++++++++++++++
 package.json                                  |  1 +
 readme.md                                     |  4 +++-
 src/.vuepress/api/api.js                      |  2 +-
 src/.vuepress/components/SearchResultList.vue |  2 +-
 src/.vuepress/config.js                       | 12 +++++++++---
 8 files changed, 35 insertions(+), 6 deletions(-)
 create mode 100644 .env

diff --git a/.env b/.env
new file mode 100644
index 0000000..bf1eeda
--- /dev/null
+++ b/.env
@@ -0,0 +1,2 @@
+VUE_APP_BASE_URL_API = http://localhost
+VUE_APP_BASE_URL_VIEWER = http://localhost
diff --git a/.gitignore b/.gitignore
index 540690c..29ebe5b 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,4 @@
 node_modules
 .vscode/settings.json
 .DS_Store
+.env.local
diff --git a/package-lock.json b/package-lock.json
index e61963a..5ab6376 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -6086,6 +6086,23 @@
         "is-obj": "^2.0.0"
       }
     },
+    "dotenv-flow": {
+      "version": "3.2.0",
+      "resolved": "https://registry.npmjs.org/dotenv-flow/-/dotenv-flow-3.2.0.tgz",
+      "integrity": "sha512-GEB6RrR4AbqDJvNSFrYHqZ33IKKbzkvLYiD5eo4+9aFXr4Y4G+QaFrB/fNp0y6McWBmvaPn3ZNjIufnj8irCtg==",
+      "dev": true,
+      "requires": {
+        "dotenv": "^8.0.0"
+      },
+      "dependencies": {
+        "dotenv": {
+          "version": "8.6.0",
+          "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-8.6.0.tgz",
+          "integrity": "sha512-IrPdXQsk2BbzvCBGBOTmmSH5SodmqZNt4ERAZDmW4CT+tL8VtvinqywuANaFu4bOMWki16nqf0e4oC0QIaDr/g==",
+          "dev": true
+        }
+      }
+    },
     "dotgitignore": {
       "version": "2.1.0",
       "resolved": "https://registry.npmjs.org/dotgitignore/-/dotgitignore-2.1.0.tgz",
diff --git a/package.json b/package.json
index ddb7440..965ba02 100755
--- a/package.json
+++ b/package.json
@@ -19,6 +19,7 @@
     "axios": "^0.21.1",
     "commitizen": "^4.2.4",
     "cz-conventional-changelog": "^3.3.0",
+    "dotenv-flow": "^3.2.0",
     "husky": "^6.0.0",
     "markdown-it-attrs": "^4.0.0",
     "material-design-icons-iconfont": "^6.1.0",
diff --git a/readme.md b/readme.md
index e524ce4..72b8328 100644
--- a/readme.md
+++ b/readme.md
@@ -3,7 +3,9 @@
 ## Get it running
 - `$ git clone git@gitlab.gwdg.de:subugoe/ahiqar/website.git`
 - `$ nvm use` *to make sure you use the required node version*
-- `$ npm i` *to install al dependencies*
+- `$ npm i` *to install all dependencies*
+- Create `.env.local` file and copy everything from `.env` file
+- Replace your desired values in `.env.local` file
 - `$ npm run dev` *to get a running (locally) website at [`http://localhost:8080/`](http://localhost:8080/)*
 
 ## Edit Content
diff --git a/src/.vuepress/api/api.js b/src/.vuepress/api/api.js
index 7da44cb..f9d9ed9 100644
--- a/src/.vuepress/api/api.js
+++ b/src/.vuepress/api/api.js
@@ -36,4 +36,4 @@ class ApiService {
   }
 }
 
-export default new ApiService('https://ahikar-dev.sub.uni-goettingen.de/api');
+export default new ApiService(process.env.VUE_APP_BASE_URL_API);
diff --git a/src/.vuepress/components/SearchResultList.vue b/src/.vuepress/components/SearchResultList.vue
index 0d9591c..57b751e 100644
--- a/src/.vuepress/components/SearchResultList.vue
+++ b/src/.vuepress/components/SearchResultList.vue
@@ -43,7 +43,7 @@ export default {
   },
   computed: {
     viewerBaseUrl() {
-      return 'https://ahikar-dev.sub.uni-goettingen.de';
+      return process.env.VUE_APP_BASE_URL_VIEWER;
     },
   },
   props: {
diff --git a/src/.vuepress/config.js b/src/.vuepress/config.js
index fccfc43..4e27837 100755
--- a/src/.vuepress/config.js
+++ b/src/.vuepress/config.js
@@ -1,4 +1,6 @@
-const { description } = require('../../package')
+const { description } = require('../../package');
+const webpack = require('webpack');
+require('dotenv-flow').config();
 
 module.exports = {
   base: '/',
@@ -39,8 +41,7 @@ module.exports = {
     }
   },
 
-  plugins: [
-  ],
+  plugins: [],
   markdown: {
     // options for markdown-it-anchor
     anchor: { permalink: false },
@@ -49,5 +50,10 @@ module.exports = {
     extendMarkdown: md => {
       md.use(require('markdown-it-attrs'))
     }
+  },
+  configureWebpack: (config) => {
+    return { plugins: [
+        new webpack.EnvironmentPlugin({...process.env})
+      ]}
   }
 }
-- 
GitLab