You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

97 lines
2.5 KiB

  1. import dotenv from 'dotenv'
  2. import express from 'express'
  3. import redis from 'redis'
  4. import pg from 'pg'
  5. dotenv.config()
  6. const pgConfigs = {
  7. development: {
  8. database: 'mastodon_development',
  9. host: '/var/run/postgresql',
  10. max: 10
  11. },
  12. production: {
  13. user: process.env.DB_USER || 'mastodon',
  14. password: process.env.DB_PASS || '',
  15. database: process.env.DB_NAME || 'mastodon_production',
  16. host: process.env.DB_HOST || 'localhost',
  17. port: process.env.DB_PORT || 5432,
  18. max: 10
  19. }
  20. }
  21. const app = express()
  22. const env = process.env.NODE_ENV || 'development'
  23. const pgPool = new pg.Pool(pgConfigs[env])
  24. const authenticationMiddleware = (req, res, next) => {
  25. const authorization = req.get('Authorization')
  26. if (!authorization) {
  27. err = new Error('Missing access token')
  28. err.statusCode = 401
  29. return next(err)
  30. }
  31. const token = authorization.replace(/^Bearer /, '')
  32. pgPool.connect((err, client, done) => {
  33. if (err) {
  34. return next(err)
  35. }
  36. client.query('SELECT oauth_access_tokens.resource_owner_id, users.account_id FROM oauth_access_tokens INNER JOIN users ON oauth_access_tokens.resource_owner_id = users.id WHERE token = $1 LIMIT 1', [token], (err, result) => {
  37. done()
  38. if (err) {
  39. return next(err)
  40. }
  41. if (result.rows.length === 0) {
  42. err = new Error('Invalid access token')
  43. err.statusCode = 401
  44. return next(err)
  45. }
  46. req.accountId = result.rows[0].account_id
  47. next()
  48. })
  49. })
  50. }
  51. const errorMiddleware = (err, req, res, next) => {
  52. res.writeHead(err.statusCode || 500, { 'Content-Type': 'application/json' })
  53. res.end(JSON.stringify({ error: `${err}` }))
  54. }
  55. const streamFrom = (id, res) => {
  56. res.setHeader('Content-Type', 'text/event-stream')
  57. res.setHeader('Transfer-Encoding', 'chunked')
  58. const redisClient = redis.createClient()
  59. redisClient.on('message', (channel, message) => {
  60. const { event, payload } = JSON.parse(message)
  61. res.write(`event: ${event}\n`)
  62. res.write(`data: ${payload}\n\n`)
  63. })
  64. setInterval(() => res.write('\n'), 15000)
  65. redisClient.subscribe(id)
  66. }
  67. app.use(authenticationMiddleware)
  68. app.use(errorMiddleware)
  69. app.get('/api/v1/streaming/user', (req, res) => streamFrom(`timeline:${req.accountId}`, res))
  70. app.get('/api/v1/streaming/public', (_, res) => streamFrom('timeline:public', res))
  71. app.get('/api/v1/streaming/hashtag', (req, res) => streamFrom(`timeline:hashtag:${req.params.tag}`, res))
  72. app.listen(4000)