2023-04-24 10:14:57 -05:00
|
|
|
import express, { Express, Request, Response } from 'express';
|
2023-03-24 06:41:33 -05:00
|
|
|
import dotenv from 'dotenv'
|
|
|
|
import routes from './routes'
|
2023-04-09 11:38:29 -05:00
|
|
|
import { errorHandler } from './util/errorHandler'
|
2023-03-24 06:41:33 -05:00
|
|
|
import { wsServer } from './routes/proxyRoute';
|
2023-03-29 09:03:16 -05:00
|
|
|
import compression from 'compression'
|
2023-04-16 15:09:07 -05:00
|
|
|
import { cors } from './util/cors';
|
2023-04-24 10:14:57 -05:00
|
|
|
import { execSync } from 'child_process';
|
2023-03-24 06:41:33 -05:00
|
|
|
|
|
|
|
dotenv.config()
|
|
|
|
|
2023-04-24 10:14:57 -05:00
|
|
|
// define version as current commit
|
|
|
|
const version = execSync('git rev-parse HEAD')
|
|
|
|
.toString().trim()
|
|
|
|
|
2023-03-24 06:41:33 -05:00
|
|
|
const app: Express = express();
|
|
|
|
const port = process.env.PORT
|
|
|
|
|
2023-04-16 15:09:07 -05:00
|
|
|
app.use(cors())
|
2023-03-29 09:03:16 -05:00
|
|
|
app.use(compression({}))
|
2023-03-24 06:41:33 -05:00
|
|
|
app.use(routes)
|
|
|
|
|
2023-04-24 10:14:57 -05:00
|
|
|
// 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`
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
|
2023-03-24 06:41:33 -05:00
|
|
|
// 404 handler
|
|
|
|
app.use((req, res) => {
|
|
|
|
if (!res.headersSent) {
|
|
|
|
res.status(404).send('404')
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// handle errors
|
|
|
|
app.use(errorHandler)
|
|
|
|
|
|
|
|
const server = app.listen(port, () => {
|
2023-04-24 10:14:57 -05:00
|
|
|
console.log(`SafeTwitch commit ${version} started on port ${port}`)
|
2023-03-24 06:41:33 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
server.on('upgrade', (request, socket, head) => {
|
|
|
|
wsServer.handleUpgrade(request, socket, head, socket => {
|
|
|
|
wsServer.emit('connection', socket, request);
|
|
|
|
});
|
|
|
|
});
|
2023-03-29 09:03:16 -05:00
|
|
|
|
2023-04-09 11:38:29 -05:00
|
|
|
// don't crash on unhandledRejection
|
|
|
|
process.on('unhandledRejection', (reason: Error, p) => {});
|