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

164 lines
5.4 KiB
TypeScript
Raw Permalink 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()
}
2022-06-03 10:54:30 +02:00
import bodyParser from 'body-parser'
import express from 'express'
import cors from 'cors'
import morgan from 'morgan'
2020-02-13 11:49:03 +01:00
import { apiRouter } from './server/controllers/api'
import { logger } from './server/helpers/logger'
2020-09-02 16:24:25 +02:00
import { API_VERSION, CONFIG, getWebserverUrl } from './server/initializers/constants'
2021-06-24 15:18:54 +02:00
import { IndexationScheduler } from './server/lib/schedulers/indexation-scheduler'
2020-08-27 14:44:21 +02:00
import { join } from 'path'
2020-09-02 14:07:22 +02:00
import { readFile } from 'fs-extra'
2022-12-28 10:58:51 +01:00
import { getConfig } from './server/controllers/api/config'
2020-02-19 15:39:35 +01:00
2020-02-13 11:49:03 +01:00
const app = express()
2020-09-11 17:39:19 +02:00
const url = getWebserverUrl()
2020-02-13 11:49:03 +01:00
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
2020-09-22 09:24:10 +02:00
app.use('/img/', express.static(join(__dirname, '../client/dist/img'), { maxAge: '30d' }))
2022-02-23 15:21:02 +01:00
app.use('/assets/', express.static(join(__dirname, '../client/dist/assets'), { maxAge: '30d' }))
2020-09-22 09:24:10 +02:00
app.use('/theme/', express.static(join(__dirname, './themes'), { maxAge: '30d' }))
2020-09-02 14:07:22 +02:00
2020-09-11 17:39:19 +02:00
app.use('/opensearch.xml', async function (req, res) {
2020-09-18 16:29:32 +02:00
const data = `<?xml version="1.0" encoding="UTF-8" ?>
<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/" xmlns:moz="http://www.mozilla.org/2006/browser/search/">
<ShortName>${CONFIG.SEARCH_INSTANCE.NAME}</ShortName>
<Description>${CONFIG.SEARCH_INSTANCE.DESCRIPTION}</Description>
<Image height="64" width="64" type="image/png">${url}/img/favicon.png</Image>
<Language>*</Language>
<Tags>peertube video</Tags>
<Url type="text/html" method="get" rel="results" template="${url}/search?search={searchTerms}&amp;page={startPage?}"/>
<Url type="application/opensearchdescription+xml" rel="self" template="${url}/opensearch.xml"/>
<Query role="example" searchTerms="peertube"/>
<SyndicationRight>open</SyndicationRight>
<AdultContent>true</AdultContent>
<InputEncoding>UTF-8</InputEncoding>
<OutputEncoding>UTF-8</OutputEncoding>
<Developer>Framasoft: contact.framasoft.org</Developer>
</OpenSearchDescription>`
return res.type('application/xml').send(data).end()
2020-09-11 17:39:19 +02:00
})
2020-09-18 16:29:32 +02:00
let indexHTML: string
2020-09-02 14:07:22 +02:00
app.use('/*', async function (req, res) {
res.set('Content-Type', 'text/html; charset=UTF-8')
if (indexHTML) return res.send(indexHTML)
2020-09-18 16:29:32 +02:00
let bufferCSS: Buffer
if (CONFIG.SEARCH_INSTANCE.THEME !== 'default') {
try {
bufferCSS = await readFile(join(__dirname, 'themes', CONFIG.SEARCH_INSTANCE.THEME, 'index.css'))
} catch (err) {
logger.error({ err }, 'Cannot fetch CSS theme.')
}
}
2020-09-02 14:07:22 +02:00
const title = CONFIG.SEARCH_INSTANCE.NAME
const description = CONFIG.SEARCH_INSTANCE.DESCRIPTION
2020-09-18 16:29:32 +02:00
const styleCSS = bufferCSS
? `<style type="text/css">${bufferCSS.toString()}</style>`
: ''
2020-09-02 14:07:22 +02:00
2022-12-28 10:58:51 +01:00
// Stringify the JSON object, and then stringify the string object so we can inject it into the HTML
const config = getConfig()
const serverConfigString = JSON.stringify(JSON.stringify(config))
2022-12-28 10:58:51 +01:00
const configJS = `<script type="application/javascript">window.PeerTubeServerConfig = ${serverConfigString}</script>`
2020-09-02 14:07:22 +02:00
const tags = `
2020-09-16 11:47:02 +02:00
<title>${title}</title>
2020-09-02 16:29:46 +02:00
<meta name="description" content="${description}">
2020-09-02 16:20:18 +02:00
2020-09-02 14:07:22 +02:00
<meta property="og:url" content="${url}">
<meta property="og:title" content="${title}">
<meta property="og:description" content="${description}">
2020-09-02 16:25:34 +02:00
<meta property="og:image" content="${url}/img/card-opengraph.png">
2020-09-02 14:07:22 +02:00
<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}">
2020-09-11 17:39:19 +02:00
<meta name="twitter:image" content="${url}/img/card-opengraph.png">
2020-09-18 16:29:32 +02:00
<link rel="search" type="application/opensearchdescription+xml" title="${title}" href="${url}/opensearch.xml" />
2022-12-28 10:58:51 +01:00
${styleCSS}
${configJS}`
2020-09-18 16:29:32 +02:00
const buffer = await readFile(join(__dirname, '../client/dist/index.html'))
2020-09-02 14:07:22 +02:00
let html = buffer.toString()
html = html.replace('</head>', tags + '</head>')
2020-09-02 14:07:22 +02:00
// Prevent caching a not ready yet configuration (because the indexer did not have time to run)
if (config.indexedHostsCount !== 0) {
indexHTML = html
}
return res.send(html)
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
}
2023-02-08 09:50:20 +01:00
logger.error({ err: 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 {
2021-06-24 15:18:54 +02:00
await IndexationScheduler.Instance.initIndexes()
2020-02-13 16:06:52 +01:00
} catch (err) {
2023-11-13 11:06:50 +01:00
logger.error(err, 'Cannot init indexes.')
2020-02-13 16:06:52 +01:00
process.exit(-1)
}
2023-11-13 11:06:50 +01:00
IndexationScheduler.Instance.enable()
2021-06-24 15:18:54 +02:00
IndexationScheduler.Instance.execute()
2023-02-08 09:50:20 +01:00
.catch(err => logger.error(err, 'Cannot run video indexer'))
2020-02-13 11:49:03 +01:00
})