import express from 'express'
import bodyParser from 'body-parser'
<% if(useCors) { -%>
import cors from 'cors'
<% } -%>
<% if(useJwt) { -%>
import jwt from 'express-jwt'
<% } -%>
import { Routes } from './routes'
import { errorHandler<% if(useCors) { %>, CORS_OPTIONS<% } if(useJwt) { %>, JWT_CONFIG<% } if(useMongoose) { %>, connectMongo<% } %> } from './config'

// Initiate express
const app = express()

<% if(useCors) { -%>
app.use(cors(CORS_OPTIONS))
<% } -%>
// Parse incoming request body
app.use(bodyParser.json({ limit: '5mb' }))
app.use(bodyParser.urlencoded({ limit: '5mb', extended: true, parameterLimit: 50 }))

<% if(useJwt) { -%>
// jwt middleware
app.use(jwt({ secret: JWT_CONFIG.SECRET }).unless({ path: JWT_CONFIG.NO_AUTH_PATHS }))

<% } -%>
<% if(useMongoose) { -%>
// Create MongoDB connection
connectMongo()

<% } -%>
// Initiate all routes
Routes(app)

// Application level error handler
app.use(errorHandler)

export { app }
