mirror of
https://github.com/ajnart/homarr.git
synced 2025-11-10 07:25:48 +01:00
🏗️ Migrate ping to tRPC
This commit is contained in:
@@ -1,10 +1,10 @@
|
|||||||
import { Indicator, Tooltip } from '@mantine/core';
|
import { Indicator, Tooltip } from '@mantine/core';
|
||||||
import Consola from 'consola';
|
import Consola from 'consola';
|
||||||
import { useQuery } from '@tanstack/react-query';
|
|
||||||
import { motion } from 'framer-motion';
|
import { motion } from 'framer-motion';
|
||||||
import { useTranslation } from 'next-i18next';
|
import { useTranslation } from 'next-i18next';
|
||||||
import { useConfigContext } from '../../../../config/provider';
|
import { useConfigContext } from '../../../../config/provider';
|
||||||
import { AppType } from '../../../../types/app';
|
import { AppType } from '../../../../types/app';
|
||||||
|
import { api } from '~/utils/api';
|
||||||
|
|
||||||
interface AppPingProps {
|
interface AppPingProps {
|
||||||
app: AppType;
|
app: AppType;
|
||||||
@@ -16,18 +16,7 @@ export const AppPing = ({ app }: AppPingProps) => {
|
|||||||
const active =
|
const active =
|
||||||
(config?.settings.customization.layout.enabledPing && app.network.enabledStatusChecker) ??
|
(config?.settings.customization.layout.enabledPing && app.network.enabledStatusChecker) ??
|
||||||
false;
|
false;
|
||||||
const { data, isLoading } = useQuery({
|
const { data, isLoading, error } = usePingQuery(app, active);
|
||||||
queryKey: ['ping', { id: app.id, name: app.name }],
|
|
||||||
queryFn: async () => {
|
|
||||||
const response = await fetch(`/api/modules/ping?url=${encodeURI(app.url)}`);
|
|
||||||
const isOk = getIsOk(app, response.status);
|
|
||||||
return {
|
|
||||||
status: response.status,
|
|
||||||
state: isOk ? 'online' : 'down',
|
|
||||||
};
|
|
||||||
},
|
|
||||||
enabled: active,
|
|
||||||
});
|
|
||||||
|
|
||||||
const isOnline = data?.state === 'online';
|
const isOnline = data?.state === 'online';
|
||||||
|
|
||||||
@@ -49,7 +38,7 @@ export const AppPing = ({ app }: AppPingProps) => {
|
|||||||
? t('states.loading')
|
? t('states.loading')
|
||||||
: isOnline
|
: isOnline
|
||||||
? t('states.online', { response: data.status })
|
? t('states.online', { response: data.status })
|
||||||
: t('states.offline', { response: data?.status })
|
: t('states.offline', { response: data?.status ?? error?.data?.httpStatus })
|
||||||
}
|
}
|
||||||
>
|
>
|
||||||
<Indicator
|
<Indicator
|
||||||
@@ -62,11 +51,29 @@ export const AppPing = ({ app }: AppPingProps) => {
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const usePingQuery = (app: AppType, isEnabled: boolean) =>
|
||||||
|
api.app.ping.useQuery(
|
||||||
|
{
|
||||||
|
url: app.url,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
enabled: isEnabled,
|
||||||
|
select: (data) => {
|
||||||
|
const statusCode = data.status;
|
||||||
|
const isOk = getIsOk(app, statusCode);
|
||||||
|
return {
|
||||||
|
status: statusCode,
|
||||||
|
state: isOk ? ('online' as const) : ('down' as const),
|
||||||
|
};
|
||||||
|
},
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
const getIsOk = (app: AppType, status: number) => {
|
const getIsOk = (app: AppType, status: number) => {
|
||||||
if (app.network.okStatus === undefined || app.network.statusCodes.length >= 1) {
|
if (app.network.okStatus === undefined || app.network.statusCodes.length >= 1) {
|
||||||
Consola.log('Using new status codes');
|
Consola.log('Using new status codes');
|
||||||
return app.network.statusCodes.includes(status.toString());
|
return app.network.statusCodes.includes(status.toString());
|
||||||
}
|
}
|
||||||
Consola.warn('Using deprecated okStatus');
|
Consola.warn('Using deprecated okStatus');
|
||||||
return app.network.okStatus.includes(status);
|
return app.network.okStatus.includes(status);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,2 +1 @@
|
|||||||
export * from './ping';
|
|
||||||
export * from './overseerr';
|
export * from './overseerr';
|
||||||
|
|||||||
@@ -1,11 +1,14 @@
|
|||||||
import { createTRPCRouter } from '~/server/api/trpc';
|
import { createTRPCRouter } from '~/server/api/trpc';
|
||||||
|
import { appRouter } from './routers/app';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This is the primary router for your server.
|
* This is the primary router for your server.
|
||||||
*
|
*
|
||||||
* All routers added in /api/routers should be manually added here.
|
* All routers added in /api/routers should be manually added here.
|
||||||
*/
|
*/
|
||||||
export const rootRouter = createTRPCRouter({});
|
export const rootRouter = createTRPCRouter({
|
||||||
|
app: appRouter,
|
||||||
|
});
|
||||||
|
|
||||||
// export type definition of API
|
// export type definition of API
|
||||||
export type RootRouter = typeof rootRouter;
|
export type RootRouter = typeof rootRouter;
|
||||||
|
|||||||
46
src/server/api/routers/app.ts
Normal file
46
src/server/api/routers/app.ts
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
import { z } from 'zod';
|
||||||
|
import axios, { AxiosError } from 'axios';
|
||||||
|
import https from 'https';
|
||||||
|
import Consola from 'consola';
|
||||||
|
import { TRPCError } from '@trpc/server';
|
||||||
|
import { createTRPCRouter, publicProcedure } from '../trpc';
|
||||||
|
|
||||||
|
export const appRouter = createTRPCRouter({
|
||||||
|
ping: publicProcedure
|
||||||
|
.input(
|
||||||
|
z.object({
|
||||||
|
url: z.string(),
|
||||||
|
})
|
||||||
|
)
|
||||||
|
.query(async ({ input }) => {
|
||||||
|
const agent = new https.Agent({ rejectUnauthorized: false });
|
||||||
|
const res = await axios
|
||||||
|
.get(input.url, { httpsAgent: agent, timeout: 2000 })
|
||||||
|
.then((response) => ({
|
||||||
|
status: response.status,
|
||||||
|
statusText: response.statusText,
|
||||||
|
}))
|
||||||
|
.catch((error: AxiosError) => {
|
||||||
|
if (error.response) {
|
||||||
|
Consola.warn(`Unexpected response: ${error.message}`);
|
||||||
|
return {
|
||||||
|
status: error.response.status,
|
||||||
|
statusText: error.response.statusText,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
if (error.code === 'ECONNABORTED') {
|
||||||
|
throw new TRPCError({
|
||||||
|
code: 'TIMEOUT',
|
||||||
|
message: 'Request Timeout',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
Consola.error(`Unexpected error: ${error.message}`);
|
||||||
|
throw new TRPCError({
|
||||||
|
code: 'INTERNAL_SERVER_ERROR',
|
||||||
|
message: 'Internal Server Error',
|
||||||
|
});
|
||||||
|
});
|
||||||
|
return res;
|
||||||
|
}),
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user