3
1

index.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import express from 'express'
  2. import path from 'path'
  3. import morgan from 'morgan'
  4. import socketio from 'socket.io'
  5. import ascii from './ascii'
  6. import db from './db'
  7. const pg = require('pg')
  8. delete pg.native
  9. const { connectUpstream } = require('./joystream/ws')
  10. const { addBlockRange } = require('./joystream')
  11. const { setupSocket } = require('./socket')
  12. const PORT: number = process.env.PORT ? +process.env.PORT : 3500
  13. const app = express()
  14. const server = app.listen(PORT, () =>
  15. console.log(`[Express] Listening on port ${PORT}`, ascii)
  16. )
  17. ;(async () => {
  18. const api = await connectUpstream()
  19. const io: any = socketio(server)
  20. setupSocket(io, api)
  21. const args = process.argv.slice(2)
  22. if (args.length === 2) {
  23. const startBlock = args[0]
  24. const endBlock = args[1]
  25. console.log(`[Joystream] syncing blocks from ${startBlock} to ${endBlock}`)
  26. addBlockRange(api, startBlock, endBlock)
  27. }
  28. })()
  29. //const cors = require("cors");
  30. //const passport = require('passport')
  31. //const LocalStrategy = require('passport-local')
  32. //const session = require('express-session')
  33. //const SequelizeStore = require('connect-session-sequelize')(session.Store)
  34. //const sessionStore = new SequelizeStore({ db })
  35. app.use(morgan('dev'))
  36. //app.use(cors({ credentials: true, origin: URL }))
  37. // passport.use(
  38. // new LocalStrategy(async (username, password, done) => {
  39. // const user = await db.models.user.findOne({ where: { username } })
  40. // if (!user) {
  41. // return done(null, false, { message: 'Incorrect username.' })
  42. // }
  43. // if (!user.correctPassword(password)) {
  44. // return done(null, false, { message: 'Incorrect password.' })
  45. // }
  46. // return done(null, user)
  47. // })
  48. // )
  49. //passport.serializeUser((user, cb) => cb(null, user.id))
  50. // passport.deserializeUser(async (id, cb) => {
  51. // try {
  52. // const user = await db.models.user.findByPk(id)
  53. // if (!user) return cb(null, { id: 0 })
  54. // const idType = user.isAdmin ? 'trainerId' : 'userId'
  55. // const { sessions, reviews, notes, referrers } = await user.fetchMyData(
  56. // idType
  57. // )
  58. // user.setDataValue('sessions', sessions)
  59. // user.setDataValue('reviews', reviews)
  60. // user.setDataValue('notes', notes)
  61. // user.setDataValue('referrers', referrers)
  62. // cb(null, user)
  63. // } catch (err) {
  64. // console.log('error', err)
  65. // cb(err)
  66. // }
  67. // })
  68. // app.use(
  69. // session({
  70. // secret: process.env.SESSION_SECRET || 'LAKaLIHWIUH9*&h3ISVAEOIUFHAW83w',
  71. // store: sessionStore,
  72. // resave: false,
  73. // saveUninitialized: false,
  74. // cookie: { maxAge: 3600000 },
  75. // })
  76. // )
  77. //
  78. // app.use(passport.initialize())
  79. // app.use(passport.session())
  80. // body parsing middleware
  81. app.use(express.json())
  82. app.use(express.urlencoded({ extended: true }))
  83. app.use(require('body-parser').text())
  84. app.use('/api', require('./api'))
  85. //app.use('/auth', require('./auth'))
  86. app.use(
  87. '/static',
  88. express.static(path.resolve(__dirname, '..', 'build', 'static'))
  89. )
  90. app.get('/manifest.json', (req: any, res: any) => {
  91. res.sendFile(path.resolve(__dirname, '..', 'build', 'manifest.json'))
  92. })
  93. app.get('/favicon.png', (req: any, res: any) => {
  94. res.sendFile(path.resolve(__dirname, '..', 'build', 'favicon.png'))
  95. })
  96. app.use('*', express.static(path.resolve(__dirname, '..', 'build')))
  97. // error handling endware
  98. app.use((err: any, req: any, res: any, next: any) => {
  99. console.error(err)
  100. console.error(err.stack)
  101. res.status(err.status || 500).send(err.message || 'Internal server error.')
  102. next()
  103. })
  104. //module.exports = {}
  105. //export {}