Skip to content

Commit

Permalink
trying something else
Browse files Browse the repository at this point in the history
  • Loading branch information
MenGonz committed Jan 20, 2025
1 parent 96a83f7 commit a491514
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 17 deletions.
52 changes: 52 additions & 0 deletions frontdoor-endpoints/frontdoor-api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const express = require('express');
const axios = require('axios');

const router = express.Router();



// Middleware: Add APIMS subscription key to outgoing requests
const addSubscriptionKey = (req, res, next) => {
const subscriptionKey = process.env.REACT_APP_APIMS_SUBSCRIPTION_KEY; // Store this key in .env file
if (!subscriptionKey) {
console.error('[Proxy Middleware] Missing APIMS Subscription Key');
return res.status(500).send('Internal Server Error: Missing APIMS Subscription Key');
}
req.headers['Ocp-Apim-Subscription-Key'] = subscriptionKey;
next();
};

// Proxy logic for all routes under /api
router.all('/*', addSubscriptionKey, async (req, res) => {
try {
// Construct the APIMS URL
const targetUrl = `${process.env.APIMS_BASE_URL}${req.originalUrl}`;

console.log(`[Proxy Middleware] Forwarding request to: ${targetUrl}`);

// Make the request to APIMS
const response = await axios({
method: req.method,
url: targetUrl,
headers: req.headers, // Forward all headers, including the subscription key
data: req.body, // Forward the request body
});

console.log(`[Proxy Middleware] Received response from APIMS: ${response.status}`);
// Send the response back to the client
res.status(response.status).send(response.data);
} catch (error) {
console.error('[Proxy Middleware] Error during request forwarding:', error.message);

if (error.response) {
// If APIMS responded with an error, forward it to the client
console.error(`[Proxy Middleware] APIMS Response:`, error.response.data);
res.status(error.response.status).send(error.response.data);
} else {
// If no response from APIMS, send a generic error
res.status(500).send('Internal Server Error: Unable to process the request');
}
}
});

module.exports = router;
15 changes: 3 additions & 12 deletions frontdoor-endpoints/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,16 @@ router.use((req, res, next) => {
router.use('/employee', (req, res, next) => {
console.log('[Debug: /employee Route] Request received:', req.originalUrl);
next();
}, apiProxy());
}, apiProxy);

router.use('/request', (req, res, next) => {
console.log('[Debug: /request Route] Request received:', req.originalUrl);
next();
}, apiProxy());
}, apiProxy);

router.use('/liquidation-request', (req, res, next) => {
console.log('[Debug: /liquidation-request Route] Request received:', req.originalUrl);
next();
}, apiProxy());


router.use('/employee', apiProxy());
router.use('/request', apiProxy());
router.use('/liquidation-request', apiProxy());
router.use('/vacations-info', apiProxy());
router.use('/email_id', apiProxy());
router.use('/employees-by-leader', apiProxy());

}, apiProxy);

module.exports = router;
7 changes: 2 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ require('dotenv').config();

// routes
const photoRoute = require('./routes/photoRoute');
const apiFrontdoor = require('./frontdoor-endpoints/routes');
const apiFrontdoor = require('./frontdoor-endpoints/frontdoor-api');

const employee_endpoint = require('./backdoor-endpoints/employee/routes');
const request_endpoint = require('./backdoor-endpoints/request/routes');
Expand Down Expand Up @@ -55,11 +55,8 @@ app.use((req, res, next) => {
});

// Routes
app.use('/protected', apiBackdoor); // Attach protected API routes
app.use('/api', apiFrontdoor); // Attach additional API routes
app.use('/employee-photos', photoRoute);

// Add a log to confirm the mount point
app.use('/protected', apiBackdoor); // Attach protected API routes
app.use('/api', (req, res, next) => {
console.log('[Debug: Router Mount] /api prefix detected, passing to router');
next();
Expand Down

0 comments on commit a491514

Please sign in to comment.