#228 running dbgate on subpath

This commit is contained in:
Jan Prochazka 2022-02-24 14:42:37 +01:00
parent 07c7a0405a
commit 7112f930ae
5 changed files with 24 additions and 31 deletions

View File

@ -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

View File

@ -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}`);

View File

@ -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;

View File

@ -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);
}
};

View File

@ -9,7 +9,7 @@ export default function resolveApi() {
if (apiUrl) {
return apiUrl;
}
return window.location.origin;
return window.location.href.replace(/\/*$/, '');
}
export function resolveApiHeaders() {