diff --git a/docker-compose.yaml b/docker-compose.yaml index 605e2eb6..b19f3aa2 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -4,7 +4,7 @@ version: "3" services: dbgate: build: docker - # image: dbgate/dbgate:beta + # image: dbgate/dbgate:beta-alpine restart: always ports: - 3100:3000 @@ -13,6 +13,9 @@ services: volumes: - dbgate-data:/root/dbgate-data + + # environment: + # WEB_ROOT: /dbgate # volumes: # - /home/jena/test/chinook:/mnt/sqt diff --git a/packages/api/src/main.js b/packages/api/src/main.js index 2ad5588d..0a08bb37 100644 --- a/packages/api/src/main.js +++ b/packages/api/src/main.js @@ -28,8 +28,7 @@ const queryHistory = require('./controllers/queryHistory'); const { rundir } = require('./utility/directories'); const platformInfo = require('./utility/platformInfo'); - -let checkLocalhostOrigin = null; +const getExpressPath = require('./utility/getExpressPath'); function start() { // console.log('process.argv', process.argv); @@ -50,29 +49,9 @@ function start() { ); } - app.use(function (req, res, next) { - if (checkLocalhostOrigin) { - if ( - req.headers.origin && - req.headers.origin != checkLocalhostOrigin && - req.headers.origin != `http://${checkLocalhostOrigin}` - ) { - console.log('API origin check FAILED'); - console.log('HEADERS', { ...req.headers, authorization: '***' }); - return res.status(403).json({ error: 'Not authorized!' }); - } - if (!req.headers.origin && req.headers.host != checkLocalhostOrigin) { - console.log('API host check FAILED'); - console.log('HEADERS', { ...req.headers, authorization: '***' }); - return res.status(403).json({ error: 'Not authorized!' }); - } - } - next(); - }); - app.use(cors()); - app.get('/stream', async function (req, res) { + app.get(getExpressPath('/stream'), async function (req, res) { res.set({ 'Cache-Control': 'no-cache', 'Content-Type': 'text/event-stream', @@ -88,7 +67,7 @@ function start() { app.use(bodyParser.json({ limit: '50mb' })); app.use( - '/uploads', + getExpressPath('/uploads'), fileUpload({ limits: { fileSize: 4 * 1024 * 1024 }, }) @@ -100,21 +79,21 @@ function start() { // app.use('/pages', express.static(process.env.PAGES_DIRECTORY)); // } - app.use('/runners/data', express.static(rundir())); + app.use(getExpressPath('/runners/data'), express.static(rundir())); if (platformInfo.isDocker) { // server static files inside docker container - app.use(express.static('/home/dbgate-docker/public')); + app.use(getExpressPath('/'), express.static('/home/dbgate-docker/public')); } else { if (!platformInfo.isNpmDist) { - app.get('/', (req, res) => { + app.get(getExpressPath('/'), (req, res) => { res.send('DbGate API'); }); } } if (platformInfo.isNpmDist) { - app.use(express.static(path.join(__dirname, '../../dbgate-web/public'))); + app.use(getExpressPath('/'), express.static(path.join(__dirname, '../../dbgate-web/public'))); getPort({ port: 5000 }).then(port => { server.listen(port, () => { console.log(`DbGate API listening on port ${port}`); diff --git a/packages/api/src/utility/getExpressPath.js b/packages/api/src/utility/getExpressPath.js new file mode 100644 index 00000000..8f237bee --- /dev/null +++ b/packages/api/src/utility/getExpressPath.js @@ -0,0 +1,10 @@ +function getExpressPath(path) { + path = path.replace(/\/*$/, '').replace(/^\/*/, ''); + const root = (process.env.WEB_ROOT || '').replace(/^\/*/, '').replace(/\/*$/, ''); + if (root) { + return `/${root}/${path}`; + } + return `/${path}`; +} + +module.exports = getExpressPath; diff --git a/packages/api/src/utility/useController.js b/packages/api/src/utility/useController.js index 877b8a33..ee891b61 100644 --- a/packages/api/src/utility/useController.js +++ b/packages/api/src/utility/useController.js @@ -1,5 +1,6 @@ const _ = require('lodash'); const express = require('express'); +const getExpressPath = require('./getExpressPath'); /** * @param {string} route @@ -74,6 +75,6 @@ module.exports = function useController(app, electron, route, controller) { } if (app) { - app.use(route, router); + app.use(getExpressPath(route), router); } }; diff --git a/packages/web/src/utility/resolveApi.ts b/packages/web/src/utility/resolveApi.ts index 6ad8ed30..e15506e6 100644 --- a/packages/web/src/utility/resolveApi.ts +++ b/packages/web/src/utility/resolveApi.ts @@ -9,7 +9,7 @@ export default function resolveApi() { if (apiUrl) { return apiUrl; } - return window.location.origin; + return window.location.href.replace(/\/*$/, ''); } export function resolveApiHeaders() {