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,11 +1,14 @@
|
||||
import { createTRPCRouter } from '~/server/api/trpc';
|
||||
import { appRouter } from './routers/app';
|
||||
|
||||
/**
|
||||
* This is the primary router for your server.
|
||||
*
|
||||
* 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 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