🐛 Fix ping not reading from correct config

This commit is contained in:
Meierschlumpf
2023-07-22 18:27:24 +02:00
parent 6658c05e5a
commit 1634510070
2 changed files with 108 additions and 71 deletions

View File

@@ -1,24 +1,24 @@
import { TRPCError } from '@trpc/server';
import axios, { AxiosError } from 'axios';
import Consola from 'consola';
import { getCookie } from 'cookies-next';
import https from 'https';
import { z } from 'zod';
import { getIsOk } from '~/components/Dashboard/Tiles/Apps/AppPing';
import { isStatusOk } from '~/components/Dashboard/Tiles/Apps/AppPing';
import { getConfig } from '~/tools/config/getConfig';
import { AppType } from '~/types/app';
import { createTRPCRouter, publicProcedure } from '../trpc';
export const appRouter = createTRPCRouter({
ping: publicProcedure.input(z.string()).query(async ({ input }) => {
ping: publicProcedure.input(z.object({
id: z.string(),
configName: z.string()
})).query(async ({ input }) => {
const agent = new https.Agent({ rejectUnauthorized: false });
const configName = getCookie('config-name');
const config = getConfig(configName?.toString() ?? 'default');
const app = config.apps.find((app) => app.id === input);
const config = getConfig(input.configName);
const app = config.apps.find((app) => app.id === input.id);
const url = app?.url;
if (url === undefined || !app) {
if (!app?.url) {
Consola.error(`App ${input} not found`);
throw new TRPCError({
code: 'NOT_FOUND',
@@ -27,22 +27,23 @@ export const appRouter = createTRPCRouter({
});
}
const res = await axios
.get(url, { httpsAgent: agent, timeout: 2000 })
.get(app.url, { httpsAgent: agent, timeout: 2000 })
.then((response) => ({
status: response.status,
statusText: response.statusText,
state: isStatusOk(app as AppType, response.status) ? 'online' : 'offline'
}))
.catch((error: AxiosError) => {
if (error.response) {
return {
state: getIsOk(app as AppType, error.response.status) ? 'online' : 'offline',
state: isStatusOk(app as AppType, error.response.status) ? 'online' : 'offline',
status: error.response.status,
statusText: error.response.statusText,
};
}
if (error.code === 'ECONNABORTED') {
Consola.error(`Ping timed out for app with id : ${input} (url: ${url})`);
Consola.error(`Ping timed out for app with id : ${input} (url: ${app.url})`);
throw new TRPCError({
code: 'TIMEOUT',
cause: input,