Skip to content

Commit

Permalink
Resolvendo issues de manutenibilidade
Browse files Browse the repository at this point in the history
  • Loading branch information
GabrielRoger07 committed Sep 5, 2024
1 parent 42f8342 commit bcf0ba1
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
23 changes: 18 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
import express, { Express, Request, Response } from 'express';
import dotenv from 'dotenv';
import cors from 'cors'
import httpProxy from 'express-http-proxy'
import cors from 'cors';
import httpProxy from 'express-http-proxy';
import { getHost, getUrl } from './utils';

dotenv.config();

const app: Express = express();
app.disable('x-powered-by');

app.use(cors())
const allowedOrigins = ['http://your-frontend-domain.com', 'http://another-allowed-domain.com'];

const corsOptions = {
origin: (origin: string | undefined, callback: (err: Error | null, allow?: boolean) => void) => {
if (!origin || allowedOrigins.indexOf(origin) !== -1) {
callback(null, true);
} else {
callback(new Error('Not allowed by CORS'));
}
},
optionsSuccessStatus: 200,
};

app.use(cors(corsOptions));

const port = 8888;
const host = "0.0.0.0";
Expand All @@ -21,8 +35,7 @@ app.use('/*', httpProxy(getHost, {
proxyReqPathResolver(req) {
return getUrl(req);
}
}))

}));

app.listen(port, host, () => {
console.log(`⚡️[server]: Server is running at http://${host}:${port}`);
Expand Down
17 changes: 10 additions & 7 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,25 @@ const getUrl = (req: Request) => {

const queryString = parts[1];
const updatedPath = parts[0];
return baseUrl + updatedPath + (queryString ? '?' + queryString : '')
return baseUrl + updatedPath + (queryString ? '?' + queryString : '');
}

const videoServiceRegex = /\/videoservice/;
const userServiceRegex = /\/userservice/;
const adminServiceRegex = /\/adminservice/;

const getHost = (req: Request) => {
if (req.baseUrl.match('/videoservice')) {
if (videoServiceRegex.exec(req.baseUrl)) {
console.log("VIDEO CALLED");
return VIDEO_API_URL;
} else if (req.baseUrl.match('/userservice')) {
} else if (userServiceRegex.exec(req.baseUrl)) {
console.log("USER CALLED");
return USER_API_URL;
} else if (req.baseUrl.match('/adminservice')) {
} else if (adminServiceRegex.exec(req.baseUrl)) {
console.log("ADMIN CALLED");
return ADMIN_API_URL;
}
else {
return ''
} else {
return '';
}
}

Expand Down

0 comments on commit bcf0ba1

Please sign in to comment.