3
1

index.ts 3.4 KB

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