diff --git a/bin/dacap b/bin/dacap index f89923b82fe70bdc0eb56fa66056c7b8dddb00a3..3365fbde8279c33a638bf9c1fb84a239e56a3aef 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 1da33b5837e7e6c4097d8f125472257d9904ebe5..f76dce868420ba15f2d6259f531193b8d6fba1a3 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 0cc0d8c23f8c8abddc0544554b4f48b129b2798b..40014d4a41f67b4a44e9af85722954a2cef9e5ce 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) {