Skip to content

Commit

Permalink
use config hostname instead of request hostname when updating json
Browse files Browse the repository at this point in the history
  • Loading branch information
ntraut committed Oct 7, 2023
1 parent 28ac8b4 commit 5cc9f39
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 38 deletions.
3 changes: 2 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/app/public/**/*.min.js
/app/public/lib/
/app/public/lib/
/app/public/js/pages/
71 changes: 34 additions & 37 deletions app/routes/routesExtensions.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const url = require('url');
// const url = require('url');

const request = require('request');

const Config = require('../../cfg.json');
// const { createRelativePositionFromTypeIndex } = require('yjs');

module.exports = (app) => {
Expand All @@ -25,57 +27,52 @@ module.exports = (app) => {
if( !source ) {
return res.status(404).send('source must be defined');
}
const thisHostname = Config.hostname;
let sourceHostname, sourcePath;
try {
const url = new URL(source);
sourceHostname = url.origin;
sourcePath = url.pathname;
} catch (err) {
sourceHostname = thisHostname;
sourcePath = source;
}

const thisHostname = req.protocol + '://' + req.get('host');
const sourceHostname =
(new RegExp('^http')).test(source) ?
url.parse(source).protocol + '//' + url.parse(source).host :
req.protocol + '://' + req.get('host');
const sourcePath = url.parse(source).path ? url.parse(source).path : null;
request(sourceHostname + sourcePath, (err, resp, body) => {
try {
if (err) {
throw err;
}

(new Promise((resolve, reject) => {
if( sourceHostname && sourcePath ) {
request(sourceHostname + sourcePath, (err, resp, body) => {
if(err) {
return reject(err);
}
if ((/error/).test(sourcePath)) {
console.log({body, resp, err});
}
if(resp && resp.statusCode >= 400) {
return reject(body);
}
if ((/(error)/).test(sourcePath)) {
console.log({ body });
}

if(resp && resp.statusCode >= 400) {
throw body;
}

return resolve(body);
});
} else {
reject(new Error('ERROR: sourceurl not defined'));
}
}))
.then((body) => {
const json = JSON.parse(body);
json.tileSources = json.tileSources.map((result) => {
let tileSource = result;
if(typeof result === 'string') {
if((new RegExp('^http')).test(tileSource) === false) {
if(tileSource[0] === '/') {
tileSource = thisHostname + '/getTile?source=' + sourceHostname + tileSource;
} else {
tileSource = thisHostname + '/getTile?source=' + sourceHostname + '/' + tileSource;
}
if (typeof result === 'string' && !(/^http/).test(tileSource)) {
if(tileSource[0] === '/') {
tileSource = thisHostname + '/getTile?source=' + sourceHostname + tileSource;
} else {
tileSource = thisHostname + '/getTile?source=' + sourceHostname + '/' + tileSource;
}
}

console.log(tileSource);

return tileSource;
});

res.status(200).send(JSON.stringify(json));
})
.catch((e) => {
console.log('13');
} catch (e) {
console.log('Error at /getJson', e);
res.status(404).send(e);
});
}
});
});
};
2 changes: 2 additions & 0 deletions cfg.json.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{
"hostname": "http://localhost:3000",
"wshostname": "ws://localhost:8080",
"app_port": 3000,
"ws_port": 8080,
"crdt_backend_host": "localhost",
Expand Down

0 comments on commit 5cc9f39

Please sign in to comment.