Skip to content

Commit

Permalink
feat(#390): Add swagger middleware logic and required env variables
Browse files Browse the repository at this point in the history
  • Loading branch information
tholulomo committed May 16, 2023
1 parent e7213c2 commit 2a577fa
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 46 deletions.
6 changes: 6 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ services:
- MM_AUTH_EMAIL_HEADER=${MM_AUTH_EMAIL_HEADER}
- MM_AUTH_USER_HEADER=${MM_AUTH_USER_HEADER}
- KNOWLEDGE_ADDRESS=${WHYIS_ADDRESS}
- TERM_OF_SERVICE=${TERM_OF_SERVICE}
- SWAGGER_CONTACT_NAME=${SWAGGER_CONTACT_NAME}
- SWAGGER_CONTACT_EMAIL=${SWAGGER_CONTACT_EMAIL}
- DEV_ENDPOINT=${DEV_ENDPOINT}
- QA_SERVER_ENDPOINT=${QA_SERVER_ENDPOINT}
- PROD_SERVER_ENDPOINT=${PROD_SERVER_ENDPOINT}
ports:
- '3001:3001'
client:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,11 @@ info:
welcome feedback and suggestions for further improvements to help you benchmark and visualize your data, explore your
research hypotheses, perform simulations and analysis using our available tools, and more.
version: 1.0.0
termsOfService: https://security.duke.edu/node/82
termsOfService:
contact:
name: Marc Palmeri, Ph.D.
email: marc.j.palmeri@duke.edu
name:
email:
servers:
- url: http://localhost/api
- url: https://qa.materialsmine.org/api
description: Staging server
- url: https://materialsmine.org/api
description: Production server

paths:
/admin/es:
Expand Down Expand Up @@ -76,7 +71,7 @@ paths:
properties:
status:
type: string
example: Successful
example: Successfully configured schemas!
'401':
description: User not authorized for this service
content:
Expand Down
33 changes: 2 additions & 31 deletions resfulservice/src/middlewares/index.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,9 @@
// const bodyParser = require('body-parser');
// const acceptedHeaders = require('./accept');
// const getEnv = require('./parseEnv');
// const { fileMgr, fileServer } = require('./fileStorage');
// const { logParser, mmLogger } = require('./loggerService');

// const log = mmLogger();

// /**
// * globalMiddleWare - this function that instantiate all
// * global middleware services for the REST API
// * @param {*} app Express app object
// */
// const globalMiddleWare = async (app) => {
// app.use(bodyParser.json());
// app.use((req, res, next) => logParser(log, req, next));
// app.use(fileMgr);
// app.use('/mm_files', fileServer);
// app.use(acceptedHeaders);
// app.use(getEnv);
// };

// /**
// * This file imports all GLOBAL middleware services &
// * exports them to the server.js file.
// */
// module.exports = {
// log,
// globalMiddleWare
// };

const express = require('express');
const acceptedHeaders = require('./accept');
const getEnv = require('./parseEnv');
const { fileMgr, fileServer } = require('./fileStorage');
const { logParser, mmLogger } = require('./loggerService');
const swaggerService = require('./swagger-service');

const log = mmLogger();

Expand All @@ -58,5 +28,6 @@ const globalMiddleWare = async (app) => {
*/
module.exports = {
log,
swaggerService,
globalMiddleWare
};
24 changes: 24 additions & 0 deletions resfulservice/src/middlewares/swagger-service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const path = require('path');
const yaml = require('yamljs');

const contractDocumentPath = path.resolve(__dirname, '../api-docs/swagger-service.yaml');

// Serve the Swagger UI
module.exports = (env) => {
const contractDocument = yaml.load(contractDocumentPath);
contractDocument && (contractDocument.info.termsOfService = env.TERM_OF_SERVICE ?? '');
contractDocument && (contractDocument.info.contact.name = env.SWAGGER_CONTACT_NAME ?? '');
contractDocument && (contractDocument.info.contact.email = env.SWAGGER_CONTACT_EMAIL ?? '');
contractDocument && (contractDocument.servers = [
{ url: env.DEV_ENDPOINT ?? '' },
{
url: env.QA_SERVER_ENDPOINT ?? undefined,
description: 'Staging server'
},
{
url: env.PROD_SERVER_ENDPOINT ?? undefined,
description: 'Production server'
}
].filter((service) => !!service.url && service));
return contractDocument;
};
9 changes: 3 additions & 6 deletions resfulservice/src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ const mongoose = require('mongoose');
const { createServer: createHttpServer } = require('http');
const { WebSocketServer } = require('ws');
const { useServer: useWsServer } = require('graphql-ws/lib/use/ws');
const path = require('path');
const swaggerUi = require('swagger-ui-express');
const yaml = require('yamljs');
const { globalMiddleWare, log } = require('./middlewares');
const { globalMiddleWare, log, swaggerService } = require('./middlewares');
const adminRoutes = require('./routes/admin');
const authRoutes = require('./routes/authService');
const curationRoutes = require('./routes/curation');
Expand All @@ -23,16 +21,15 @@ const typeDefs = require('./graphql');
const getHttpContext = require('./graphql/context/getHttpContext');
const getWsContext = require('./graphql/context/getWsContext');

const swaggerDocumentPath = path.resolve(__dirname, './api-docs/swagger.yaml');
const env = process.env;

const app = express();
globalMiddleWare(app);
elasticSearch.ping(log);

// Serve the Swagger UI
const swaggerDocument = yaml.load(swaggerDocumentPath);
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument, { explorer: true }));
const apiContractDocument = swaggerService(env);
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(apiContractDocument, { explorer: true }));

app.use('/admin', adminRoutes);
app.use('/curate', curationRoutes);
Expand Down

0 comments on commit 2a577fa

Please sign in to comment.