瀏覽代碼

Add `config.js` file modding support

Sergiu Miclea 6 年之前
父節點
當前提交
e12ddaf5e8
共有 7 個文件被更改,包括 58 次插入10 次删除
  1. 1 3
      Dockerfile
  2. 2 0
      README.md
  3. 43 0
      server/api/ConfigApi.js
  4. 1 1
      server/api/LogosApi.js
  5. 2 6
      server/api/router.js
  6. 1 0
      src/components/atoms/EndpointLogos/EndpointLogos.jsx
  7. 8 0
      ui-mod-sample.json

+ 1 - 3
Dockerfile

@@ -1,7 +1,5 @@
 FROM node:12.5.0
 
-RUN curl -o- -L https://yarnpkg.com/install.sh | bash
-
 WORKDIR /usr/src/app
 COPY . .
 
@@ -10,4 +8,4 @@ RUN yarn build
 
 ENTRYPOINT [ "node", "server.js" ]
 
-EXPOSE 3000
+EXPOSE 3000

+ 2 - 0
README.md

@@ -42,3 +42,5 @@ The path to the .json mod file needs to be set in `MOD_JSON` environment variabl
 Any provider logos can be replaced using local logo images. The local image file paths need to be absolute.
 
 You can specify one logo, in which case it will be scaled to all sizes. You can also specify logos for just a couple of the sizes, in which case the closest size to the one required will be used. Open [`ui-mod-sample.json`](ui-mod-sample.json) for more details.
+
+Any option from [`config.js`](config.js) can be modified by adding the `config` field to the [`ui-mod-sample.json`](ui-mod-sample.json) file.

+ 43 - 0
server/api/ConfigApi.js

@@ -0,0 +1,43 @@
+/*
+Copyright (C) 2019  Cloudbase Solutions SRL
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU Affero General Public License as
+published by the Free Software Foundation, either version 3 of the
+License, or (at your option) any later version.
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU Affero General Public License for more details.
+You should have received a copy of the GNU Affero General Public License
+along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+// @flow
+
+import path from 'path'
+import fs from 'fs'
+import requireWithoutCache from 'require-without-cache'
+
+import type { Config } from '../../src/types/Config'
+
+export default (router: express$Router) => {
+  // $FlowIgnore
+  router.get('/config', (req, res) => {
+    let configPath = path.join(__dirname, '../../config.js')
+    let config: Config = requireWithoutCache(configPath, require).config
+    let modJsonPath: ?string = process.env.MOD_JSON
+    if (!modJsonPath) {
+      res.send(config)
+      return
+    }
+    try {
+      let jsonContent = fs.readFileSync(modJsonPath)
+      let configMod = JSON.parse(jsonContent).config
+      Object.keys(configMod).forEach(key => { config[key] = configMod[key] })
+      res.send(config)
+    } catch (err) {
+      console.error(err)
+      res.status(400).json({ error: { message: 'Invalid MOD_JSON file' } })
+    }
+  })
+}

+ 1 - 1
server/api/LogosApi.js

@@ -104,7 +104,7 @@ export default (router: express$Router) => {
       res.sendFile(modLogoPath)
     } catch (err) {
       console.error(err)
-      res.status(500).json({ error: { message: 'Invalid Mod JSON file' } })
+      res.status(400).json({ error: { message: 'Invalid Mod JSON file' } })
     }
   })
 }

+ 2 - 6
server/api/router.js

@@ -14,12 +14,12 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 // @flow
 
-import requireWithoutCache from 'require-without-cache'
 import express from 'express'
 import bodyParser from 'body-parser'
 
 import LogosApi from './LogosApi'
 import DownloadZipApi from './DownloadZipApi'
+import ConfigApi from './ConfigApi'
 
 import packageJson from '../../package.json'
 
@@ -32,11 +32,7 @@ router.get('/version', (req, res) => {
   res.json({ version: packageJson.version })
 })
 
-// $FlowIgnore
-router.get('/config', (req, res) => {
-  res.send(requireWithoutCache('../../config.js', require).config)
-})
-
+ConfigApi(router)
 DownloadZipApi(router)
 LogosApi(router)
 

+ 1 - 0
src/components/atoms/EndpointLogos/EndpointLogos.jsx

@@ -28,6 +28,7 @@ const Logo = styled.div`
   width: ${props => props.width}px;
   height: ${props => props.height}px;
   ${props => props.url ? css`background: url('${props.url}') no-repeat center;` : ''}
+  background-size: contain;
 `
 const widthHeights = [
   { w: 80, h: 32 },

+ 8 - 0
ui-mod-sample.json

@@ -1,4 +1,12 @@
 {
+  "config": {
+    "_comment": "Add any `config.js` property here to override it.",
+    "disabledPages": [
+      "planning",
+      "users",
+      "projects"
+    ]
+  },
   "providers": {
     "openstack": {
       "logos": {