import express from 'express' import path from 'path' import morgan from 'morgan' import socketio from 'socket.io' import ascii from './ascii' import db from './db' const pg = require('pg') delete pg.native const { connectUpstream } = require('./joystream/ws') const { addBlockRange } = require('./joystream') const { setupSocket } = require('./socket') const PORT: number = process.env.PORT ? +process.env.PORT : 3500 const app = express() const server = app.listen(PORT, () => console.log(`[Express] Listening on port ${PORT}`, ascii) ) ;(async () => { const api = await connectUpstream() const io: any = socketio(server) setupSocket(io, api) const args = process.argv.slice(2) if (args.length === 2) { const startBlock = args[0] const endBlock = args[1] console.log(`[Joystream] syncing blocks from ${startBlock} to ${endBlock}`) addBlockRange(api, startBlock, endBlock) } })() //const cors = require("cors"); //const passport = require('passport') //const LocalStrategy = require('passport-local') //const session = require('express-session') //const SequelizeStore = require('connect-session-sequelize')(session.Store) //const sessionStore = new SequelizeStore({ db }) app.use(morgan('dev')) //app.use(cors({ credentials: true, origin: URL })) // passport.use( // new LocalStrategy(async (username, password, done) => { // const user = await db.models.user.findOne({ where: { username } }) // if (!user) { // return done(null, false, { message: 'Incorrect username.' }) // } // if (!user.correctPassword(password)) { // return done(null, false, { message: 'Incorrect password.' }) // } // return done(null, user) // }) // ) //passport.serializeUser((user, cb) => cb(null, user.id)) // passport.deserializeUser(async (id, cb) => { // try { // const user = await db.models.user.findByPk(id) // if (!user) return cb(null, { id: 0 }) // const idType = user.isAdmin ? 'trainerId' : 'userId' // const { sessions, reviews, notes, referrers } = await user.fetchMyData( // idType // ) // user.setDataValue('sessions', sessions) // user.setDataValue('reviews', reviews) // user.setDataValue('notes', notes) // user.setDataValue('referrers', referrers) // cb(null, user) // } catch (err) { // console.log('error', err) // cb(err) // } // }) // app.use( // session({ // secret: process.env.SESSION_SECRET || 'LAKaLIHWIUH9*&h3ISVAEOIUFHAW83w', // store: sessionStore, // resave: false, // saveUninitialized: false, // cookie: { maxAge: 3600000 }, // }) // ) // // app.use(passport.initialize()) // app.use(passport.session()) // body parsing middleware app.use(express.json()) app.use(express.urlencoded({ extended: true })) app.use(require('body-parser').text()) app.use('/api', require('./api')) //app.use('/auth', require('./auth')) app.use( '/static', express.static(path.resolve(__dirname, '..', 'build', 'static')) ) app.get('/manifest.json', (req: any, res: any) => { res.sendFile(path.resolve(__dirname, '..', 'build', 'manifest.json')) }) app.get('/favicon.png', (req: any, res: any) => { res.sendFile(path.resolve(__dirname, '..', 'build', 'favicon.png')) }) app.use('*', express.static(path.resolve(__dirname, '..', 'build'))) // error handling endware app.use((err: any, req: any, res: any, next: any) => { console.error(err) console.error(err.stack) res.status(err.status || 500).send(err.message || 'Internal server error.') next() }) //module.exports = {} //export {}