Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

api: Discourse API "proxy" endpoint at /api/comments #819

Merged
merged 11 commits into from
Dec 4, 2019
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
"dependencies": {
"color": "^3.1.2",
"dom-scroll-into-view": "^2.0.1",
"force-ssl-heroku": "^1.0.2",
"github-markdown-css": "^3.0.1",
"isomorphic-fetch": "^2.2.1",
"lodash.includes": "^4.3.0",
Expand Down Expand Up @@ -63,9 +62,11 @@
"eslint-plugin-react": "^7.17.0",
"husky": "^3.1.0",
"jest": "^24.9.0",
"lint-staged": "^9.5.0",
"lint-staged": "^10.0.0-1",
"micro-cors": "^0.1.1",
"prettier": "^1.19.1",
"pretty-quick": "^2.0.1"
"pretty-quick": "^2.0.1",
"request": "^2.88.0"
},
"husky": {
"hooks": {
Expand Down
55 changes: 55 additions & 0 deletions pages/api/comments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* This API endpoint is used by https://blog.dvc.org
* to get comments count for the post, it gets
* discuss.dvc.org topic url as a param and returns
* comments count or error.
*
* It made this way to configure CORS, reduce user's payload
* and to add potential ability to cache comments count
* in the future.
*/
Comment on lines +1 to +10
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll rewrap this.


import Cors from 'micro-cors'
import request from 'request'

import { BLOG_URL, FORUM_URL } from '../../src/consts'

const cors = Cors({
shcheklein marked this conversation as resolved.
Show resolved Hide resolved
allowedMethods: ['GET', 'HEAD'],
origin: BLOG_URL
})

const getCommentCount = (req, res) => {
const {
query: { url }
} = req

if (!url.startsWith(FORUM_URL)) {
res.status(400)

return
}

request(`${url}.json`, (error, response, body) => {
if (error || response.statusCode !== 200) {
res.status(502).json({ error: 'Unexpected response from Forum' })

return
}

const json = JSON.parse(body)

if (!json.posts_count) {
res.status(502).json({ error: 'Unexpected payload from Forum' })

return
}

// post_count return all posts including topic itself
const count = json.posts_count - 1

res.status(200).json({ count })
})
}

export default cors(getCommentCount)
5 changes: 3 additions & 2 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-env node */

// This file doesn't go through babel or webpack transformation.
// Make sure the syntax and sources this file requires are compatible with the
// current node version you are running.
Expand All @@ -6,7 +8,6 @@

const { createServer } = require('http')
const { parse } = require('url')
const _ = require('force-ssl-heroku')
shcheklein marked this conversation as resolved.
Show resolved Hide resolved
const next = require('next')
const querystring = require('querystring')

Expand Down Expand Up @@ -96,7 +97,7 @@ app.prepare().then(() => {
res.end()
} else if (/^\/doc.*/i.test(pathname)) {
// path /doc*/... -> /doc/...
let normalized_pathname = pathname.replace(/^\/doc[^?\/]*/i, '/doc')
let normalized_pathname = pathname.replace(/^\/doc[^?/]*/i, '/doc')
if (normalized_pathname !== pathname) {
res.writeHead(301, {
Location:
Expand Down
7 changes: 6 additions & 1 deletion src/consts.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
export const HEADER = 'header'

const WEBSITE_HOST = 'dvc.org'

export const META_BASE_TITLE = 'Data Version Control · DVC'
export const META_DESCRIPTION =
'Open-source version control system for Data Science and Machine Learning ' +
'projects. Git-like experience to organize your data, models, and ' +
'experiments.'
export const META_KEYWORDS =
'data version control machine learning models management'
export const META_SOCIAL_IMAGE = 'https://dvc.org/static/social-share.png'
export const META_SOCIAL_IMAGE = `https://${WEBSITE_HOST}/static/social-share.png`

export const FORUM_URL = `https://discuss.${WEBSITE_HOST}`
export const BLOG_URL = `https://blog.${WEBSITE_HOST}`
Loading