0
Fork 0
mirror of https://codeberg.org/SafeTwitch/safetwitch-backend.git synced 2024-12-22 21:23:01 -05:00
safetwitch-backend/index.ts

54 lines
1.3 KiB
TypeScript
Raw Normal View History

import express, { Express, Request, Response } from 'express';
2023-03-24 06:41:33 -05:00
import dotenv from 'dotenv'
import routes from './routes'
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';
import { execSync } from 'child_process';
2023-03-24 06:41:33 -05:00
dotenv.config()
// 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)
// 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, () => {
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
// don't crash on unhandledRejection
process.on('unhandledRejection', (reason: Error, p) => {});