refactor: add option to activate the request distribution

This commit is contained in:
Mauricio Siu
2024-09-06 01:32:14 -06:00
parent c4dca57614
commit d3a54163bf
4 changed files with 113 additions and 30 deletions

View File

@@ -24,6 +24,11 @@ export const ShowRequests = () => {
const { mutateAsync: deactivateLogRotate } =
api.settings.deactivateLogRotate.useMutation();
const { data: isActive, refetch } =
api.settings.haveActivateRequests.useQuery();
const { mutateAsync: toggleRequests } =
api.settings.toggleRequests.useMutation();
return (
<>
<Card className="bg-transparent mt-10">
@@ -33,39 +38,62 @@ export const ShowRequests = () => {
<CardDescription>
<span>Showing web and API requests over time</span>
</CardDescription>
{!isLogRotateActive && (
<div className="flex w-fit gap-4">
<Button
onClick={() => {
mutateAsync()
.then(() => {
toast.success("Log rotate activated");
refetchLogRotate();
.then(async () => {
await toggleRequests({ enable: !isActive })
.then(() => {
refetch();
toast.success("Access Log Added to Traefik");
})
.catch((err) => {
toast.error(err.message);
});
})
.catch((err) => {
toast.error(err.message);
});
}}
>
Activate Log Rotate
{isActive ? "Deactivate" : "Activate"}
</Button>
)}
{isLogRotateActive && (
<Button
onClick={() => {
deactivateLogRotate()
.then(() => {
toast.success("Log rotate deactivated");
refetchLogRotate();
})
.catch((err) => {
toast.error(err.message);
});
}}
>
Deactivate Log Rotate
</Button>
)}
{!isLogRotateActive && (
<Button
variant="secondary"
onClick={() => {
mutateAsync()
.then(() => {
toast.success("Log rotate activated");
refetchLogRotate();
})
.catch((err) => {
toast.error(err.message);
});
}}
>
Activate Log Rotate
</Button>
)}
{isLogRotateActive && (
<Button
variant="secondary"
onClick={() => {
deactivateLogRotate()
.then(() => {
toast.success("Log rotate deactivated");
refetchLogRotate();
})
.catch((err) => {
toast.error(err.message);
});
}}
>
Deactivate Log Rotate
</Button>
)}
</div>
</div>
</CardHeader>
<CardContent>

View File

@@ -53,6 +53,7 @@ import {
} from "../services/settings";
import { canAccessToTraefikFiles } from "../services/user";
import { adminProcedure, createTRPCRouter, protectedProcedure } from "../trpc";
import { dump, load } from "js-yaml";
export const settingsRouter = createTRPCRouter({
reloadServer: adminProcedure.mutation(async () => {
@@ -382,4 +383,64 @@ export const settingsRouter = createTRPCRouter({
getLogRotateStatus: adminProcedure.query(async () => {
return await logRotationManager.getStatus();
}),
haveActivateRequests: adminProcedure.query(async () => {
const config = readMainConfig();
if (!config) return false;
const parsedConfig = load(config) as {
accessLog?: {
filePath: string;
};
};
return !!parsedConfig?.accessLog?.filePath;
}),
toggleRequests: adminProcedure
.input(
z.object({
enable: z.boolean(),
}),
)
.mutation(async ({ input }) => {
const config = readMainConfig();
if (!config) return false;
if (input.enable) {
const parsedConfig = load(config) as {
accessLog?: {
filePath: string;
format: string;
bufferingSize: number;
filters?: {
retryAttempts?: boolean;
minDuration?: string;
};
};
};
const config2 = {
accessLog: {
filePath: "/etc/dokploy/traefik/dynamic/access.log",
format: "json",
bufferingSize: 100,
filters: {
retryAttempts: true,
minDuration: "10ms",
},
},
};
parsedConfig.accessLog = config2.accessLog;
writeMainConfig(dump(parsedConfig));
} else {
const parsedConfig = load(config) as {
accessLog?: {
filePath: string;
};
};
delete parsedConfig.accessLog;
writeMainConfig(dump(parsedConfig));
}
return true;
}),
});

View File

@@ -59,12 +59,6 @@ export const createApplication = async (
if (process.env.NODE_ENV === "development") {
createTraefikConfig(newApplication.appName);
await tx.insert(domains).values({
applicationId: newApplication.applicationId,
host: `${newApplication.appName}.docker.localhost`,
port: process.env.NODE_ENV === "development" ? 3000 : 80,
certificateType: "none",
});
}
return newApplication;

View File

@@ -1,7 +1,7 @@
import fs, { writeFileSync } from "node:fs";
import path from "node:path";
import type { Domain } from "@/server/api/services/domain";
import { DYNAMIC_TRAEFIK_PATH } from "@/server/constants";
import { DYNAMIC_TRAEFIK_PATH, MAIN_TRAEFIK_PATH } from "@/server/constants";
import { dump, load } from "js-yaml";
import type { FileConfig, HttpLoadBalancerService } from "./file-types";