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

69 lines
1.7 KiB
TypeScript

import { isTestInstance } from './server/helpers/core-utils'
if (isTestInstance()) {
require('source-map-support').install()
}
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/helpers/elastic-search'
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 initVideosIndex()
} catch (err) {
logger.error('Cannot init videos index.', { err })
process.exit(-1)
}
VideosIndexer.Instance.enable()
VideosIndexer.Instance.execute()
})