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

116 lines
3.4 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-08-27 14:44:21 +02:00
import { join } from 'path'
2020-09-02 14:07:22 +02:00
import { send } from 'process'
import { readFile } from 'fs-extra'
2020-02-19 15:39:35 +01:00
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)
2020-08-27 14:44:21 +02:00
// Static client files
app.use('/js/', express.static(join(__dirname, '../client/dist/js')))
app.use('/css/', express.static(join(__dirname, '../client/dist/css')))
app.use('/img/', express.static(join(__dirname, '../client/dist/img')))
2020-09-02 14:07:22 +02:00
let indexHTML: string
app.use('/*', async function (req, res) {
res.set('Content-Type', 'text/html; charset=UTF-8')
if (indexHTML) return res.send(indexHTML)
const buffer = await readFile(join(__dirname, '../client/dist/index.html'))
const url = CONFIG.WEBSERVER.SCHEME + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT
const title = CONFIG.SEARCH_INSTANCE.NAME
const description = CONFIG.SEARCH_INSTANCE.DESCRIPTION
const tags = `
<meta property="og:url" content="${url}">
<meta property="og:title" content="${title}">
<meta property="og:description" content="${description}">
<meta property="og:image" content="${url}/img/card-opengraph.jpg">
<meta property="og:site_name" content="${title}">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:site" content="@joinpeertube">
<meta name="twitter:creator" content="@chocobozzz">
<meta name="twitter:title" content="${title}">
<meta name="twitter:description" content="${description}">
<meta name="twitter:image" content="${url}/img/card-opengraph.jpg">`
indexHTML = buffer.toString()
indexHTML = indexHTML.replace('</head>', tags + '</head>')
return res.send(indexHTML)
2020-08-27 14:44:21 +02:00
})
2020-02-13 11:49:03 +01:00
// ----------- 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
})