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