-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
59 lines (49 loc) · 1.46 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import * as http from 'http';
import * as httpProxy from 'http-proxy';
interface Options {
[key: string]: string,
}
const defaultOptions: Options = {
orca: 'http://localhost:8000',
web: 'http://localhost:1313',
port: '3000',
};
console.error('Supported flags:');
for (const key in defaultOptions) {
console.error(`--${key}=${defaultOptions[key]}`);
}
console.log('\n');
function getOptions() {
const opts: Options = {};
process.argv.slice(2).forEach(arg => {
const parts: string[] = arg.split("=");
const key = parts[0] && parts[0].replace(/--/g, '');
if (key) {
opts[key] = parts[1];
} else {
throw new Error('Cannot decode cli arguments');
}
});
return opts;
}
const options: Options = Object.assign({}, defaultOptions, getOptions());
const proxy = httpProxy.createProxyServer({});
const server = http.createServer((req: any, res: any) => {
// if path starts with `/api` it should be proxied to the api
const regexp = new RegExp('^\/api');
if (req.url.match(regexp)) {
// rewrite url and proxy
req.url = req.url.substr(4);
proxy.web(req, res, {
target: options.orca
});
} else {
// everything else is proxied to the web:
proxy.web(req, res, {
target: options.web
});
}
});
const port: number = parseInt(options.port);
console.log(`listening on port ${port}`);
server.listen(port);