diff options
author | Chocobozzz <me@florianbigard.com> | 2019-07-25 16:56:41 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2019-07-25 16:56:41 +0200 |
commit | 552d95b1e69fbbd99f5bc300a127457e1b97b9df (patch) | |
tree | 1579d43f5265916df94fabb5b516b7640b4289aa | |
parent | c8861d5dc0436ef4342ce517241e3591fa256a13 (diff) | |
download | PeerTube-552d95b1e69fbbd99f5bc300a127457e1b97b9df.tar.gz PeerTube-552d95b1e69fbbd99f5bc300a127457e1b97b9df.tar.zst PeerTube-552d95b1e69fbbd99f5bc300a127457e1b97b9df.zip |
Move controller in dedicated functions
-rw-r--r-- | server/controllers/client.ts | 40 |
1 files changed, 22 insertions, 18 deletions
diff --git a/server/controllers/client.ts b/server/controllers/client.ts index f51470b41..18b8b58e9 100644 --- a/server/controllers/client.ts +++ b/server/controllers/client.ts | |||
@@ -48,7 +48,26 @@ for (const staticClientFile of staticClientFiles) { | |||
48 | clientsRouter.use('/client', express.static(distPath, { maxAge: STATIC_MAX_AGE })) | 48 | clientsRouter.use('/client', express.static(distPath, { maxAge: STATIC_MAX_AGE })) |
49 | clientsRouter.use('/client/assets/images', express.static(assetsImagesPath, { maxAge: STATIC_MAX_AGE })) | 49 | clientsRouter.use('/client/assets/images', express.static(assetsImagesPath, { maxAge: STATIC_MAX_AGE })) |
50 | 50 | ||
51 | clientsRouter.use('/client/locales/:locale/:file.json', function (req, res) { | 51 | clientsRouter.use('/client/locales/:locale/:file.json', serveServerTranslations) |
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) { | ||
52 | const locale = req.params.locale | 71 | const locale = req.params.locale |
53 | const file = req.params.file | 72 | const file = req.params.file |
54 | 73 | ||
@@ -59,16 +78,9 @@ clientsRouter.use('/client/locales/:locale/:file.json', function (req, res) { | |||
59 | } | 78 | } |
60 | 79 | ||
61 | return res.sendStatus(404) | 80 | return res.sendStatus(404) |
62 | }) | 81 | } |
63 | |||
64 | // 404 for static files not found | ||
65 | clientsRouter.use('/client/*', (req: express.Request, res: express.Response, next: express.NextFunction) => { | ||
66 | res.sendStatus(404) | ||
67 | }) | ||
68 | 82 | ||
69 | // Always serve index client page (the client is a single page application, let it handle routing) | 83 | async function serveIndexHTML (req: express.Request, res: express.Response) { |
70 | // Try to provide the right language index.html | ||
71 | clientsRouter.use('/(:language)?', async function (req, res) { | ||
72 | if (req.accepts(ACCEPT_HEADERS) === 'html') { | 84 | if (req.accepts(ACCEPT_HEADERS) === 'html') { |
73 | try { | 85 | try { |
74 | await generateHTMLPage(req, res, req.params.language) | 86 | await generateHTMLPage(req, res, req.params.language) |
@@ -79,16 +91,8 @@ clientsRouter.use('/(:language)?', async function (req, res) { | |||
79 | } | 91 | } |
80 | 92 | ||
81 | return res.status(404).end() | 93 | return res.status(404).end() |
82 | }) | ||
83 | |||
84 | // --------------------------------------------------------------------------- | ||
85 | |||
86 | export { | ||
87 | clientsRouter | ||
88 | } | 94 | } |
89 | 95 | ||
90 | // --------------------------------------------------------------------------- | ||
91 | |||
92 | async function generateHTMLPage (req: express.Request, res: express.Response, paramLang?: string) { | 96 | async function generateHTMLPage (req: express.Request, res: express.Response, paramLang?: string) { |
93 | const html = await ClientHtml.getDefaultHTMLPage(req, res, paramLang) | 97 | const html = await ClientHtml.getDefaultHTMLPage(req, res, paramLang) |
94 | 98 | ||