Skip to content

Commit

Permalink
Merge pull request #17 from elixir-oslo/requires_entry_point_path
Browse files Browse the repository at this point in the history
Added support for requires_path_in_url and requires_path_in_header_named
  • Loading branch information
mvdbeek authored Oct 25, 2023
2 parents 29d7ad0 + 925cfc0 commit c887ccb
Show file tree
Hide file tree
Showing 9 changed files with 272 additions and 136 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"mocha": true
},
"parserOptions": {
"ecmaVersion": 2017
"ecmaVersion": 2020
}
}

8 changes: 4 additions & 4 deletions lib/createdb.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ args
.option("--sessions <file>", "Routes file to monitor")
.option("--verbose");

const main = function(argv_) {
const main = function (argv_) {
const argv = argv_ || process.argv;
args.parse(argv);
let db = new sqlite3.Database(args.sessions, err => {
let db = new sqlite3.Database(args.sessions, (err) => {
if (err) {
return console.error(err.message);
}
Expand All @@ -27,11 +27,11 @@ CREATE TABLE gxitproxy
info text,
PRIMARY KEY (key, key_type)
);`,
err => {
(err) => {
if (err) {
return console.log(err.message);
}
}
},
);
db.close();
};
Expand Down
8 changes: 4 additions & 4 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ args
.option("--forwardPort <n>", "Forward all requests to port", parseInt)
.option(
"--reverseProxy",
"Cause the proxy to rewrite location blocks with its own port"
"Cause the proxy to rewrite location blocks with its own port",
)
.option("--verbose");

const main = function(argv_) {
const main = function (argv_) {
const argv = argv_ || process.argv;
args.parse(argv);

Expand All @@ -35,7 +35,7 @@ const main = function(argv_) {
sessionMap: sessions,
proxyPathPrefix: args.proxyPathPrefix,
verbose: args.verbose,
port: args.port
port: args.port,
};

if (args.reverseProxy) {
Expand All @@ -53,7 +53,7 @@ const main = function(argv_) {
const dynamicProxy = new DynamicProxy(dynamicProxyOptions);
const listen = {
port: args.port || 8000,
ip: args.ip
ip: args.ip,
};
dynamicProxy.listen(listen);
return dynamicProxy;
Expand Down
40 changes: 26 additions & 14 deletions lib/mapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,29 @@ var fs = require("fs");
var sqlite3 = require("sqlite3");
var watch = require("node-watch");

var endsWith = function(subjectString, searchString) {
var endsWith = function (subjectString, searchString) {
var position = subjectString.length;
position -= searchString.length;
var lastIndex = subjectString.indexOf(searchString, position);
return lastIndex !== -1 && lastIndex === position;
};

var updateFromJson = function(path, map) {
var updateFromJson = function (path, map) {
var content = fs.readFileSync(path, "utf8");
var keyToSession = JSON.parse(content);
var newSessions = {};
for (var key in keyToSession) {
var info = keyToSession[key]?.info;
if (info) {
info = JSON.parse(info);
}
newSessions[key] = {
target: {
host: keyToSession[key]["host"],
port: parseInt(keyToSession[key]["port"])
}
port: parseInt(keyToSession[key]["port"]),
requires_path_in_url: info?.requires_path_in_url,
requires_path_in_header_named: info?.requires_path_in_header_named,
},
};
}
for (var oldSession in map) {
Expand All @@ -45,24 +51,30 @@ INSERT INTO "gxitproxy" VALUES('d24902ddec2e97f1','interactivetoolentrypoint','b
*/

var updateFromSqlite = function(path, map) {
var updateFromSqlite = function (path, map) {
var newSessions = {};
var loadSessions = function() {
var loadSessions = function () {
db.each(
"SELECT key, key_type, token, host, port FROM gxitproxy",
function(err, row) {
"SELECT key, key_type, token, host, port, info FROM gxitproxy",
function (err, row) {
var key = row["key"];
var info = row["info"];
if (info) {
info = JSON.parse(info);
}
newSessions[key] = {
target: { host: row["host"], port: parseInt(row["port"]) },
key_type: row["key_type"],
token: row["token"]
token: row["token"],
requires_path_in_url: info?.requires_path_in_url,
requires_path_in_header_named: info?.requires_path_in_header_named,
};
},
finish
finish,
);
};

var finish = function() {
var finish = function () {
for (var oldSession in map) {
if (!(oldSession in newSessions)) {
delete map[oldSession];
Expand All @@ -77,15 +89,15 @@ var updateFromSqlite = function(path, map) {
var db = new sqlite3.Database(path, loadSessions);
};

var mapFor = function(path) {
var mapFor = function (path) {
var map = {};
var loadMap;
if (endsWith(path, ".sqlite")) {
loadMap = function() {
loadMap = function () {
updateFromSqlite(path, map);
};
} else {
loadMap = function() {
loadMap = function () {
updateFromJson(path, map);
};
}
Expand Down
Loading

0 comments on commit c887ccb

Please sign in to comment.