import express, { Express, Request, Response } from 'express'; import dotenv from 'dotenv' import routes from './routes' import { errorHandler } from './util/errorHandler' import { wsServer } from './routes/proxyRoute'; import compression from 'compression' import { cors } from './util/cors'; import { execSync } from 'child_process'; dotenv.config() // define version as current commit const version = execSync('git rev-parse HEAD') .toString().trim() const app: Express = express(); const port = process.env.PORT app.use(cors()) app.use(compression({})) app.use(routes) // main endpoint app.get('/', (req: Request, res: Response) => { res.send({ status: 'ok', message: `SafeTwitch Backend on commit ${version}. If you meant to use SafeTwitch, you're at the wrong URL` }) }) // 404 handler app.use((req, res) => { if (!res.headersSent) { res.status(404).send('404') } }); // handle errors app.use(errorHandler) const server = app.listen(port, () => { console.log(`SafeTwitch commit ${version} started on port ${port}`) }) server.on('upgrade', (request, socket, head) => { wsServer.handleUpgrade(request, socket, head, socket => { wsServer.emit('connection', socket, request); }); }); // don't crash on unhandledRejection process.on('unhandledRejection', (reason: Error, p) => {});