'use strict' import http from 'http' import express, { Response, Request } from 'express' import bodyParser from 'body-parser' import { initialize } from 'express-openapi' import swaggerUi from 'swagger-ui-express' import path from 'path' import cors from 'cors' import { initPassport, isAuthEnabled, initAllowedUsers } from './auth' require('dotenv').config() const app = express() const server = http.createServer(app) app.use(cors()) app.use(bodyParser.json({ limit: '5mb' })) const passport = initPassport() app.use(passport.initialize()) if (isAuthEnabled()) { initAllowedUsers() } else { console.warn('No Auth0 env variables found - starting unauthenticated API endpoint!') } const swaggerConfig = require('./config/swagger.json') initialize({ app, apiDoc: swaggerConfig, errorMiddleware: (err, req, res, next) => { console.log(JSON.stringify(err.errors, undefined, ' ')) res.status(err.status).send(err.errors) }, paths: path.resolve('./handlers'), routesGlob: '**/*.[jt]s', routesIndexFileRegExp: /(?:index)?\.[jt]s$/ }) // custom error handler (DO NOT DELETE "next" PARAM) app.use((err: Error, req: Request, res: Response, next: Function) => { res.status(500).send(JSON.stringify(err, Object.getOwnPropertyNames(err))) }) app.use('/api/docs', swaggerUi.serve, swaggerUi.setup(swaggerConfig)) const port = process.env.PORT || 8000 server.listen(port, function() { console.log('feedbot-server listening at port :' + port) })