-
Notifications
You must be signed in to change notification settings - Fork 386
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #819 from iterative/forum-proxy
api: Discourse API "proxy" endpoint at /api/comments
- Loading branch information
Showing
5 changed files
with
198 additions
and
187 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}` |
Oops, something went wrong.