Feature/api paramyxrx (#25)
* Update .gitignore * Finalize server and add endpoints for paramyx app * Update server.js * Add middleware * fix error * Update auth.js * add logs * log * Remove Console Logs
This commit is contained in:
parent
8cebefe5a6
commit
6ef7d89de3
4 changed files with 34 additions and 3 deletions
22
api/middleware/auth.js
Normal file
22
api/middleware/auth.js
Normal file
|
|
@ -0,0 +1,22 @@
|
||||||
|
export const validateMedicationApiKey = (req, res, next) => {
|
||||||
|
const authHeader = req.headers['authorization'];
|
||||||
|
const token = authHeader && authHeader.split(' ')[1];
|
||||||
|
|
||||||
|
if (!token || token !== process.env.MEDICATION_API_KEY) {
|
||||||
|
console.log('MEDICATION - User entered an Invalid token: ', token);
|
||||||
|
return res.status(401).json({ error: 'Unauthorized - Invalid API Key' });
|
||||||
|
}
|
||||||
|
next();
|
||||||
|
};
|
||||||
|
|
||||||
|
export const validateShiftSyncApiKey = (req, res, next) => {
|
||||||
|
const authHeader = req.headers['authorization'];
|
||||||
|
const token = authHeader && authHeader.split(' ')[1];
|
||||||
|
|
||||||
|
if (!token || token !== process.env.SHIFTSYNC_API_KEY) {
|
||||||
|
console.log('SHIFT - User entered an Invalid token: ', token);
|
||||||
|
return res.status(401).json({ error: 'Unauthorized - Invalid API Key' });
|
||||||
|
}
|
||||||
|
|
||||||
|
next();
|
||||||
|
};
|
||||||
|
|
@ -2,7 +2,9 @@ import express from 'express';
|
||||||
import cors from 'cors';
|
import cors from 'cors';
|
||||||
import dotenv from 'dotenv';
|
import dotenv from 'dotenv';
|
||||||
import { medicationRouter } from './services/medications/index.js';
|
import { medicationRouter } from './services/medications/index.js';
|
||||||
|
import { shiftsRouter } from './services/shifts/index.js';
|
||||||
import { shiftRunQuery } from './services/shiftConnection.js';
|
import { shiftRunQuery } from './services/shiftConnection.js';
|
||||||
|
import { validateMedicationApiKey, validateShiftSyncApiKey } from './middleware/auth.js';
|
||||||
dotenv.config();
|
dotenv.config();
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
|
|
@ -22,7 +24,11 @@ app.use(express.json());
|
||||||
|
|
||||||
const apiRouter = express.Router();
|
const apiRouter = express.Router();
|
||||||
|
|
||||||
apiRouter.use('/medication', medicationRouter);
|
// ParamyxRx Router (/api/medication)
|
||||||
|
apiRouter.use('/medication', validateMedicationApiKey, medicationRouter);
|
||||||
|
|
||||||
|
// ShiftSync Router (/api/shifts)
|
||||||
|
apiRouter.use('/shifts', validateShiftSyncApiKey, shiftsRouter);
|
||||||
|
|
||||||
app.get("/api", (req, res) => {
|
app.get("/api", (req, res) => {
|
||||||
res.json('Welcome to Shift Sync API');
|
res.json('Welcome to Shift Sync API');
|
||||||
|
|
@ -30,7 +36,7 @@ app.get("/api", (req, res) => {
|
||||||
|
|
||||||
app.use('/api', apiRouter);
|
app.use('/api', apiRouter);
|
||||||
|
|
||||||
app.get('/db-health', async (req, res) => {
|
app.get('/api/db-health', async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const result = await shiftRunQuery('SELECT NOW()');
|
const result = await shiftRunQuery('SELECT NOW()');
|
||||||
res.json({ connected: true, time: result.rows[0].now });
|
res.json({ connected: true, time: result.rows[0].now });
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,7 @@ const getBaseMedications = async () => {
|
||||||
const dataResp = medicationHelpers.getBaseMedications();
|
const dataResp = medicationHelpers.getBaseMedications();
|
||||||
return dataResp;
|
return dataResp;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.log('GET MEDICATIONS ERROR: ', err);
|
console.log('GET BASE MEDICATIONS ERROR: ', err);
|
||||||
throw err;
|
throw err;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
3
api/services/shifts/index.js
Normal file
3
api/services/shifts/index.js
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
import express from 'express';
|
||||||
|
|
||||||
|
export const shiftsRouter = express.Router();
|
||||||
Loading…
Reference in a new issue