From 0405faa237947a754f5d676fc38951b92a2e9764 Mon Sep 17 00:00:00 2001
From: Ulf Seltmann <seltmann@ub.uni-leipzig.de>
Date: Tue, 26 Sep 2017 17:48:33 +0200
Subject: [PATCH] added option to strip path when necessary

---
 bin/dacap     |  4 +++-
 docs/index.md |  6 +++---
 src/server.ts | 26 ++++++++++++++++----------
 3 files changed, 22 insertions(+), 14 deletions(-)

diff --git a/bin/dacap b/bin/dacap
index f89923b..3365fbd 100755
--- a/bin/dacap
+++ b/bin/dacap
@@ -15,6 +15,7 @@ const defaultArrayValueSize = parseInt(process.env.array_value_size) || 40;
 const defaultObjectValueSize = parseInt(process.env.object_value_size) || 80;
 const autosaveInterval = parseInt(process.env.autosave_interval) || 60;
 const registerName = process.env.register_name || 'api-cache';
+const stripPath = !(process.env.strip_path == 'false');
 
 const server = new Server({
 	storagePath: storagePath,
@@ -25,7 +26,8 @@ const server = new Server({
 	defaultArrayValueSize: defaultArrayValueSize,
 	defaultObjectValueSize: defaultObjectValueSize,
 	autosaveInterval: autosaveInterval,
-	registerName: registerName
+	registerName: registerName,
+	stripPath: stripPath
 });
 
 server.listen(proxyPort, () => {
diff --git a/docs/index.md b/docs/index.md
index 1da33b5..f76dce8 100644
--- a/docs/index.md
+++ b/docs/index.md
@@ -52,6 +52,6 @@ The service can be configured by environment variables. The following are availa
 * `default_check_period`: specifies the interval in seconds the cache is checked for expiry. Defaults to `60`
 * `array_value_size`: unkown configuration. Defaults to `40`
 * `object_value_size`: unkown configuration. Defaults to `80`
-* `autosave_interval`: specifies the interval the cache is stored to harddisk. defaults to `60`;
-* `register_name`: specifies the name of the register where the cache is stored. Defaults to `api-cache`;
-
+* `autosave_interval`: specifies the interval the cache is stored to harddisk. defaults to `60`
+* `register_name`: specifies the name of the register where the cache is stored. Defaults to `api-cache`
+* `strip_path`: specifies whether the path should be stripped when processing requests
diff --git a/src/server.ts b/src/server.ts
index 0cc0d8c..40014d4 100644
--- a/src/server.ts
+++ b/src/server.ts
@@ -5,12 +5,14 @@ import * as path from 'path';
 import * as cache from './cache';
 import * as bodyparser from 'body-parser';
 import * as cors from 'cors';
+import * as url from 'url';
 const debug = debugFactory('dacap:server');
 
 export class Server {
 	private register: cache.Register;
 	private expressApp: express.Application;
 	private corsOptions: cors.CorsOptions;
+	private prePath: string = '';
 
 	constructor(private config: {
 		storagePath: string,
@@ -21,9 +23,13 @@ export class Server {
 		defaultArrayValueSize: number,
 		defaultObjectValueSize: number,
 		autosaveInterval: number,
-		registerName: string
+		registerName: string,
+		stripPath: boolean
 	}) {
 
+		if (this.config.stripPath === false) {
+			this.prePath = url.parse(this.config.proxyUrl).path;
+		}
 		this.expressApp = express();
 
 		this.initRegister();
@@ -57,11 +63,11 @@ export class Server {
 			res.send();
 		});
 
-		this.expressApp.get('/admin/api/config', (req, res, next) => {
+		this.expressApp.get(this.prePath + '/admin/api/config', (req, res, next) => {
 			return res.json(this.config);
 		});
 
-		this.expressApp.post('/admin/api/add/cache/:name', (req, res, next) => {
+		this.expressApp.post(this.prePath + '/admin/api/add/cache/:name', (req, res, next) => {
 			if (!req.body || !req.body.apiEndPoint) return res.status(400).send(`you need to specify at least an url`);
 			if (this.register.has(req.body.name)) return res.send(`already registered endpoint "${req.params.name}"`);
 			this.register.add(req.params.name, req.body.apiEndPoint, {
@@ -73,7 +79,7 @@ export class Server {
 			res.json(this.register.getInfo(req.params.name));
 		});
 
-		this.expressApp.get('/admin/api/list/cache', async (req, res, next) => {
+		this.expressApp.get(this.prePath + '/admin/api/list/cache', async (req, res, next) => {
 			try {
 				res.json(this.register.getInfo());
 			} catch (err) {
@@ -81,7 +87,7 @@ export class Server {
 			}
 		});
 
-		this.expressApp.get('/admin/api/delete/cache/:name', (req, res, next) => {
+		this.expressApp.get(this.prePath + '/admin/api/delete/cache/:name', (req, res, next) => {
 			try {
 				this.register.delete(req.params.name);
 				res.json({});
@@ -90,7 +96,7 @@ export class Server {
 			}
 		})
 
-		this.expressApp.get('/admin/api/flush/cache/:name', (req, res, next) => {
+		this.expressApp.get(this.prePath + '/admin/api/flush/cache/:name', (req, res, next) => {
 			try {
 				const cache = this.register.get(req.params.name)
 				cache.flush();
@@ -100,7 +106,7 @@ export class Server {
 			}
 		});
 
-		this.expressApp.get('/admin/api/delete/cache/:name/key/:hash', (req, res, next) => {
+		this.expressApp.get(this.prePath + '/admin/api/delete/cache/:name/key/:hash', (req, res, next) => {
 			try {
 				const cache = this.register.get(req.params.name)
 				cache.del(req.params.hash);
@@ -110,7 +116,7 @@ export class Server {
 			}
 		})
 
-		this.expressApp.get('/admin/api/refresh/cache/:name/key/:hash', async (req, res, next) => {
+		this.expressApp.get(this.prePath + '/admin/api/refresh/cache/:name/key/:hash', async (req, res, next) => {
 			try {
 				const cache = this.register.get(req.params.name);
 				const value = cache.getCache().get(req.params.hash);
@@ -121,9 +127,9 @@ export class Server {
 			}
 		});
 
-		this.expressApp.use('/admin/', express.static(path.resolve(__dirname, '..', 'public')));
+		this.expressApp.use(this.prePath + '/admin/', express.static(path.resolve(__dirname, '..', 'public')));
 
-		this.expressApp.use(this.config.proxyPath, cache.Middleware(this.register));
+		this.expressApp.use(this.prePath + this.config.proxyPath, cache.Middleware(this.register));
 	}
 
 	listen(port: number, cb) {
-- 
GitLab