-
Notifications
You must be signed in to change notification settings - Fork 1
/
handler.js
67 lines (57 loc) · 1.5 KB
/
handler.js
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
60
61
62
63
64
65
66
67
import {
readFileSync
} from "fs";
import path from "path";
import YAML from "yaml";
import express from "express";
import {
createProxyMiddleware
} from "http-proxy-middleware";
// reads and parses config file
const readConfigFile = () => {
const file = readFileSync(
path.join(process.cwd(), "sls-multi-gateways.yml"),
"utf8"
);
return YAML.parse(file);
};
// runs each services
const runServices = (services, httpPort, stage, prefixColors) => {
const commands = [];
for (let i = 0; i < services.length; i++) {
const execCommand = `
cd ${process.cwd()}/${services[i].srvSource};
sls offline --stage ${stage} --httpPort ${
httpPort + i
} --lambdaPort ${httpPort + i + 1000}
`;
commands.push({
command: execCommand,
name: services[i].srvName,
prefixColor: i < prefixColors.length ? prefixColors[i] : "gray",
});
}
return commands;
};
// proxy each service
const runProxy = (services, httpPort, stage) => {
const app = express();
for (let i = 0; i < services.length; i++) {
const proxyPath = `/${services[i].srvPath}`;
const stripBasePath = services[i].stripBasePath;
app.use(
proxyPath,
createProxyMiddleware({
pathRewrite: (path) => {
return stripBasePath ? path.replace(proxyPath, "/") : path;
},
target: `http://localhost:${httpPort + i}/${stage}/`,
changeOrigin: true,
})
);
}
app.listen(4000);
};
export {
readConfigFile, runServices, runProxy
};