mirror of
https://codeberg.org/SafeTwitch/safetwitch-backend.git
synced 2025-03-11 13:41:20 -05:00
Remove all logging except startup message
This commit is contained in:
parent
1f4636e624
commit
2aafaef8b4
8 changed files with 28 additions and 65 deletions
11
index.ts
11
index.ts
|
@ -1,16 +1,16 @@
|
||||||
import express, { Express } from 'express';
|
import express, { Express } from 'express';
|
||||||
import dotenv from 'dotenv'
|
import dotenv from 'dotenv'
|
||||||
import routes from './routes'
|
import routes from './routes'
|
||||||
import { errorHandler, logger, uuid } from './util/logger'
|
import { errorHandler } from './util/errorHandler'
|
||||||
import { wsServer } from './routes/proxyRoute';
|
import { wsServer } from './routes/proxyRoute';
|
||||||
import compression from 'compression'
|
import compression from 'compression'
|
||||||
|
import { version } from './package.json';
|
||||||
|
|
||||||
dotenv.config()
|
dotenv.config()
|
||||||
|
|
||||||
const app: Express = express();
|
const app: Express = express();
|
||||||
const port = process.env.PORT
|
const port = process.env.PORT
|
||||||
|
|
||||||
app.use(uuid)
|
|
||||||
app.use(compression({}))
|
app.use(compression({}))
|
||||||
app.use(routes)
|
app.use(routes)
|
||||||
|
|
||||||
|
@ -25,7 +25,7 @@ app.use((req, res) => {
|
||||||
app.use(errorHandler)
|
app.use(errorHandler)
|
||||||
|
|
||||||
const server = app.listen(port, () => {
|
const server = app.listen(port, () => {
|
||||||
console.log('Server up')
|
console.log(`SafeTwitch v${version} started on port ${port}`)
|
||||||
})
|
})
|
||||||
|
|
||||||
server.on('upgrade', (request, socket, head) => {
|
server.on('upgrade', (request, socket, head) => {
|
||||||
|
@ -34,6 +34,5 @@ server.on('upgrade', (request, socket, head) => {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
process.on('unhandledRejection', (reason: Error, p) => {
|
// don't crash on unhandledRejection
|
||||||
logger.error(reason)
|
process.on('unhandledRejection', (reason: Error, p) => {});
|
||||||
});
|
|
||||||
|
|
13
package.json
13
package.json
|
@ -1,9 +1,15 @@
|
||||||
{
|
{
|
||||||
|
"name": "safetwitch-backend",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"description": "Privacy respecting frontend for twitch",
|
||||||
|
"main": "index.ts",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"compression": "^1.7.4",
|
"compression": "^1.7.4",
|
||||||
|
"connect-history-api-fallback": "^2.0.0",
|
||||||
"dotenv": "^16.0.3",
|
"dotenv": "^16.0.3",
|
||||||
"express": "^4.18.2",
|
"express": "^4.18.2",
|
||||||
"videojs-contrib-quality-levels": "^3.0.0",
|
"videojs-contrib-quality-levels": "^3.0.0",
|
||||||
|
"videojs-hls-quality-selector": "^1.1.4",
|
||||||
"winston": "^3.8.2",
|
"winston": "^3.8.2",
|
||||||
"ws": "^8.13.0"
|
"ws": "^8.13.0"
|
||||||
},
|
},
|
||||||
|
@ -21,5 +27,10 @@
|
||||||
"dev": "npx nodemon index.ts",
|
"dev": "npx nodemon index.ts",
|
||||||
"prod": "npx ts-node index.ts",
|
"prod": "npx ts-node index.ts",
|
||||||
"build": "npx tsc"
|
"build": "npx tsc"
|
||||||
}
|
},
|
||||||
|
"repository": {
|
||||||
|
"type": "git",
|
||||||
|
"url": "https://codeberg.org/dragongoose/safetwitch-backend"
|
||||||
|
},
|
||||||
|
"author": "dragongoose"
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
import { randomUUID } from 'crypto';
|
import { randomUUID } from 'crypto';
|
||||||
import ws from 'ws';
|
import ws from 'ws';
|
||||||
import { TwitchChat } from '../util/scraping/chat/chat';
|
import { TwitchChat } from '../util/scraping/chat/chat';
|
||||||
import { logger } from '../util/logger';
|
|
||||||
|
|
||||||
interface ExtWebSocket extends ws {
|
interface ExtWebSocket extends ws {
|
||||||
id?: string;
|
id?: string;
|
||||||
|
@ -87,10 +86,6 @@ export class TwitchChatServer {
|
||||||
|
|
||||||
private handleHostTarget = async(channel: string, viewers: number, target: string, tags: Record<string,string>) => {
|
private handleHostTarget = async(channel: string, viewers: number, target: string, tags: Record<string,string>) => {
|
||||||
const socketsToSend = await this.findClientsForStreamer(channel)
|
const socketsToSend = await this.findClientsForStreamer(channel)
|
||||||
.catch((err) => {
|
|
||||||
logger.info(`No clients following ${channel}, unsubscribing`)
|
|
||||||
})
|
|
||||||
if(!socketsToSend) return;
|
|
||||||
|
|
||||||
for(let socket of socketsToSend) {
|
for(let socket of socketsToSend) {
|
||||||
let payload = {
|
let payload = {
|
||||||
|
@ -138,7 +133,7 @@ export class TwitchChatServer {
|
||||||
}
|
}
|
||||||
|
|
||||||
private async findClientsForStreamer(streamerName: string): Promise<ExtWebSocket[]> {
|
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];
|
return this.clients[streamerName];
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
import { Router, Response, Request, NextFunction } from 'express'
|
import { Router, Response, Request, NextFunction } from 'express'
|
||||||
import { TwitchAPI } from '../util/scraping/extractor';
|
import { TwitchAPI } from '../util/scraping/extractor';
|
||||||
import ws, { WebSocket } from 'ws';
|
import ws, { WebSocket } from 'ws';
|
||||||
import { logger } from '../util/logger';
|
|
||||||
import { TwitchChatServer } from './chatIrcProxy'
|
import { TwitchChatServer } from './chatIrcProxy'
|
||||||
|
|
||||||
const proxyRouter = Router();
|
const proxyRouter = Router();
|
||||||
|
@ -18,8 +17,6 @@ proxyRouter.get('/img/:base64Url', async (req: Request, res: Response, next: Nex
|
||||||
|
|
||||||
if(imageRes.status !== 200) {
|
if(imageRes.status !== 200) {
|
||||||
res.status(imageRes.status).send()
|
res.status(imageRes.status).send()
|
||||||
const error = new Error('Image proxy fetch was not status 200')
|
|
||||||
logger.warn(error)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const arrayBuffer = await imageRes.arrayBuffer()
|
const arrayBuffer = await imageRes.arrayBuffer()
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
"module": "commonjs",
|
"module": "commonjs",
|
||||||
"esModuleInterop": true,
|
"esModuleInterop": true,
|
||||||
"outDir": "./dist",
|
"outDir": "./dist",
|
||||||
|
"resolveJsonModule": true,
|
||||||
"strict": true
|
"strict": true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
9
util/errorHandler.ts
Normal file
9
util/errorHandler.ts
Normal 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})
|
||||||
|
}
|
|
@ -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()
|
|
||||||
}
|
|
|
@ -2,7 +2,6 @@ import { EventEmitter } from 'stream';
|
||||||
import WebSocket from 'ws';
|
import WebSocket from 'ws';
|
||||||
import { TwitchChatOptions, Metadata, MessageType, MessageTypes } from '../../../types/scraping/Chat';
|
import { TwitchChatOptions, Metadata, MessageType, MessageTypes } from '../../../types/scraping/Chat';
|
||||||
import { parseUsername } from './utils';
|
import { parseUsername } from './utils';
|
||||||
import { logger } from '../../logger';
|
|
||||||
|
|
||||||
export declare interface TwitchChat {
|
export declare interface TwitchChat {
|
||||||
on(event: 'PRIVMSG', listener: (username: string, messageType: MessageType, channel: string, message: string, tags: Record<string,string>) => void): this;
|
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 = new WebSocket(this.url);
|
||||||
|
|
||||||
this.ws.onclose = () => {
|
this.ws.onclose = () => {
|
||||||
logger.info('Disconnected from Twitch IRC');
|
|
||||||
logger.info(`Subscribed to channels ${this.channels}`);
|
|
||||||
|
|
||||||
if (this.manualDisconnect) {
|
if (this.manualDisconnect) {
|
||||||
return;
|
return;
|
||||||
|
|
Loading…
Add table
Reference in a new issue