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

75 lines
1.9 KiB
TypeScript
Raw Normal View History

2020-02-13 11:49:03 +01:00
import { isTestInstance } from './server/helpers/core-utils'
2020-03-04 15:32:39 +01:00
if (isTestInstance()) {
require('source-map-support').install()
}
2020-02-13 11:49:03 +01:00
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'
2020-02-13 16:06:52 +01:00
import { VideosIndexer } from './server/lib/schedulers/videos-indexer'
2020-02-14 14:09:31 +01:00
import { initVideosIndex } from './server/lib/elastic-search-videos'
2020-02-19 15:39:35 +01:00
import { initChannelsIndex } from './server/lib/elastic-search-channels'
2020-02-13 11:49:03 +01:00
const app = express()
app.use(morgan('combined', {
stream: { write: logger.info.bind(logger) }
}))
app.use(bodyParser.json({
type: [ 'application/json', 'application/*+json' ],
2020-05-29 16:16:55 +02:00
limit: '5mb'
2020-02-13 11:49:03 +01:00
}))
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) {
2020-05-29 16:16:55 +02:00
const err = new Error('Not Found') as any
err.status = 404
2020-02-13 11:49:03 +01:00
next(err)
})
app.use(function (err, req, res, next) {
let error = 'Unknown error.'
if (err) {
error = err.stack || err.message || err
}
logger.error({ error }, 'Error in controller.')
2020-02-13 11:49:03 +01:00
return res.status(err.status || 500).end()
})
// ----------- Run -----------
2020-02-13 16:06:52 +01:00
app.listen(CONFIG.LISTEN.PORT, async () => {
2020-02-13 11:49:03 +01:00
logger.info('Server listening on port %d', CONFIG.LISTEN.PORT)
2020-02-13 16:06:52 +01:00
try {
2020-02-19 15:39:35 +01:00
await Promise.all([
initVideosIndex(),
initChannelsIndex()
])
2020-02-13 16:06:52 +01:00
} catch (err) {
logger.error('Cannot init videos index.', { err })
process.exit(-1)
}
VideosIndexer.Instance.enable()
VideosIndexer.Instance.execute()
2020-02-19 15:39:35 +01:00
.catch(err => logger.error('Cannot run video indexer', { err }))
2020-02-13 11:49:03 +01:00
})