Merge branch 'main' into feature/widget_strelaysrv

This commit is contained in:
Ben Phelps 2022-09-21 09:05:42 +03:00 committed by GitHub
commit 122b987fa3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
33 changed files with 433 additions and 38 deletions

View file

@ -25,6 +25,7 @@ const formats = {
jackett: `{url}/api/v2.0/{endpoint}?apikey={key}&configured=true`,
adguard: `{url}/control/{endpoint}`,
strelaysrv: `{url}/{endpoint}`,
mastodon: `{url}/api/v1/{endpoint}`,
};
export function formatApiCall(api, args) {

76
src/utils/logger.js Normal file
View file

@ -0,0 +1,76 @@
import winston from "winston";
function messageFormatter(logInfo) {
if (logInfo.stack) {
return `[${logInfo.timestamp}] ${logInfo.level}: ${logInfo.stack}`;
}
return `[${logInfo.timestamp}] ${logInfo.level}: ${logInfo.message}`;
};
const consoleFormat = winston.format.combine(
winston.format.errors({ stack: true }),
winston.format.splat(),
winston.format.timestamp(),
winston.format.colorize(),
winston.format.printf(messageFormatter)
);
const fileFormat = winston.format.combine(
winston.format.errors({ stack: true }),
winston.format.splat(),
winston.format.timestamp(),
winston.format.printf(messageFormatter)
);
const logger = winston.createLogger({
level: process.env.LOG_LEVEL || 'info',
transports: [
new winston.transports.Console({
format: consoleFormat,
handleExceptions: true,
handleRejections: true
}),
new winston.transports.File({
format: fileFormat,
filename: 'homepage.log',
handleExceptions: true,
handleRejections: true
}),
]
});
function debug(message, ...args) {
logger.debug(message, ...args);
}
function verbose(message, ...args) {
logger.verbose(message, ...args);
}
function info(message, ...args) {
logger.info(message, ...args);
}
function warn(message, ...args) {
logger.warn(message, ...args);
}
function error(message, ...args) {
logger.error(message, ...args);
}
function crit(message, ...args) {
logger.crit(message, ...args);
}
const thisModule = {
debug,
verbose,
info,
warn,
error,
crit
};
export default thisModule;

View file

@ -1,6 +1,7 @@
import getServiceWidget from "utils/service-helpers";
import { formatApiCall } from "utils/api-helpers";
import { httpProxy } from "utils/http";
import logger from "utils/logger";
export default async function genericProxyHandler(req, res, maps) {
const { group, service, endpoint } = req.query;
@ -24,7 +25,7 @@ export default async function genericProxyHandler(req, res, maps) {
});
let resultData = data;
if (maps?.[endpoint]) {
if ((status === 200) && (maps?.[endpoint])) {
resultData = maps[endpoint](data);
}
@ -34,9 +35,14 @@ export default async function genericProxyHandler(req, res, maps) {
return res.status(status).end();
}
if (status >= 400) {
logger.debug("HTTP Error %d calling %s//%s%s...", status, url.protocol, url.hostname, url.pathname);
}
return res.status(status).send(resultData);
}
}
logger.debug("Invalid or missing proxy service type '%s' in group '%s'", service, group);
return res.status(400).json({ error: "Invalid proxy service type" });
}

View file

@ -0,0 +1,15 @@
import { createContext, useState, useMemo } from "react";
export const SettingsContext = createContext();
export function SettingsProvider({ initialSettings, children }) {
const [settings, setSettings] = useState({});
if (initialSettings) {
setSettings(initialSettings);
}
const value = useMemo(() => ({ settings, setSettings }), [settings]);
return <SettingsContext.Provider value={value}>{children}</SettingsContext.Provider>;
}