Skip to content

Commit

Permalink
refactoring swagger config (#213)
Browse files Browse the repository at this point in the history
## PR Description
Hey team,

This pull request aims to improve the code structure and organization of
the app.js file in our GitHub repository. The main objective is to
enhance maintainability and readability by moving the Swagger
configuration code to a separate file called setupSwagger.js. By doing
this, we can achieve better separation of concerns and adhere to the
single responsibility principle.


## Changes Made
- Created a new file called setupSwagger.js in the appropriate
directory.
- Transferred the Swagger configuration code from app.js to
setupSwagger.js.
- Updated the app.js file to import the necessary functions or
configurations from setupSwagger.js.
- Verified that the application remains functional after the code
refactoring.


## Notes for Reviewers
Please review the changes made in this pull request carefully, paying
particular attention to the new setupSwagger.js file and the
modifications in app.js. Any feedback, suggestions, or improvements are
welcome

Best regards,
Louay HICHRI
  • Loading branch information
ksibisamir committed May 25, 2023
2 parents e8a5168 + 4b942f8 commit 2a86742
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 42 deletions.
47 changes: 5 additions & 42 deletions app.js
Expand Up @@ -11,6 +11,7 @@ require('dotenv').config()
let logger = require('morgan')
let cookieParser = require('cookie-parser')
let path = require('path')
const {swaggerUi, swaggerSpec, cssOptions} = require('./conf/swaggerSetup');
// set up rate limiter: maximum of five requests per minute
var RateLimit = require('express-rate-limit')
const package = require('./package.json')
Expand Down Expand Up @@ -111,6 +112,7 @@ app.use('/auth', loginroutes)
app.use('/wallet', walletroutes)
app.use('/profile', profileroutes)
app.use('/campaign', campaignroutes)
app.use('/docs', swaggerUi.serve, swaggerUi.setup(swaggerSpec, cssOptions));
let host
if (process.env.NODE_ENV == 'testnet') {
host = process.env.BASEURL
Expand All @@ -120,49 +122,10 @@ if (process.env.NODE_ENV == 'testnet') {
host = process.env.BASEURL_MAINNET
}

const swaggerJSDoc = require('swagger-jsdoc')
const swaggerUi = require('swagger-ui-express')
const swaggerDefinition = {
openapi: '3.0.0',
info: {
title: 'API for node-satt',
version: package.version,
description:
'Welcome to SaTT Webservice endpoint, this backend provides webservice to SaTT WebWallet and advertising campaign manager',
customCss: '.swagger-ui .topbar { display: none }',
},
host: host,
components: {
securitySchemes: {
bearerAuth: {
type: 'http',
scheme: 'bearer',
bearerFormat: 'JWT',
},
},
},
security: [
{
bearerAuth: [],
},
],
}
var cssOptions = {
customCss: `
.topbar-wrapper img {content:url(/assets/SaTT.png); width:50px; height:auto;}`,
customSiteTitle: 'SaTT',
customfavIcon: '/assets/SaTT-noire.png',
}
const options = {
swaggerDefinition,
apis: ['./routes/*.js'],
}
const swaggerSpec = swaggerJSDoc(options)

// if (process.env.NODE_ENV !== 'mainnet') {
// app.use('/docs', swaggerUi.serve, swaggerUi.setup(swaggerSpec, cssOptions))
// }
app.use('/docs', swaggerUi.serve, swaggerUi.setup(swaggerSpec, cssOptions))




// catch 204 and forward to error handler
app.use(function (req, res, next) {
Expand Down
54 changes: 54 additions & 0 deletions conf/swaggerSetup.js
@@ -0,0 +1,54 @@
const swaggerUi = require('swagger-ui-express')
const swaggerJSDoc = require('swagger-jsdoc')
const package = require('./../package.json')
require('dotenv').config()
let host
if (process.env.NODE_ENV == 'testnet') {
host = process.env.BASEURL
} else if (process.env.NODE_ENV == 'local') {
host = process.env.BASEURLLOCAL
} else {
host = process.env.BASEURL_MAINNET
}
const swaggerDefinition = {
openapi: '3.0.0',
info: {
title: 'API for node-satt',
version: package.version,
description:
'Welcome to SaTT Webservice endpoint, this backend provides webservice to SaTT WebWallet and advertising campaign manager',
customCss: '.swagger-ui .topbar { display: none }',
},
host: host,
components: {
securitySchemes: {
bearerAuth: {
type: 'http',
scheme: 'bearer',
bearerFormat: 'JWT',
},
},
},
security: [
{
bearerAuth: [],
},
],
}

const cssOptions = {
customCss: `
.topbar-wrapper img {content:url(/assets/SaTT.png); width:50px; height:auto;}`,
customSiteTitle: 'SaTT',
customfavIcon: '/assets/SaTT-noire.png',
}

const options = {
swaggerDefinition,
apis: ['./routes/*.js'],
}
const swaggerSpec = swaggerJSDoc(options)



module.exports = {swaggerUi, swaggerSpec, cssOptions};

0 comments on commit 2a86742

Please sign in to comment.