Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a proxy from bit-server to graphql in the cloud #9069

Merged
merged 2 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 35 additions & 1 deletion scopes/harmony/api-server/api-server.main.runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import { InstallAspect, InstallMain } from '@teambit/install';
import { ImporterAspect, ImporterMain } from '@teambit/importer';
import { Component } from '@teambit/component';
import { WorkspaceAspect, Workspace } from '@teambit/workspace';
import cors from 'cors';
import { createProxyMiddleware, fixRequestBody } from 'http-proxy-middleware';
import { ApiServerAspect } from './api-server.aspect';
import { CLIRoute } from './cli.route';
import { ServerCmd } from './server.cmd';
Expand Down Expand Up @@ -83,7 +85,39 @@ export class ApiServerMain {
});

const port = options.port || 3000;
const server = await this.express.listen(port);

const app = this.express.createApp();

app.use(
// @ts-ignore todo: it's not clear what's the issue.
cors({
origin(origin, callback) {
callback(null, true);
},
credentials: true,
})
);
app.use(
'/api/cloud-graphql',
// eslint-disable-next-line @typescript-eslint/no-misused-promises
createProxyMiddleware({
target: 'https://api.main.lanes.bit.cloud/graphql',
changeOrigin: true,
on: {
error: (err, req, res) => {
this.logger.error('graphql cloud proxy error', err);
// @ts-ignore
res.writeHead(500, {
'Content-Type': 'text/plain',
});
res.end('Something went wrong with the proxy server of bit cloud graphql');
},
proxyReq: fixRequestBody,
},
})
);

const server = await app.listen(port);

return new Promise((resolve, reject) => {
server.on('error', (err) => {
Expand Down
1 change: 1 addition & 0 deletions workspace.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,7 @@
"html-escaper": "3.0.0",
"html-webpack-plugin": "5.3.2",
"http-proxy": "1.18.1",
"http-proxy-middleware": "^3.0.0",
"humanize-duration": "3.23.1",
"humanize-string": "2.1.0",
"inject-body-webpack-plugin": "1.3.0",
Expand Down