-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
153 lines (140 loc) · 3.28 KB
/
index.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
const express = require("express");
const morgan = require("morgan");
const { createProxyMiddleware } = require("http-proxy-middleware");
const cors = require("cors");
require("dotenv").config();
const params = require("./middlewares/readParams");
/* const fetch = require("node-fetch");
const sessionIds = require("./middlewares/sessionbd"); */
// Create Express Server
const app = express();
app.use(cors());
// Configuration
const PORT = process.env.PORT;
const HOST = process.env.HOST || "localhost";
const API_SERVICE_URL = process.env.API_SERVICE_URL;
const API_SERVICE_STF = process.env.API_SERVICE_STF;
const API_SERVICE_GRID = process.env.API_SERVICE_GRID;
const API_SERVICE_APPIUM_SESSION = process.env.API_SERVICE_APPIUM_SESSION;
// Logging
app.use(morgan("dev"));
// Info GET endpoint
app.get(
"/taas/grid/sessions",
createProxyMiddleware({
target: API_SERVICE_APPIUM_SESSION,
changeOrigin: false,
ws: true,
pathRewrite: {
[`^/taas/grid`]: "",
},
})
);
app.delete(
"/taas/grid/*",
params.readGrid,
createProxyMiddleware({
target: API_SERVICE_GRID,
changeOrigin: false,
ws: true,
pathRewrite: {
[`^/taas/grid`]: "",
},
})
);
// app.use(express.json());
const restream = function (proxyReq, req, res, options) {
console.log(req.body);
if (req.body) {
let bodyData = JSON.stringify(req.body);
// incase if content-type is application/x-www-form-urlencoded -> we need to change to application/json
proxyReq.setHeader("Content-Type", "application/json;charset=UTF-8");
proxyReq.setHeader("Content-Length", Buffer.byteLength(bodyData));
// stream the content
proxyReq.write(bodyData);
}
};
/* app.use('/taas/stf', createProxyMiddleware({
target: API_SERVICE_STF,
changeOrigin: false,
ws:true,
pathRewrite: {
[`^/taas/stf`]: '',
}
})); */
app.post(
"/taas/grid/wd/hub/session",
express.json(),
params.readGrid,
createProxyMiddleware({
target: API_SERVICE_GRID,
changeOrigin: false,
onProxyReq: restream,
ws: true,
pathRewrite: {
[`^/taas/grid`]: "",
},
})
);
app.post(
"/taas/grid/wd/hub/session/:session/*",
express.json(),
params.readGrid,
createProxyMiddleware({
target: API_SERVICE_GRID,
changeOrigin: false,
onProxyReq: restream,
ws: true,
pathRewrite: {
[`^/taas/grid`]: "",
},
})
);
app.get(
"/taas/grid/*",
express.json(),
params.readGrid,
createProxyMiddleware({
target: API_SERVICE_GRID,
changeOrigin: false,
onProxyReq: restream,
ws: true,
pathRewrite: {
[`^/taas/grid`]: "",
},
})
);
//appium inspector
app.use(
"/taas/appiumInspector/session/:idSession",
express.json(),
params.readSession,
createProxyMiddleware({
target: API_SERVICE_URL,
changeOrigin: false,
onProxyReq: restream,
ws: true,
pathRewrite: {
[`^/taas/appiumInspector`]: "",
},
})
);
// Proxy endpoints
app.use(
"/taas/appiumInspector",
express.json(),
params.readeParams,
createProxyMiddleware({
target: API_SERVICE_URL,
changeOrigin: false,
onProxyReq: restream,
ws: true,
pathRewrite: {
[`^/taas/appiumInspector`]: "",
},
})
);
// Start the Proxy
app.listen(PORT, HOST, () => {
console.log(`Starting Proxy at ${HOST}:${PORT}`);
});