forked from ciqulover/disqus-proxy-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
125 lines (104 loc) · 3.47 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
const Koa = require('koa')
const fetch = require('node-fetch')
const wrappedFetch = require('socks5-node-fetch')
const router = require('koa-router')()
const bodyParser = require('koa-bodyparser')
const log4js = require('log4js')
const cors = require('kcors')
const URLSearchParams = require('url').URLSearchParams
const ENV = process.env.NODE_ENV
const config = ENV === 'DEV' ? require('./dev.config') : require('./config')
// 这个API_key是disqus专门用来匿名评论的公有API_key
const api_key = 'E8Uh5l5fHZ6gD8U3KycjAIAk46f68Zw7C6eW8WSjZvCLXebZ7p0r1yrYDrLilk2F'
const logger = log4js.getLogger('disqus-proxy')
if (config.log === 'file') log4js.configure({
appenders: [{
type: 'file',
filename: 'disqus-proxy.log'
}]
})
const socks5 = config.socks5Proxy
let request = fetch
if (socks5 && socks5.host && socks5.port) {
request = wrappedFetch({
socksHost: socks5.host,
socksPort: socks5.port
})
}
router.get('/api/getThreads', async function (ctx) {
const identifier = ctx.request.query.identifier
logger.info('Get thread with identifier: ' + identifier)
const url = 'https://disqus.com/api/3.0/threads/list.json?' +
'api_secret=' + config.api_secret +
'&forum=' + config.shortname +
'&thread:ident=' + encodeURIComponent(identifier)
let result
try {
result = await request(url)
result = await result.json()
logger.info('Get thread successfully with response code: ' + result.code)
} catch (e) {
result = unexpectedError(e)
logger.error('Error when get thread:' + e.message)
}
ctx.body = result
})
router.get('/api/getComments', async function (ctx) {
const identifier = ctx.request.query.identifier
logger.info('Get Comments with identifier: ' + identifier)
let result
const url = 'https://disqus.com/api/3.0/threads/listPosts.json?' +
'api_secret=' + config.api_secret +
'&forum=' + config.shortname +
'&thread:ident=' + encodeURIComponent(ctx.request.query.identifier)
try {
result = await request(url)
result = await result.json()
logger.info('Get comments successfully with response code: ' + result.code)
} catch (e) {
result = unexpectedError(e)
logger.error('Error when get comment:' + e.message)
}
ctx.body = result
})
router.post('/api/createComment', async function (ctx) {
const body = ctx.request.body
logger.info('Create comment: ' + JSON.stringify(body))
const {message, thread, author_name, author_email} = body
const params = new URLSearchParams()
params.append('api_key', api_key)
params.append('message', message)
params.append('thread', thread)
params.append('author_name', author_name)
params.append('author_email', author_email)
let result
try {
result = await request('https://disqus.com/api/3.0/posts/create.json', {
method: 'POST',
body: params
})
result = await result.json()
logger.info('Create comment successfully with response code: ' + result.code)
} catch (e) {
result = unexpectedError(e)
logger.error('Error when create comment:' + e.message)
}
ctx.body = result
})
const app = new Koa()
app.use(cors())
.use(bodyParser())
.use(router.routes())
.use(router.allowedMethods())
app.listen(config.port)
console.log('Disqus proxy server start at port: ' + config.port)
if (config.log === 'file') {
console.log('See disqus-proxy.log in current directory.')
logger.info('Server start at port: ' + config.port)
}
function unexpectedError(error) {
return {
code: 500,
response: error.message
}
}