sepia-search-motore-di-rice.../server.ts

74 lines
1.9 KiB
TypeScript

import { isTestInstance } from './server/helpers/core-utils'
import * as bodyParser from 'body-parser'
import * as express from 'express'
import * as cors from 'cors'
import * as morgan from 'morgan'
import { apiRouter } from './server/controllers/api'
import { logger } from './server/helpers/logger'
import { API_VERSION, CONFIG } from './server/initializers/constants'
import { VideosIndexer } from './server/lib/schedulers/videos-indexer'
import { initVideosIndex } from './server/lib/elastic-search-videos'
import { initChannelsIndex } from './server/lib/elastic-search-channels'
if (isTestInstance()) {
require('source-map-support').install()
}
const app = express()
app.use(morgan('combined', {
stream: { write: logger.info.bind(logger) }
}))
app.use(bodyParser.json({
type: [ 'application/json', 'application/*+json' ],
limit: '500kb'
}))
app.use(bodyParser.urlencoded({ extended: false }))
// ----------- Views, routes and static files -----------
app.use(cors())
const apiRoute = '/api/' + API_VERSION
app.use(apiRoute, apiRouter)
// ----------- Errors -----------
// Catch 404 and forward to error handler
app.use(function (req, res, next) {
const err = new Error('Not Found')
err['status'] = 404
next(err)
})
app.use(function (err, req, res, next) {
let error = 'Unknown error.'
if (err) {
error = err.stack || err.message || err
}
logger.error('Error in controller.', { error })
return res.status(err.status || 500).end()
})
// ----------- Run -----------
app.listen(CONFIG.LISTEN.PORT, async () => {
logger.info('Server listening on port %d', CONFIG.LISTEN.PORT)
try {
await Promise.all([
initVideosIndex(),
initChannelsIndex()
])
} catch (err) {
logger.error('Cannot init videos index.', { err })
process.exit(-1)
}
VideosIndexer.Instance.enable()
VideosIndexer.Instance.execute()
.catch(err => logger.error('Cannot run video indexer', { err }))
})