Skip to content

Commit

Permalink
[INJICERT-567] (#174)
Browse files Browse the repository at this point in the history
* [INJICERT-567]

Signed-off-by: Hitesh C <[email protected]>

* [INJICERT-567]

CHanges for demo
[ADDED] inji-web-proxy src code

Signed-off-by: Hitesh C <[email protected]>

---------

Signed-off-by: Hitesh C <[email protected]>
Signed-off-by: Vishwa <[email protected]>
  • Loading branch information
jainhitesh9998 authored Oct 27, 2024
1 parent 046f049 commit b67508e
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 2 deletions.
2 changes: 2 additions & 0 deletions inji-web-proxy/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
MIMOTO_HOST=http://localhost:8088/v1/mimoto
PORT=3010
4 changes: 4 additions & 0 deletions inji-web-proxy/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.project
.idea
package-*.json
node_modules
21 changes: 21 additions & 0 deletions inji-web-proxy/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Dockerfile
# Use the official Node.js image
FROM node:16.9.1

# Create and set the working directory inside the container
WORKDIR /usr/src/app

# Copy package.json and package-lock.json (if available)
COPY package*.json ./

# Install dependencies
RUN npm install

# Copy the rest of the application code
COPY . .

# Expose the port on which the app runs
EXPOSE 3010

# Run the application
CMD ["node", "proxy_server.js"]
20 changes: 20 additions & 0 deletions inji-web-proxy/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
### Inji Web Proxy

Inji Web Proxy is express js application which is build to connect Backend Service From Inji Web to Avoid CORS issue.


### Environment Variables :

> MIMOTO_HOST : Update the host url of the Mimoto
> PORT : port in which proxy will run
### Installation Steps :

> npm i && node ./proxy_server.js
### Usage :

- Goto InjiWeb [api.ts](../inji-web/src/utils/api.ts)
- In order to avoid CORS, update the **mimotoHost** of Inji Web from Mimoto service url to Inji Web Proxy server url, so that it proxies and bypasses the CORS
- ref : https://localhost:3010
20 changes: 20 additions & 0 deletions inji-web-proxy/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "proxy-server",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"axios": "^1.6.8",
"body-parser": "^1.20.2",
"cors": "^2.8.5",
"dotenv": "^16.4.5",
"express": "^4.19.2",
"fs": "^0.0.1-security",
"path": "^0.12.7"
}
}
62 changes: 62 additions & 0 deletions inji-web-proxy/proxy_server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
const express = require('express');
const cors = require('cors');
const axios = require('axios');
const bodyParser = require('body-parser');
require('dotenv').config()

const app = express();
const PORT = process.env.PORT;

app.use(express.json());
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

app.all('*', async (req, res) => {
delete req.headers.host
delete req.headers.referer
const API_URL = process.env.MIMOTO_HOST;
const path = req.url
try {
let response = {};
if(path.indexOf("download") !== -1 ) {
res.setHeader('Access-Control-Allow-Origin', '*'); // Change '*' to specific origin if needed
res.setHeader('Access-Control-Allow-Methods', 'GET,OPTIONS,POST'); // Allow GET requests
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization'); // Allow specific headers

response = await axios({
method: req.method,
responseType: "arraybuffer",
url: `${API_URL}/v1/mimoto/credentials/download`,
data: new URLSearchParams(req.body),
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
"Accept": 'application/pdf',
'Cache-Control': 'no-cache, no-store, must-revalidate'
}
});
res.set("Content-Type", "application/pdf");
res.status(response.status).send(response.data);
} else {
response = await axios({
method: req.method,
url: `${API_URL}${path}`,
headers: req.headers,
data: new URLSearchParams(req.body)
});
res.status(response.status).json(response.data);
}

} catch (error) {
console.error("Error occurred: ", error);
if (error.response) {
res.status(error.response.status).json(error.response.data);
} else {
res.status(500).json({ error: error.message });
}
}
});

app.listen(PORT, () => {
console.log(`Proxy server listening on port ${PORT}`);
});
3 changes: 2 additions & 1 deletion inji-web/.env
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
CI=false
CI=false
REACT_APP_MIMOTO_HOST=http://localhost:3010/v1/mimoto
3 changes: 2 additions & 1 deletion inji-web/src/utils/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ export enum MethodType {
export class api {

// static mimotoHost = "http://localhost:3010";
static mimotoHost = window.location.origin + "/v1/mimoto";
// static mimotoHost = window.location.origin + "/v1/mimoto";
static mimotoHost = process.env.REACT_APP_MIMOTO_HOST;

static authorizationRedirectionUrl = window.location.origin + "/redirect";

Expand Down

0 comments on commit b67508e

Please sign in to comment.