]>
Commit | Line | Data |
---|---|---|
4d4e5cd4 | 1 | import * as express from 'express' |
65fcc311 | 2 | import { join } from 'path' |
e032aec9 C |
3 | import { root } from '../helpers/core-utils' |
4 | import { ACCEPT_HEADERS, STATIC_MAX_AGE } from '../initializers' | |
eb080476 | 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' | |
830bcd0f | 8 | |
65fcc311 | 9 | const clientsRouter = express.Router() |
830bcd0f | 10 | |
e02643f3 | 11 | const distPath = join(root(), 'client', 'dist') |
1f30a185 | 12 | const assetsImagesPath = join(root(), 'client', 'dist', 'assets', 'images') |
e02643f3 | 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 |
eb080476 C |
18 | clientsRouter.use('/videos/watch/:id', |
19 | asyncMiddleware(generateWatchHtmlPage) | |
20 | ) | |
830bcd0f | 21 | |
8afc19a6 | 22 | clientsRouter.use('' + |
d00e2393 RK |
23 | '/videos/embed', |
24 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
25 | res.removeHeader('X-Frame-Options') | |
26 | res.sendFile(embedPath) | |
27 | } | |
28 | ) | |
99941732 WL |
29 | clientsRouter.use('' + |
30 | '/videos/test-embed', (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
31 | res.sendFile(testEmbedPath) | |
32 | }) | |
830bcd0f | 33 | |
79530164 | 34 | // Static HTML/CSS/JS client files |
78967fca C |
35 | |
36 | const staticClientFiles = [ | |
37 | 'manifest.json', | |
38 | 'ngsw-worker.js', | |
39 | 'ngsw.json' | |
40 | ] | |
41 | for (const staticClientFile of staticClientFiles) { | |
42 | const path = join(root(), 'client', 'dist', staticClientFile) | |
43 | clientsRouter.use('/' + staticClientFile, express.static(path, { maxAge: STATIC_MAX_AGE })) | |
44 | } | |
45 | ||
65fcc311 | 46 | clientsRouter.use('/client', express.static(distPath, { maxAge: STATIC_MAX_AGE })) |
6bafac54 | 47 | clientsRouter.use('/client/assets/images', express.static(assetsImagesPath, { maxAge: STATIC_MAX_AGE })) |
79530164 | 48 | |
e945b184 | 49 | clientsRouter.use('/client/locales/:locale/:file.json', function (req, res) { |
7ce44a74 C |
50 | const locale = req.params.locale |
51 | const file = req.params.file | |
52 | ||
74b7c6d4 C |
53 | if (is18nLocale(locale) && LOCALE_FILES.indexOf(file) !== -1) { |
54 | const completeLocale = getCompleteLocale(locale) | |
55 | const completeFileLocale = buildFileLocale(completeLocale) | |
56 | return res.sendFile(join(__dirname, `../../../client/dist/locale/${file}_${completeFileLocale}.json`)) | |
e945b184 C |
57 | } |
58 | ||
59 | return res.sendStatus(404) | |
60 | }) | |
61 | ||
79530164 | 62 | // 404 for static files not found |
075f16ca | 63 | clientsRouter.use('/client/*', (req: express.Request, res: express.Response, next: express.NextFunction) => { |
79530164 C |
64 | res.sendStatus(404) |
65 | }) | |
66 | ||
989e526a C |
67 | // Always serve index client page (the client is a single page application, let it handle routing) |
68 | // Try to provide the right language index.html | |
69 | clientsRouter.use('/(:language)?', function (req, res) { | |
70 | if (req.accepts(ACCEPT_HEADERS) === 'html') { | |
e032aec9 | 71 | return generateHTMLPage(req, res, req.params.language) |
989e526a C |
72 | } |
73 | ||
74 | return res.status(404).end() | |
75 | }) | |
76 | ||
830bcd0f C |
77 | // --------------------------------------------------------------------------- |
78 | ||
65fcc311 C |
79 | export { |
80 | clientsRouter | |
81 | } | |
830bcd0f C |
82 | |
83 | // --------------------------------------------------------------------------- | |
84 | ||
e032aec9 C |
85 | async function generateHTMLPage (req: express.Request, res: express.Response, paramLang?: string) { |
86 | const html = await ClientHtml.getIndexHTML(req, res) | |
989e526a | 87 | |
e032aec9 | 88 | return sendHTML(html, res) |
989e526a C |
89 | } |
90 | ||
e032aec9 C |
91 | async function generateWatchHtmlPage (req: express.Request, res: express.Response) { |
92 | const html = await ClientHtml.getWatchHTMLPage(req.params.id + '', req, res) | |
830bcd0f | 93 | |
e032aec9 | 94 | return sendHTML(html, res) |
830bcd0f C |
95 | } |
96 | ||
e032aec9 C |
97 | function sendHTML (html: string, res: express.Response) { |
98 | res.set('Content-Type', 'text/html; charset=UTF-8') | |
eb080476 | 99 | |
e032aec9 | 100 | return res.send(html) |
830bcd0f | 101 | } |