0
Fork 0
mirror of https://codeberg.org/SafeTwitch/safetwitch-backend.git synced 2024-12-22 05:02:58 -05:00

Remove all logging except startup message

This commit is contained in:
dragongoose 2023-04-09 12:38:29 -04:00
parent 1f4636e624
commit 2aafaef8b4
No known key found for this signature in database
GPG key ID: 50DB99B921579009
8 changed files with 28 additions and 65 deletions

View file

@ -1,16 +1,16 @@
import express, { Express } from 'express';
import dotenv from 'dotenv'
import routes from './routes'
import { errorHandler, logger, uuid } from './util/logger'
import { errorHandler } from './util/errorHandler'
import { wsServer } from './routes/proxyRoute';
import compression from 'compression'
import { version } from './package.json';
dotenv.config()
const app: Express = express();
const port = process.env.PORT
app.use(uuid)
app.use(compression({}))
app.use(routes)
@ -25,7 +25,7 @@ app.use((req, res) => {
app.use(errorHandler)
const server = app.listen(port, () => {
console.log('Server up')
console.log(`SafeTwitch v${version} started on port ${port}`)
})
server.on('upgrade', (request, socket, head) => {
@ -34,6 +34,5 @@ server.on('upgrade', (request, socket, head) => {
});
});
process.on('unhandledRejection', (reason: Error, p) => {
logger.error(reason)
});
// don't crash on unhandledRejection
process.on('unhandledRejection', (reason: Error, p) => {});

View file

@ -1,9 +1,15 @@
{
"name": "safetwitch-backend",
"version": "1.0.0",
"description": "Privacy respecting frontend for twitch",
"main": "index.ts",
"dependencies": {
"compression": "^1.7.4",
"connect-history-api-fallback": "^2.0.0",
"dotenv": "^16.0.3",
"express": "^4.18.2",
"videojs-contrib-quality-levels": "^3.0.0",
"videojs-hls-quality-selector": "^1.1.4",
"winston": "^3.8.2",
"ws": "^8.13.0"
},
@ -21,5 +27,10 @@
"dev": "npx nodemon index.ts",
"prod": "npx ts-node index.ts",
"build": "npx tsc"
}
},
"repository": {
"type": "git",
"url": "https://codeberg.org/dragongoose/safetwitch-backend"
},
"author": "dragongoose"
}

View file

@ -1,7 +1,6 @@
import { randomUUID } from 'crypto';
import ws from 'ws';
import { TwitchChat } from '../util/scraping/chat/chat';
import { logger } from '../util/logger';
interface ExtWebSocket extends ws {
id?: string;
@ -87,10 +86,6 @@ export class TwitchChatServer {
private handleHostTarget = async(channel: string, viewers: number, target: string, tags: Record<string,string>) => {
const socketsToSend = await this.findClientsForStreamer(channel)
.catch((err) => {
logger.info(`No clients following ${channel}, unsubscribing`)
})
if(!socketsToSend) return;
for(let socket of socketsToSend) {
let payload = {
@ -138,7 +133,7 @@ export class TwitchChatServer {
}
private async findClientsForStreamer(streamerName: string): Promise<ExtWebSocket[]> {
if(!this.clients[streamerName]) return Promise.reject('No clients following streamer')
if(!this.clients[streamerName]) return []
return this.clients[streamerName];
}

View file

@ -1,7 +1,6 @@
import { Router, Response, Request, NextFunction } from 'express'
import { TwitchAPI } from '../util/scraping/extractor';
import ws, { WebSocket } from 'ws';
import { logger } from '../util/logger';
import { TwitchChatServer } from './chatIrcProxy'
const proxyRouter = Router();
@ -18,8 +17,6 @@ proxyRouter.get('/img/:base64Url', async (req: Request, res: Response, next: Nex
if(imageRes.status !== 200) {
res.status(imageRes.status).send()
const error = new Error('Image proxy fetch was not status 200')
logger.warn(error)
}
const arrayBuffer = await imageRes.arrayBuffer()

View file

@ -4,6 +4,7 @@
"module": "commonjs",
"esModuleInterop": true,
"outDir": "./dist",
"resolveJsonModule": true,
"strict": true
}
}

9
util/errorHandler.ts Normal file
View file

@ -0,0 +1,9 @@
import { NextFunction, Request, Response } from 'express'
export const errorHandler = (err: Error, req: Request, res: Response, next: NextFunction) => {
if (res.headersSent) {
return next(err)
}
res.status(500).send({ status: 'error', message: err.message})
}

View file

@ -1,46 +0,0 @@
import { randomUUID } from 'crypto'
import { createLogger, format, transports } from 'winston'
import { NextFunction, Request, Response } from 'express'
const logLevels = {
fatal: 0,
error: 1,
warn: 2,
info: 3,
debug: 4,
trace: 5,
};
export const logger = createLogger({
format: format.combine(format.timestamp(), format.json()),
transports: [
new transports.Console({
format: format.combine(
format.colorize(),
format.simple()
)
}),
new transports.File({ filename: './serverLog.log' })
],
levels: logLevels
});
export const errorHandler = (err: Error, req: Request, res: Response, next: NextFunction) => {
if (res.headersSent) {
return next(err)
}
res.status(500).send({ status: 'error', message: err.message, code: res.locals.uuid })
logger.warn({
message: err.message,
endpoint: req.originalUrl,
reqId: res.locals.uuid,
origin: req.headers.origin,
})
}
export const uuid = (req: Request, res: Response, next: NextFunction) => {
res.locals.uuid = randomUUID()
next()
}

View file

@ -2,7 +2,6 @@ import { EventEmitter } from 'stream';
import WebSocket from 'ws';
import { TwitchChatOptions, Metadata, MessageType, MessageTypes } from '../../../types/scraping/Chat';
import { parseUsername } from './utils';
import { logger } from '../../logger';
export declare interface TwitchChat {
on(event: 'PRIVMSG', listener: (username: string, messageType: MessageType, channel: string, message: string, tags: Record<string,string>) => void): this;
@ -101,8 +100,6 @@ export class TwitchChat extends EventEmitter {
this.ws = new WebSocket(this.url);
this.ws.onclose = () => {
logger.info('Disconnected from Twitch IRC');
logger.info(`Subscribed to channels ${this.channels}`);
if (this.manualDisconnect) {
return;