Skip to content

Commit

Permalink
Merge pull request #819 from iterative/forum-proxy
Browse files Browse the repository at this point in the history
api: Discourse API "proxy" endpoint at /api/comments
  • Loading branch information
shcheklein committed Dec 4, 2019
2 parents 62ebbde + 6b9d01a commit cc97843
Show file tree
Hide file tree
Showing 5 changed files with 198 additions and 187 deletions.
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.
*/

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

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

const cors = Cors({
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')
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

0 comments on commit cc97843

Please sign in to comment.