|
| 1 | +/** |
| 2 | + * The routes that expose assets and content from Contentful CMS to the CDN. |
| 3 | + */ |
| 4 | + |
| 5 | +import express from 'express'; |
| 6 | +import RSS from 'rss'; |
| 7 | +import ReactDOMServer from 'react-dom/server'; |
| 8 | +import md from 'utils/markdown'; |
| 9 | +import { |
| 10 | + getService, |
| 11 | +} from '../services/contentful'; |
| 12 | + |
| 13 | +const cors = require('cors'); |
| 14 | + |
| 15 | +const routes = express.Router(); |
| 16 | + |
| 17 | +// Enables CORS on those routes according config above |
| 18 | +// ToDo configure CORS for set of our trusted domains |
| 19 | +routes.use(cors()); |
| 20 | +routes.options('*', cors()); |
| 21 | + |
| 22 | +routes.get('/thrive', async (req, res, next) => { |
| 23 | + try { |
| 24 | + const data = await getService('EDU', 'master', true).queryEntries({ |
| 25 | + content_type: 'article', |
| 26 | + limit: 20, |
| 27 | + order: '-sys.createdAt', |
| 28 | + include: 2, |
| 29 | + }); |
| 30 | + const feed = new RSS({ |
| 31 | + title: 'Topcoder Thrive', |
| 32 | + description: 'Tutorials And Workshops That Matter | Thrive | Topcoder', |
| 33 | + feed_url: 'https://topcoder.com/api/feeds/thrive', |
| 34 | + site_url: 'https://topcoder.com/thrive', |
| 35 | + image_url: 'https://www.topcoder.com/wp-content/uploads/2020/05/cropped-TC-Icon-32x32.png', |
| 36 | + docs: 'https://www.topcoder.com/thrive/tracks?track=Topcoder', |
| 37 | + webMaster: '<[email protected]> Kiril Kartunov', |
| 38 | + copyright: '2021 - today, Topcoder', |
| 39 | + language: 'en', |
| 40 | + categories: ['Competitive Programming', 'Data Science', 'Design', 'Development', 'QA', 'Gig work', 'Topcoder'], |
| 41 | + ttl: '60', |
| 42 | + }); |
| 43 | + if (data && data.total) { |
| 44 | + data.items.forEach((entry) => { |
| 45 | + feed.item({ |
| 46 | + title: entry.fields.title, |
| 47 | + description: ReactDOMServer.renderToString(md(entry.fields.content)), |
| 48 | + url: `https://topcoder.com/thrive/articles/${entry.fields.slug || encodeURIComponent(entry.fields.title)}?utm_source=community&utm_campaign=thrive-feed&utm_medium=promotion`, |
| 49 | + date: entry.fields.creationDate, |
| 50 | + categories: entry.fields.tags, |
| 51 | + author: entry.fields.contentAuthor[0].fields.name, |
| 52 | + }); |
| 53 | + }); |
| 54 | + } |
| 55 | + res.set('Content-Type', 'application/rss+xml'); |
| 56 | + res.send(feed.xml({ indent: true })); |
| 57 | + } catch (e) { |
| 58 | + next(e); |
| 59 | + } |
| 60 | +}); |
| 61 | + |
| 62 | +export default routes; |
0 commit comments