Better logging system
parent
e5f96b966a
commit
e9696fac25
|
@ -2,6 +2,7 @@ import * as config from "./config";
|
||||||
import * as requests from "./requests";
|
import * as requests from "./requests";
|
||||||
import * as twitch from "./twitch";
|
import * as twitch from "./twitch";
|
||||||
import * as queries from "./queries";
|
import * as queries from "./queries";
|
||||||
|
import { log, LogLevel } from "./logging"
|
||||||
import { URLSearchParams } from "url";
|
import { URLSearchParams } from "url";
|
||||||
import express from "express";
|
import express from "express";
|
||||||
import session from "express-session";
|
import session from "express-session";
|
||||||
|
@ -11,6 +12,9 @@ import fetch, { Response as FetchResponse } from "node-fetch";
|
||||||
import db from "./db";
|
import db from "./db";
|
||||||
import errorHandler from "./errors";
|
import errorHandler from "./errors";
|
||||||
|
|
||||||
|
|
||||||
|
console.log("Starting at " + new Date().toISOString());
|
||||||
|
|
||||||
// Ensure that any API token we have is valid - if not, destroy the session,
|
// Ensure that any API token we have is valid - if not, destroy the session,
|
||||||
// logging out the user. Should be called before checking whether a user is
|
// logging out the user. Should be called before checking whether a user is
|
||||||
// logged in.
|
// logged in.
|
||||||
|
@ -300,7 +304,7 @@ app.get("/", async (request, response) => {
|
||||||
app.get("/logout", async (request, response) => request.session!.destroy(() => response.redirect(307, '/')));
|
app.get("/logout", async (request, response) => request.session!.destroy(() => response.redirect(307, '/')));
|
||||||
|
|
||||||
async function processBannedUsers() {
|
async function processBannedUsers() {
|
||||||
console.log("processBannedUsers run at " + new Date().toISOString());
|
log(LogLevel.INFO,"processBannedUsers run at " + new Date().toISOString());
|
||||||
var streamer = await db.query(queries.getStreamerIdToken).then((result: pg.QueryResult) => result.rows[0]);
|
var streamer = await db.query(queries.getStreamerIdToken).then((result: pg.QueryResult) => result.rows[0]);
|
||||||
if (streamer['tokenpair'] != null) {
|
if (streamer['tokenpair'] != null) {
|
||||||
var response = await twitch.apiRequest(streamer['tokenpair'],"/moderation/banned?broadcaster_id=" + streamer['userid']);
|
var response = await twitch.apiRequest(streamer['tokenpair'],"/moderation/banned?broadcaster_id=" + streamer['userid']);
|
||||||
|
|
|
@ -0,0 +1,14 @@
|
||||||
|
import * as config from "./config";
|
||||||
|
|
||||||
|
export enum LogLevel {
|
||||||
|
SILENT,
|
||||||
|
ERROR,
|
||||||
|
WARNING,
|
||||||
|
INFO,
|
||||||
|
DEBUG
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function log(logLevel: LogLevel, logMessage: any) {
|
||||||
|
if (config.logLevel >= logLevel)
|
||||||
|
console.log(LogLevel[logLevel].padStart(7) + ' | ' + logMessage)
|
||||||
|
}
|
|
@ -1,4 +1,5 @@
|
||||||
import * as config from "./config";
|
import * as config from "./config";
|
||||||
|
import { log, LogLevel } from "./logging"
|
||||||
import fetch, { Response as FetchResponse } from "node-fetch";
|
import fetch, { Response as FetchResponse } from "node-fetch";
|
||||||
|
|
||||||
export interface TokenPair {
|
export interface TokenPair {
|
||||||
|
@ -8,6 +9,7 @@ export interface TokenPair {
|
||||||
|
|
||||||
// Refresh the API token. Returns true on success and false on failure.
|
// Refresh the API token. Returns true on success and false on failure.
|
||||||
async function refreshApiToken(tokens: TokenPair): Promise<boolean> {
|
async function refreshApiToken(tokens: TokenPair): Promise<boolean> {
|
||||||
|
log(LogLevel.DEBUG,`Call: refreshApiToken(${JSON.stringify(tokens,null,2)})`);
|
||||||
return fetch("https://id.twitch.tv/oauth2/token", {
|
return fetch("https://id.twitch.tv/oauth2/token", {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
body: new URLSearchParams({
|
body: new URLSearchParams({
|
||||||
|
@ -18,11 +20,16 @@ async function refreshApiToken(tokens: TokenPair): Promise<boolean> {
|
||||||
})
|
})
|
||||||
}).then(async (res: FetchResponse) => {
|
}).then(async (res: FetchResponse) => {
|
||||||
if (res.status == 200) {
|
if (res.status == 200) {
|
||||||
|
log(LogLevel.INFO,"Refresh returned success.");
|
||||||
var data = await (res.json() as Promise<TokenPair>);
|
var data = await (res.json() as Promise<TokenPair>);
|
||||||
|
log(LogLevel.DEBUG, "Returned data:")
|
||||||
|
log(LogLevel.DEBUG, data)
|
||||||
tokens.access_token = data.access_token;
|
tokens.access_token = data.access_token;
|
||||||
tokens.refresh_token = data.refresh_token;
|
tokens.refresh_token = data.refresh_token;
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
|
log(LogLevel.ERROR,"Refresh returned failure. Response object:");
|
||||||
|
log(LogLevel.ERROR,res);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
@ -31,6 +38,7 @@ async function refreshApiToken(tokens: TokenPair): Promise<boolean> {
|
||||||
// Send an API request. On success, return the specified data. On failure,
|
// Send an API request. On success, return the specified data. On failure,
|
||||||
// attempt to refresh the API token and retry
|
// attempt to refresh the API token and retry
|
||||||
export async function apiRequest(tokens: TokenPair, endpoint: string): Promise <any> {
|
export async function apiRequest(tokens: TokenPair, endpoint: string): Promise <any> {
|
||||||
|
log(LogLevel.DEBUG,`Call: apiRequest(${JSON.stringify(tokens,null,2)},${endpoint})`);
|
||||||
var headers = {
|
var headers = {
|
||||||
"Authorization": "Bearer " + tokens.access_token,
|
"Authorization": "Bearer " + tokens.access_token,
|
||||||
"Client-ID": config.twitchClientId
|
"Client-ID": config.twitchClientId
|
||||||
|
@ -41,17 +49,25 @@ export async function apiRequest(tokens: TokenPair, endpoint: string): Promise <
|
||||||
return res.json();
|
return res.json();
|
||||||
} else {
|
} else {
|
||||||
if (refreshApiToken(tokens)) {
|
if (refreshApiToken(tokens)) {
|
||||||
|
log(LogLevel.WARNING,"Failed API request (pre-refresh):");
|
||||||
|
log(LogLevel.WARNING,"Request URL: https://api.twitch.tv/helix" + endpoint);
|
||||||
|
log(LogLevel.WARNING,"Headers:");
|
||||||
|
log(LogLevel.WARNING,JSON.stringify(headers,null,2));
|
||||||
|
log(LogLevel.WARNING,"Response:");
|
||||||
|
log(LogLevel.WARNING,JSON.stringify(await res.json(),null,2));
|
||||||
|
log(LogLevel.WARNING,"Attempting refresh");
|
||||||
return fetch("https://api.twitch.tv/helix" + endpoint, { headers: headers })
|
return fetch("https://api.twitch.tv/helix" + endpoint, { headers: headers })
|
||||||
.then(async (res: FetchResponse) => {
|
.then(async (res: FetchResponse) => {
|
||||||
if (res.status == 200) {
|
if (res.status == 200) {
|
||||||
|
log(LogLevel.WARNING,"API call succeeded after token refresh.")
|
||||||
return res.json();
|
return res.json();
|
||||||
} else {
|
} else {
|
||||||
console.log("Failed API request:");
|
log(LogLevel.ERROR,"Failed API request:");
|
||||||
console.log("Request URL: https://api.twitch.tv/helix" + endpoint);
|
log(LogLevel.ERROR,"Request URL: https://api.twitch.tv/helix" + endpoint);
|
||||||
console.log("Headers: ");
|
log(LogLevel.ERROR,"Headers:");
|
||||||
console.log(headers);
|
log(LogLevel.ERROR,JSON.stringify(headers,null,2));
|
||||||
console.log("Response: ");
|
log(LogLevel.ERROR,"Response:");
|
||||||
console.log(await res.json());
|
log(LogLevel.ERROR,JSON.stringify(await res.json(),null,2));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
@ -66,6 +82,7 @@ export async function apiRequest(tokens: TokenPair, endpoint: string): Promise <
|
||||||
// success, return true. If failure, return the result of attempting to refresh
|
// success, return true. If failure, return the result of attempting to refresh
|
||||||
// the API token.
|
// the API token.
|
||||||
export async function isApiTokenValid(tokens: TokenPair) {
|
export async function isApiTokenValid(tokens: TokenPair) {
|
||||||
|
log(LogLevel.DEBUG,`Call: isApiTokenValid(${JSON.stringify(tokens,null,2)})`);
|
||||||
return fetch("https://id.twitch.tv/oauth2/validate", {
|
return fetch("https://id.twitch.tv/oauth2/validate", {
|
||||||
headers: {'Authorization': `OAuth ${tokens.access_token}`}
|
headers: {'Authorization': `OAuth ${tokens.access_token}`}
|
||||||
}).then((res: FetchResponse) => {
|
}).then((res: FetchResponse) => {
|
||||||
|
|
Loading…
Reference in New Issue