index.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /* eslint-disable no-console */
  2. import express from 'express'
  3. import { OrionClient } from './api'
  4. import { APP_URL, PORT } from './config'
  5. import {
  6. generateChannelMetaTags,
  7. generateChannelSchemaTagsHtml,
  8. generateCommonMetaTags,
  9. generateVideoMetaTags,
  10. generateVideoSchemaTagsHtml,
  11. } from './tags'
  12. import { applyMetaTagsToHtml, applySchemaTagsToHtml, fetchHtmlAndAppData, generateAssetUrl } from './utils'
  13. const app = express()
  14. let orionClient: OrionClient
  15. app.get('/video/:id', async (req, res) => {
  16. try {
  17. const id = req.params['id']
  18. const [[html, appData], video] = await Promise.all([fetchHtmlAndAppData(APP_URL), orionClient.getVideo(id)])
  19. if (!video) {
  20. return res.status(404).send('Video not found')
  21. }
  22. const title = html.querySelector('title')
  23. if (title) {
  24. title.innerHTML = video.title || appData.name
  25. }
  26. const thumbnailUrl = video.thumbnailPhoto ? generateAssetUrl(video.thumbnailPhoto) : ''
  27. const videoMetaTags = generateVideoMetaTags(video, thumbnailUrl, appData.name, APP_URL, appData.twitterId)
  28. const videoSchemaTagsHtml = generateVideoSchemaTagsHtml(video, thumbnailUrl, appData.name, APP_URL)
  29. applyMetaTagsToHtml(html, videoMetaTags)
  30. applySchemaTagsToHtml(html, videoSchemaTagsHtml)
  31. return res.send(html.toString())
  32. } catch (err) {
  33. console.error(err)
  34. return res.status(500).send()
  35. }
  36. })
  37. app.get('/channel/:id', async (req, res) => {
  38. try {
  39. const id = req.params['id']
  40. const [[html, appData], channel] = await Promise.all([fetchHtmlAndAppData(APP_URL), orionClient.getChannel(id)])
  41. if (!channel) {
  42. return res.status(404).send('Channel not found')
  43. }
  44. const title = html.querySelector('title')
  45. if (title) {
  46. title.innerHTML = channel.title || appData.name
  47. }
  48. const avatarUrl = channel.avatarPhoto ? generateAssetUrl(channel.avatarPhoto) : ''
  49. const channelMetaTags = generateChannelMetaTags(channel, avatarUrl, appData.name, APP_URL, appData.twitterId)
  50. const channelSchemaTagsHtml = generateChannelSchemaTagsHtml(channel, avatarUrl, APP_URL)
  51. applyMetaTagsToHtml(html, channelMetaTags)
  52. applySchemaTagsToHtml(html, channelSchemaTagsHtml)
  53. return res.send(html.toString())
  54. } catch (err) {
  55. console.error(err)
  56. return res.status(500).send()
  57. }
  58. })
  59. app.get('/ypp', async (req, res) => {
  60. try {
  61. const [html, appData] = await fetchHtmlAndAppData(APP_URL)
  62. const titleNode = html.querySelector('title')
  63. if (titleNode && appData.yppOgTitle) {
  64. titleNode.innerHTML = appData.yppOgTitle
  65. }
  66. const metaTags = generateCommonMetaTags(
  67. appData.name,
  68. APP_URL,
  69. appData.yppOgTitle || appData.name,
  70. appData.yppOgDescription,
  71. appData.yppOgImage,
  72. appData.twitterId
  73. )
  74. applyMetaTagsToHtml(html, metaTags)
  75. return res.send(html.toString())
  76. } catch (err) {
  77. console.error(err)
  78. return res.status(500).send()
  79. }
  80. })
  81. const init = async () => {
  82. console.log('Initializing...')
  83. console.log(`Fetching app data from ${APP_URL}...`)
  84. const [html, appData] = await fetchHtmlAndAppData(APP_URL)
  85. console.log('App data fetched')
  86. console.log(JSON.stringify(appData, null, 2))
  87. console.log('Initializing Orion client...')
  88. orionClient = new OrionClient(appData.orionUrl)
  89. await orionClient.testConnection()
  90. console.log('Orion client initialized')
  91. app.get('/*', (req, res) => {
  92. res.send(html.toString())
  93. })
  94. app
  95. .listen(PORT, () => console.log('Listening on ' + PORT + '...'))
  96. .on('error', (error) => console.log('Error during app startup', error))
  97. }
  98. init()