Skip to content

Commit

Permalink
changes in config props:
Browse files Browse the repository at this point in the history
server.fallback
proxy.disabled
proxies[].cache.enabled
  • Loading branch information
akupiec authored and Artur Kupiec committed Oct 29, 2017
1 parent 900d8e0 commit 61c125e
Show file tree
Hide file tree
Showing 12 changed files with 111 additions and 38 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Changelog

### 0.0.1 (in progress)
### 0.0.1

- changes in 0.0.1.... what can they be ?
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Auto Proxy

[![Travis](https://img.shields.io/travis/akupiec/auto-proxy.svg)](https://travis-ci.org/akupiec/auto-proxy.svg?branch=master)
[![license](https://img.shields.io/github/license/akupiec/auto-proxy.svg)]()


Auto caching reverse proxy, with partial static server -
highly useful for testing UI applications when backed is slow or temporary unavailable.

Expand Down
15 changes: 10 additions & 5 deletions bin/auto-proxy.config.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
/* eslint-disable indent */

module.exports = {
server: { //local server configuration block
port: 8088, // [required]
staticSource: '/public', // sever have possibility to serve local content - defines content url
staticPath: '../assets/public', // static content will be served form this directory
fallback: 'index.html', // if request will not it elsewhere this file will be served //TODO change to path
fallback: '../assets/public/index.html', // if request will not it elsewhere this file will be served
},
proxy: { //reverse proxy configuration block
target: 'http://google.com', // [required] proxy target
changeOrigin: true,
secure: true,
disabled: false, //TODO: Disable proxy, only already cached files will be served
disabled: false, //Disable proxy, only already cached files will be served
},
proxies: [ // ... it's kind'of required block
{
contextPath: '/services', // [required] poxed url path to target

cache: { //cache configuration block
enabled: true, //TODO: save new cache files
enabled: true, //save new cache files & serve them when match request
meta: false, //TODO: store request/response meta (headers, resp code)
hashing: true, //TODO: cached file name will contains hash based on query & payload request, instead of plain text
madCache: true, //TODO: cache all request even 404 will be cached as success
//When set to false all cached files will be served with 200 response code with minimum meta data
hashing: true, //TODO: file names will contain crc32 hash based on query & payload request, instead of plain text
madCache: true, //TODO: cache all request's (even 404) will be cached as success (200)
//Requires disabled meta storing
filtering: { //describes what should be cached
//TODO
},
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
"dependencies": {
"body-parser": "^1.18.2",
"chalk": "^2.3.0",
"cookie-parser": "^1.4.3",
"crc": "^3.5.0",
"deep-assign": "^2.0.0",
"express": "^4.16.2",
Expand All @@ -56,7 +55,9 @@
"yargs": "^10.0.3"
},
"devDependencies": {
"eslint": "^4.10.0",
"mocha": "^4.0.1",
"mock-fs": "^4.4.2",
"supertest": "^3.0.0"
}
}
6 changes: 1 addition & 5 deletions src/argv.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const argv = yargs.usage('$0 cli usage:')
describe: 'Path to the config file, accepts js|json. All cli options can be override by configuration file.',
type: 'string',
group: GENERAL_GROUP,
default: 'auto-proxy.json',
// default: 'auto-proxy.json',
})
.options('mock', {
alias: 'm',
Expand All @@ -36,23 +36,19 @@ const argv = yargs.usage('$0 cli usage:')
})
.options('proxy.secure', {
alias: ['secure'],
type: 'boolean',
group: PROXY_GROUP,
})
.options('proxy.changeOrigin', {
type: 'boolean',
describe: 'Proxy changes origin of requests',
group: PROXY_GROUP,
})
.options('proxy.disabled', {
type: 'boolean',
describe: 'Disable proxy, only already cached files will be served.',
group: PROXY_GROUP,
})
.options('log', {
type: 'string',
describe: 'logging level [ALL, DEBUG, INFO, WARN, ERROR, FATAL, OFF]',
default: 'ALL',
})
.help('help')
.version()
Expand Down
4 changes: 2 additions & 2 deletions src/middlewares/mockGeter.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ const fs = require('fs');
const config = require('../config');
const LOGGER = require('../logger')(config);

module.exports = function () {
module.exports = function (proxyConfig) {
return function middleware(req, res, next) {
if(req.mock.mockExists) {
if(req.mock.mockExists && proxyConfig.cache.enabled) {
LOGGER.debug('Sending from mock', req.mock.filePath);
let readStream = fs.createReadStream(req.mock.filePath);
res.status(200);
Expand Down
4 changes: 2 additions & 2 deletions src/middlewares/mockSaver.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ function mkdirSync(filePath) {
fs.mkdirSync(dirName);
}

module.exports = function () {
module.exports = function (proxyConfig) {
return function middleware(req, res, next) {
if(req.mock.mockExists) {
if(req.mock.mockExists || !proxyConfig.cache.enabled) {
next();
return;
}
Expand Down
4 changes: 4 additions & 0 deletions src/middlewares/reverseProxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ const LOGGER = require('../logger')(config);

module.exports = function (confProxy, proxyServer) {
return function middleware(req, res) {
if(config.proxy.disabled) {
res.sendStatus(404);
return;
}
if(!req.mock.mockExists) {
LOGGER.debug(`Sending: ${req.method} ${req.url} ${req.mock.hash}`);
proxyServer.web(req, res);
Expand Down
9 changes: 5 additions & 4 deletions src/server.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const express = require('express');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const mockData = require('./middlewares/mockData');
const validateCache = require('./middlewares/validateCache');
Expand All @@ -8,19 +7,21 @@ const reverseProxy = require('./middlewares/reverseProxy');
const mockGetter = require('./middlewares/mockGeter');
const mockSaver = require('./middlewares/mockSaver');
const config = require('./config');
const path = require('path');
const LOGGER = require('./logger')(config);

module.exports = function (proxyServer) {
const app = express();
app.use(cookieParser());
app.use(bodyParser.raw({type: '*/*'}));

if (config.server.staticSource) {
LOGGER.info(`Binding static content on: ${config.server.staticSource}`);
app.use(config.server.staticSource, express.static(config.server.staticPath));
}

config.proxies.map(confProxy => {
LOGGER.info(`Binding proxy on: ${confProxy.contextPath}`);
confProxy.cache = confProxy.cache || {};

app.use(confProxy.contextPath, mockData(confProxy));
app.use(confProxy.contextPath, validateCache(confProxy));
Expand All @@ -31,9 +32,9 @@ module.exports = function (proxyServer) {
});

if (config.server.fallback) {
LOGGER.info(`Binding fallback: ${config.server.staticPath}${config.server.fallback}`);
LOGGER.info(`Binding fallback: ${path.resolve(config.server.fallback)}`);
app.all('/*', function (req, res) {
res.sendFile(config.server.fallback, {root: config.server.staticPath});
res.sendFile(path.resolve(config.server.fallback));
});
}

Expand Down
49 changes: 49 additions & 0 deletions test/cachingTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/* eslint-env mocha */

const assert = require('assert');
const request = require('supertest');
const mockFs = require('mock-fs');
const fakeConfig = require('./testingConfig');
const fs = require('fs');

describe('caching', function () {
let server, proxyServer;
const fsSystem = {};
beforeEach(function () {
fakeConfig({
proxies: [{
contextPath: '/api',
cache: {
enabled: true,
},
}],
});

// server ? undefined : server.close();
proxyServer = {
web: function (req, res) {
res.send(200, 'REVERSE RESPONSE SUCCESS');
},
};
server = require('../src/server')(proxyServer);
mockFs(fsSystem);
});
afterEach(function () {
server.close();
proxyServer = null;
mockFs.restore();
});

it('should create cached file', function testSlash() {
return request(server)
.get('/api/abb-cc')
.send('aaa')
.expect(200)
.then(response => {
const a = fs.readFileSync('./FAKE_MOCK_DIR/api/GET/abb-cc#f007732d.html').toString();
assert.equal(response.text, 'REVERSE RESPONSE SUCCESS');
assert.equal(a, response.text);
});

});
});
36 changes: 18 additions & 18 deletions test/serverTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,32 @@

const assert = require('assert');
const request = require('supertest');

const config = require('../src/config');
const fakeConfig = {
mock: './FAKE_MOCK_DIR/',
server: {
port: 9999,
},
proxies: [{
contextPath: '/api',
}],
log: 'ALL',
};
Object.assign(config, fakeConfig);
const mockFs = require('mock-fs');
const fakeConfig = require('./testingConfig');

describe('loading express', function () {
let server, proxy;
let server, proxyServer;
beforeEach(function () {
proxy = {
fakeConfig({
proxies: [{
contextPath: '/api',
}],
});
proxyServer = {
web: function (req, res) {
res.send(200, 'REVERSE RESPONSE SUCCESS');
},
};
server = require('../src/server')(proxy);
server = require('../src/server')(proxyServer);
mockFs();
});
afterEach(function () {
server.close();
proxy = null;
proxyServer = null;
mockFs.restore();
});

it('responds to reverse proxy', function testSlash() {
it('responds to reverse proxyServer', function testSlash() {
return request(server)
.get('/api/abb-cc')
.send('aaa')
Expand All @@ -40,4 +36,8 @@ describe('loading express', function () {
assert.equal(response.text, 'REVERSE RESPONSE SUCCESS');
});
});

it('should write file', function testSlash() {

});
});
13 changes: 13 additions & 0 deletions test/testingConfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const config = require('../src/config');
const fakeConfig = {
mock: './FAKE_MOCK_DIR/',
server: {
port: 9999,
},
log: 'ALL',
};
Object.assign(config, fakeConfig);

module.exports = function (newConfig) {
Object.assign(config, newConfig);
};

0 comments on commit 61c125e

Please sign in to comment.