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