]>
Commit | Line | Data |
---|---|---|
4d4e5cd4 | 1 | import * as express from 'express' |
bd45d503 | 2 | import { constants, promises as fs } from 'fs' |
65fcc311 | 3 | import { join } from 'path' |
bd45d503 C |
4 | import { CONFIG } from '@server/initializers/config' |
5 | import { buildFileLocale, getCompleteLocale, is18nLocale, LOCALE_FILES } from '@shared/core-utils/i18n' | |
f2eb23cd | 6 | import { HttpStatusCode } from '@shared/core-utils' |
e032aec9 | 7 | import { root } from '../helpers/core-utils' |
f2eb23cd RK |
8 | import { STATIC_MAX_AGE } from '../initializers/constants' |
9 | import { ClientHtml, sendHTML, serveIndexHTML } from '../lib/client-html' | |
bd45d503 | 10 | import { asyncMiddleware, embedCSP } from '../middlewares' |
830bcd0f | 11 | |
65fcc311 | 12 | const clientsRouter = express.Router() |
830bcd0f | 13 | |
e02643f3 | 14 | const distPath = join(root(), 'client', 'dist') |
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 |
8d987ec6 | 19 | clientsRouter.use('/videos/watch/playlist/:id', asyncMiddleware(generateWatchPlaylistHtmlPage)) |
9aac4423 | 20 | clientsRouter.use('/videos/watch/:id', asyncMiddleware(generateWatchHtmlPage)) |
92bf2f62 C |
21 | clientsRouter.use('/accounts/:nameWithHost', asyncMiddleware(generateAccountHtmlPage)) |
22 | clientsRouter.use('/video-channels/:nameWithHost', asyncMiddleware(generateVideoChannelHtmlPage)) | |
830bcd0f | 23 | |
5abc96fc C |
24 | const embedMiddlewares = [ |
25 | CONFIG.CSP.ENABLED | |
26 | ? embedCSP | |
27 | : (req: express.Request, res: express.Response, next: express.NextFunction) => next(), | |
dfab4fa9 | 28 | |
cf649c2e | 29 | (req: express.Request, res: express.Response, next: express.NextFunction) => { |
d00e2393 | 30 | res.removeHeader('X-Frame-Options') |
cf649c2e | 31 | |
78646451 | 32 | // Don't cache HTML file since it's an index to the immutable JS/CSS files |
cf649c2e C |
33 | res.setHeader('Cache-Control', 'public, max-age=0') |
34 | ||
35 | next() | |
36 | }, | |
37 | ||
38 | asyncMiddleware(generateEmbedHtmlPage) | |
5abc96fc C |
39 | ] |
40 | ||
41 | clientsRouter.use('/videos/embed', ...embedMiddlewares) | |
42 | clientsRouter.use('/video-playlists/embed', ...embedMiddlewares) | |
9054a8b6 C |
43 | |
44 | const testEmbedController = (req: express.Request, res: express.Response) => res.sendFile(testEmbedPath) | |
45 | ||
46 | clientsRouter.use('/videos/test-embed', testEmbedController) | |
47 | clientsRouter.use('/video-playlists/test-embed', testEmbedController) | |
830bcd0f | 48 | |
79530164 | 49 | // Static HTML/CSS/JS client files |
78967fca | 50 | const staticClientFiles = [ |
78967fca C |
51 | 'ngsw-worker.js', |
52 | 'ngsw.json' | |
53 | ] | |
caf2aaf4 | 54 | |
78967fca C |
55 | for (const staticClientFile of staticClientFiles) { |
56 | const path = join(root(), 'client', 'dist', staticClientFile) | |
78967fca | 57 | |
caf2aaf4 | 58 | clientsRouter.get(`/${staticClientFile}`, (req: express.Request, res: express.Response) => { |
cd4cb177 C |
59 | res.sendFile(path, { maxAge: STATIC_MAX_AGE.SERVER }) |
60 | }) | |
61 | } | |
79530164 | 62 | |
caf2aaf4 K |
63 | // Dynamic PWA manifest |
64 | clientsRouter.get('/manifest.webmanifest', asyncMiddleware(generateManifest)) | |
65 | ||
66 | // Static client overrides | |
8872828d | 67 | // Must be consistent with static client overrides redirections in /support/nginx/peertube |
caf2aaf4 K |
68 | const staticClientOverrides = [ |
69 | 'assets/images/logo.svg', | |
70 | 'assets/images/favicon.png', | |
71 | 'assets/images/icons/icon-36x36.png', | |
72 | 'assets/images/icons/icon-48x48.png', | |
73 | 'assets/images/icons/icon-72x72.png', | |
74 | 'assets/images/icons/icon-96x96.png', | |
75 | 'assets/images/icons/icon-144x144.png', | |
76 | 'assets/images/icons/icon-192x192.png', | |
77 | 'assets/images/icons/icon-512x512.png' | |
78 | ] | |
79 | ||
80 | for (const staticClientOverride of staticClientOverrides) { | |
81 | const overridePhysicalPath = join(CONFIG.STORAGE.CLIENT_OVERRIDES_DIR, staticClientOverride) | |
82 | clientsRouter.use(`/client/${staticClientOverride}`, asyncMiddleware(serveClientOverride(overridePhysicalPath))) | |
83 | } | |
84 | ||
552d95b1 | 85 | clientsRouter.use('/client/locales/:locale/:file.json', serveServerTranslations) |
cd4cb177 | 86 | clientsRouter.use('/client', express.static(distPath, { maxAge: STATIC_MAX_AGE.CLIENT })) |
552d95b1 C |
87 | |
88 | // 404 for static files not found | |
89 | clientsRouter.use('/client/*', (req: express.Request, res: express.Response) => { | |
2d53be02 | 90 | res.sendStatus(HttpStatusCode.NOT_FOUND_404) |
552d95b1 C |
91 | }) |
92 | ||
93 | // Always serve index client page (the client is a single page application, let it handle routing) | |
94 | // Try to provide the right language index.html | |
95 | clientsRouter.use('/(:language)?', asyncMiddleware(serveIndexHTML)) | |
96 | ||
97 | // --------------------------------------------------------------------------- | |
98 | ||
99 | export { | |
100 | clientsRouter | |
101 | } | |
102 | ||
103 | // --------------------------------------------------------------------------- | |
104 | ||
a1587156 | 105 | function serveServerTranslations (req: express.Request, res: express.Response) { |
7ce44a74 C |
106 | const locale = req.params.locale |
107 | const file = req.params.file | |
108 | ||
bdd428a6 | 109 | if (is18nLocale(locale) && LOCALE_FILES.includes(file)) { |
74b7c6d4 C |
110 | const completeLocale = getCompleteLocale(locale) |
111 | const completeFileLocale = buildFileLocale(completeLocale) | |
cd4cb177 | 112 | |
350131cb | 113 | const path = join(__dirname, `../../../client/dist/locale/${file}.${completeFileLocale}.json`) |
cd4cb177 | 114 | return res.sendFile(path, { maxAge: STATIC_MAX_AGE.SERVER }) |
e945b184 C |
115 | } |
116 | ||
2d53be02 | 117 | return res.sendStatus(HttpStatusCode.NOT_FOUND_404) |
552d95b1 | 118 | } |
79530164 | 119 | |
cf649c2e C |
120 | async function generateEmbedHtmlPage (req: express.Request, res: express.Response) { |
121 | const html = await ClientHtml.getEmbedHTML() | |
122 | ||
123 | return sendHTML(html, res) | |
124 | } | |
125 | ||
e032aec9 C |
126 | async function generateWatchHtmlPage (req: express.Request, res: express.Response) { |
127 | const html = await ClientHtml.getWatchHTMLPage(req.params.id + '', req, res) | |
830bcd0f | 128 | |
e032aec9 | 129 | return sendHTML(html, res) |
830bcd0f C |
130 | } |
131 | ||
8d987ec6 K |
132 | async function generateWatchPlaylistHtmlPage (req: express.Request, res: express.Response) { |
133 | const html = await ClientHtml.getWatchPlaylistHTMLPage(req.params.id + '', req, res) | |
134 | ||
135 | return sendHTML(html, res) | |
136 | } | |
137 | ||
92bf2f62 C |
138 | async function generateAccountHtmlPage (req: express.Request, res: express.Response) { |
139 | const html = await ClientHtml.getAccountHTMLPage(req.params.nameWithHost, req, res) | |
140 | ||
141 | return sendHTML(html, res) | |
142 | } | |
143 | ||
144 | async function generateVideoChannelHtmlPage (req: express.Request, res: express.Response) { | |
145 | const html = await ClientHtml.getVideoChannelHTMLPage(req.params.nameWithHost, req, res) | |
146 | ||
147 | return sendHTML(html, res) | |
148 | } | |
149 | ||
caf2aaf4 K |
150 | async function generateManifest (req: express.Request, res: express.Response) { |
151 | const manifestPhysicalPath = join(root(), 'client', 'dist', 'manifest.webmanifest') | |
152 | const manifestJson = await fs.readFile(manifestPhysicalPath, 'utf8') | |
153 | const manifest = JSON.parse(manifestJson) | |
154 | ||
155 | manifest.name = CONFIG.INSTANCE.NAME | |
156 | manifest.short_name = CONFIG.INSTANCE.NAME | |
157 | manifest.description = CONFIG.INSTANCE.SHORT_DESCRIPTION | |
158 | ||
159 | res.json(manifest) | |
160 | } | |
161 | ||
162 | function serveClientOverride (path: string) { | |
163 | return async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
164 | try { | |
165 | await fs.access(path, constants.F_OK) | |
166 | // Serve override client | |
167 | res.sendFile(path, { maxAge: STATIC_MAX_AGE.SERVER }) | |
168 | } catch { | |
169 | // Serve dist client | |
170 | next() | |
171 | } | |
172 | } | |
173 | } |