-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathadmin.api.v1.runs-replication.start-monitor.ts
More file actions
59 lines (48 loc) · 1.69 KB
/
admin.api.v1.runs-replication.start-monitor.ts
File metadata and controls
59 lines (48 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import { ActionFunctionArgs, json } from "@remix-run/server-runtime";
import { z } from "zod";
import { prisma } from "~/db.server";
import { startTcpBufferMonitor } from "~/services/monitorTcpBuffers.server";
import { authenticateApiRequestWithPersonalAccessToken } from "~/services/personalAccessToken.server";
import { getTcpMonitorGlobal, setTcpMonitorGlobal } from "~/services/runsReplicationGlobal.server";
const schema = z.object({
intervalMs: z.number().min(1000).max(60_000).default(5_000),
});
export async function action({ request }: ActionFunctionArgs) {
// Next authenticate the request
const authenticationResult = await authenticateApiRequestWithPersonalAccessToken(request);
if (!authenticationResult) {
return json({ error: "Invalid or Missing API key" }, { status: 401 });
}
const user = await prisma.user.findUnique({
where: {
id: authenticationResult.userId,
},
});
if (!user) {
return json({ error: "Invalid or Missing API key" }, { status: 401 });
}
if (!user.admin) {
return json({ error: "You must be an admin to perform this action" }, { status: 403 });
}
try {
const body = await request.json();
const { intervalMs } = schema.parse(body);
const globalMonitor = getTcpMonitorGlobal();
if (globalMonitor) {
return json(
{
error: "Tcp buffer monitor already running, you must stop it before starting a new one",
},
{
status: 400,
}
);
}
setTcpMonitorGlobal(startTcpBufferMonitor(intervalMs));
return json({
success: true,
});
} catch (error) {
return json({ error: error instanceof Error ? error.message : error }, { status: 400 });
}
}