|
20 | 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
21 | 21 | // SOFTWARE. |
22 | 22 |
|
23 | | -const fs = require('fs'); |
24 | | -const path = require('path'); |
| 23 | +const config = require('config'); |
| 24 | +const errors = require('restify-errors'); |
| 25 | + |
| 26 | +const staticHandlerInstance = require('../../lib/StaticHandler').getHandler(); |
| 27 | +const StaticDocumentTypes = require('../../lib/StaticDocumentTypes'); |
| 28 | + |
| 29 | +const sitemapSorter = (a, b) => ( |
| 30 | + // eslint-disable-next-line no-nested-ternary |
| 31 | + (a.page_name > b.page_name) |
| 32 | + ? 1 |
| 33 | + : ((a.page_name < [ 'page_name' ]) ? -1 : 0)); |
25 | 34 |
|
26 | 35 | const displaySitemap = async (req, res, next) => { |
27 | | - const robots = fs.readFileSync(path.join(__dirname, '../../seo/sitemap.txt')); |
28 | | - res.contentType = 'text/plain'; |
29 | | - res.header('Content-Type', 'text/plain'); |
30 | | - res.send(200, robots); |
31 | | - next(); |
| 36 | + try { |
| 37 | + const baseLink = `${ |
| 38 | + config.get('app.http2.enabled') ? 'https' : 'http' |
| 39 | + }://${config.get('app.hostname')}:${config.get('app.port')}`; |
| 40 | + |
| 41 | + const sitemapItems = await staticHandlerInstance.findStatic(StaticDocumentTypes.SITEMAP); |
| 42 | + const sortedSiteMap = ((sitemapItems || {}).content || []).sort(sitemapSorter); |
| 43 | + const sitemapText = sortedSiteMap.reduce( |
| 44 | + (ongoing, current) => `${ongoing}${baseLink}${current.page_link}\n`, |
| 45 | + '', |
| 46 | + ); |
| 47 | + |
| 48 | + res.contentType = 'text/plain'; |
| 49 | + res.header('content-type', 'text/plain'); |
| 50 | + res.send(200, sitemapText); |
| 51 | + next(); |
| 52 | + } catch (caught) { |
| 53 | + req.log.warn(`Catch during find static :: ${caught}`); |
| 54 | + next(new errors.InternalServerError(caught.message)); |
| 55 | + } |
32 | 56 | }; |
33 | 57 |
|
34 | 58 | module.exports = displaySitemap; |
0 commit comments