]>
Commit | Line | Data |
---|---|---|
4d4e5cd4 | 1 | import * as express from 'express' |
65fcc311 | 2 | import { join } from 'path' |
e032aec9 | 3 | import { root } from '../helpers/core-utils' |
34dd7cb4 | 4 | import { ACCEPT_HEADERS, STATIC_MAX_AGE } from '../initializers/constants' |
dfab4fa9 | 5 | import { asyncMiddleware, embedCSP } from '../middlewares' |
e032aec9 C |
6 | import { buildFileLocale, getCompleteLocale, is18nLocale, LOCALE_FILES } from '../../shared/models/i18n/i18n' |
7 | import { ClientHtml } from '../lib/client-html' | |
57c36b27 | 8 | import { logger } from '../helpers/logger' |
dfab4fa9 | 9 | import { CONFIG } from '@server/initializers/config' |
830bcd0f | 10 | |
65fcc311 | 11 | const clientsRouter = express.Router() |
830bcd0f | 12 | |
e02643f3 C |
13 | const distPath = join(root(), 'client', 'dist') |
14 | const embedPath = join(distPath, 'standalone', 'videos', 'embed.html') | |
99941732 | 15 | const testEmbedPath = join(distPath, 'standalone', 'videos', 'test-embed.html') |
830bcd0f | 16 | |
d8755eed | 17 | // Special route that add OpenGraph and oEmbed tags |
830bcd0f | 18 | // Do not use a template engine for a so little thing |
9aac4423 | 19 | clientsRouter.use('/videos/watch/:id', asyncMiddleware(generateWatchHtmlPage)) |
92bf2f62 C |
20 | clientsRouter.use('/accounts/:nameWithHost', asyncMiddleware(generateAccountHtmlPage)) |
21 | clientsRouter.use('/video-channels/:nameWithHost', asyncMiddleware(generateVideoChannelHtmlPage)) | |
830bcd0f | 22 | |
dfab4fa9 C |
23 | const embedCSPMiddleware = CONFIG.CSP.ENABLED |
24 | ? embedCSP | |
25 | : (req: express.Request, res: express.Response, next: express.NextFunction) => next() | |
26 | ||
9aac4423 | 27 | clientsRouter.use( |
d00e2393 | 28 | '/videos/embed', |
dfab4fa9 | 29 | embedCSPMiddleware, |
9aac4423 | 30 | (req: express.Request, res: express.Response) => { |
d00e2393 RK |
31 | res.removeHeader('X-Frame-Options') |
32 | res.sendFile(embedPath) | |
33 | } | |
34 | ) | |
9aac4423 C |
35 | clientsRouter.use( |
36 | '/videos/test-embed', | |
37 | (req: express.Request, res: express.Response) => res.sendFile(testEmbedPath) | |
38 | ) | |
830bcd0f | 39 | |
79530164 | 40 | // Static HTML/CSS/JS client files |
78967fca C |
41 | |
42 | const staticClientFiles = [ | |
fcc7c060 | 43 | 'manifest.webmanifest', |
78967fca C |
44 | 'ngsw-worker.js', |
45 | 'ngsw.json' | |
46 | ] | |
47 | for (const staticClientFile of staticClientFiles) { | |
48 | const path = join(root(), 'client', 'dist', staticClientFile) | |
78967fca | 49 | |
cd4cb177 C |
50 | clientsRouter.get('/' + staticClientFile, (req: express.Request, res: express.Response) => { |
51 | res.sendFile(path, { maxAge: STATIC_MAX_AGE.SERVER }) | |
52 | }) | |
53 | } | |
79530164 | 54 | |
552d95b1 | 55 | clientsRouter.use('/client/locales/:locale/:file.json', serveServerTranslations) |
cd4cb177 | 56 | clientsRouter.use('/client', express.static(distPath, { maxAge: STATIC_MAX_AGE.CLIENT })) |
552d95b1 C |
57 | |
58 | // 404 for static files not found | |
59 | clientsRouter.use('/client/*', (req: express.Request, res: express.Response) => { | |
60 | res.sendStatus(404) | |
61 | }) | |
62 | ||
63 | // Always serve index client page (the client is a single page application, let it handle routing) | |
64 | // Try to provide the right language index.html | |
65 | clientsRouter.use('/(:language)?', asyncMiddleware(serveIndexHTML)) | |
66 | ||
67 | // --------------------------------------------------------------------------- | |
68 | ||
69 | export { | |
70 | clientsRouter | |
71 | } | |
72 | ||
73 | // --------------------------------------------------------------------------- | |
74 | ||
a1587156 | 75 | function serveServerTranslations (req: express.Request, res: express.Response) { |
7ce44a74 C |
76 | const locale = req.params.locale |
77 | const file = req.params.file | |
78 | ||
bdd428a6 | 79 | if (is18nLocale(locale) && LOCALE_FILES.includes(file)) { |
74b7c6d4 C |
80 | const completeLocale = getCompleteLocale(locale) |
81 | const completeFileLocale = buildFileLocale(completeLocale) | |
cd4cb177 | 82 | |
350131cb | 83 | const path = join(__dirname, `../../../client/dist/locale/${file}.${completeFileLocale}.json`) |
cd4cb177 | 84 | return res.sendFile(path, { maxAge: STATIC_MAX_AGE.SERVER }) |
e945b184 C |
85 | } |
86 | ||
87 | return res.sendStatus(404) | |
552d95b1 | 88 | } |
79530164 | 89 | |
552d95b1 | 90 | async function serveIndexHTML (req: express.Request, res: express.Response) { |
989e526a | 91 | if (req.accepts(ACCEPT_HEADERS) === 'html') { |
57c36b27 C |
92 | try { |
93 | await generateHTMLPage(req, res, req.params.language) | |
94 | return | |
95 | } catch (err) { | |
96 | logger.error('Cannot generate HTML page.', err) | |
97 | } | |
989e526a C |
98 | } |
99 | ||
100 | return res.status(404).end() | |
65fcc311 | 101 | } |
830bcd0f | 102 | |
e032aec9 | 103 | async function generateHTMLPage (req: express.Request, res: express.Response, paramLang?: string) { |
9aac4423 | 104 | const html = await ClientHtml.getDefaultHTMLPage(req, res, paramLang) |
989e526a | 105 | |
e032aec9 | 106 | return sendHTML(html, res) |
989e526a C |
107 | } |
108 | ||
e032aec9 C |
109 | async function generateWatchHtmlPage (req: express.Request, res: express.Response) { |
110 | const html = await ClientHtml.getWatchHTMLPage(req.params.id + '', req, res) | |
830bcd0f | 111 | |
e032aec9 | 112 | return sendHTML(html, res) |
830bcd0f C |
113 | } |
114 | ||
92bf2f62 C |
115 | async function generateAccountHtmlPage (req: express.Request, res: express.Response) { |
116 | const html = await ClientHtml.getAccountHTMLPage(req.params.nameWithHost, req, res) | |
117 | ||
118 | return sendHTML(html, res) | |
119 | } | |
120 | ||
121 | async function generateVideoChannelHtmlPage (req: express.Request, res: express.Response) { | |
122 | const html = await ClientHtml.getVideoChannelHTMLPage(req.params.nameWithHost, req, res) | |
123 | ||
124 | return sendHTML(html, res) | |
125 | } | |
126 | ||
e032aec9 C |
127 | function sendHTML (html: string, res: express.Response) { |
128 | res.set('Content-Type', 'text/html; charset=UTF-8') | |
eb080476 | 129 | |
e032aec9 | 130 | return res.send(html) |
830bcd0f | 131 | } |