2023-10-09 17:22:48 +00:00
|
|
|
require('dotenv').config()
|
|
|
|
require('express-async-errors')
|
|
|
|
|
|
|
|
// express
|
|
|
|
const express = require('express')
|
|
|
|
const app = express()
|
|
|
|
|
|
|
|
// rest of the packages
|
2023-10-10 13:58:54 +00:00
|
|
|
const morgan = require('morgan')
|
2023-10-09 17:22:48 +00:00
|
|
|
// const fileUpload = require('express-fileupload')
|
|
|
|
|
|
|
|
const rateLimiter = require('express-rate-limit')
|
2023-10-10 13:58:54 +00:00
|
|
|
|
|
|
|
// Swagger
|
|
|
|
const swaggerUI = require('swagger-ui-express')
|
|
|
|
const YAML = require('yamljs')
|
2023-11-03 12:02:59 +00:00
|
|
|
const swaggerDocument = YAML.load('./swagger.yaml')
|
2023-10-09 17:22:48 +00:00
|
|
|
|
2023-11-03 12:02:59 +00:00
|
|
|
const helmet = require('helmet')
|
|
|
|
const xss = require('xss-clean')
|
|
|
|
const cors = require('cors')
|
|
|
|
|
2023-10-09 17:22:48 +00:00
|
|
|
// routers
|
2023-11-03 12:02:59 +00:00
|
|
|
const nlRouter = require('./routes/naturalLanguageRoute')
|
2023-10-09 17:22:48 +00:00
|
|
|
|
|
|
|
const notFoundMiddlware = require('./middleware/not-found')
|
|
|
|
const errorHandlerMiddleware = require('./middleware/error-handler')
|
|
|
|
|
|
|
|
//middleware
|
|
|
|
app.set('trust proxy', 1)
|
|
|
|
app.use(rateLimiter({
|
|
|
|
windowMs: 15 * 60 * 1000,
|
|
|
|
max: 60,
|
|
|
|
}))
|
2023-11-03 12:02:59 +00:00
|
|
|
|
2023-10-09 17:22:48 +00:00
|
|
|
// Security packages
|
|
|
|
app.use(helmet())
|
|
|
|
app.use(cors())
|
2023-10-10 13:58:54 +00:00
|
|
|
app.use(xss())
|
2023-10-09 17:22:48 +00:00
|
|
|
|
|
|
|
app.use(morgan('tiny'))
|
2023-10-10 13:58:54 +00:00
|
|
|
app.use(express.json())
|
2023-11-03 12:02:59 +00:00
|
|
|
|
2023-10-09 17:22:48 +00:00
|
|
|
// app.use(fileUpload())
|
|
|
|
|
|
|
|
app.get('/', (req, res) => {
|
2023-10-10 13:58:54 +00:00
|
|
|
res.send('<h1>Sentiment API</h1><a href="/api-docs">Documentation</a>')
|
2023-11-03 12:02:59 +00:00
|
|
|
})
|
2023-10-09 17:22:48 +00:00
|
|
|
|
2023-10-10 13:58:54 +00:00
|
|
|
app.use('/api-docs', swaggerUI.serve, swaggerUI.setup(swaggerDocument))
|
2023-11-03 12:02:59 +00:00
|
|
|
|
2023-10-09 17:22:48 +00:00
|
|
|
app.use('/api/v1/nl', nlRouter)
|
|
|
|
|
|
|
|
app.use(notFoundMiddlware)
|
|
|
|
app.use(errorHandlerMiddleware)
|
|
|
|
|
|
|
|
const port = process.env.PORT || 3000
|
|
|
|
|
|
|
|
app.listen(port, console.log(`Listening on port: ${port}...`))
|