mirror of
https://github.com/andwati/dokploy.git
synced 2026-12-06 01:28:46 +00:00
refactor: add authorization
This commit is contained in:
@@ -254,7 +254,7 @@ export const applicationRouter = createTRPCRouter({
|
||||
|
||||
if (IS_CLOUD && application.serverId) {
|
||||
jobData.serverId = application.serverId;
|
||||
await deploy(jobData);
|
||||
await deploy(jobData, ctx.session.id);
|
||||
return true;
|
||||
}
|
||||
await myQueue.add(
|
||||
@@ -482,7 +482,7 @@ export const applicationRouter = createTRPCRouter({
|
||||
};
|
||||
if (IS_CLOUD && application.serverId) {
|
||||
jobData.serverId = application.serverId;
|
||||
await deploy(jobData);
|
||||
await deploy(jobData, ctx.session.id);
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -571,7 +571,7 @@ export const applicationRouter = createTRPCRouter({
|
||||
};
|
||||
if (IS_CLOUD && app.serverId) {
|
||||
jobData.serverId = app.serverId;
|
||||
await deploy(jobData);
|
||||
await deploy(jobData, ctx.session.id);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@ import {
|
||||
apiRemoveBackup,
|
||||
apiUpdateBackup,
|
||||
} from "@/server/db/schema";
|
||||
import { removeJob, schedule } from "@/server/utils/backup";
|
||||
import { removeJob, schedule, updateJob } from "@/server/utils/backup";
|
||||
import {
|
||||
IS_CLOUD,
|
||||
createBackup,
|
||||
@@ -29,18 +29,21 @@ import { TRPCError } from "@trpc/server";
|
||||
export const backupRouter = createTRPCRouter({
|
||||
create: protectedProcedure
|
||||
.input(apiCreateBackup)
|
||||
.mutation(async ({ input }) => {
|
||||
.mutation(async ({ input, ctx }) => {
|
||||
try {
|
||||
const newBackup = await createBackup(input);
|
||||
|
||||
const backup = await findBackupById(newBackup.backupId);
|
||||
|
||||
if (IS_CLOUD && backup.enabled) {
|
||||
await schedule({
|
||||
cronSchedule: backup.schedule,
|
||||
backupId: backup.backupId,
|
||||
type: "backup",
|
||||
});
|
||||
await schedule(
|
||||
{
|
||||
cronSchedule: backup.schedule,
|
||||
backupId: backup.backupId,
|
||||
type: "backup",
|
||||
},
|
||||
ctx.session.id,
|
||||
);
|
||||
} else {
|
||||
if (backup.enabled) {
|
||||
scheduleBackup(backup);
|
||||
@@ -60,17 +63,31 @@ export const backupRouter = createTRPCRouter({
|
||||
}),
|
||||
update: protectedProcedure
|
||||
.input(apiUpdateBackup)
|
||||
.mutation(async ({ input }) => {
|
||||
.mutation(async ({ input, ctx }) => {
|
||||
try {
|
||||
await updateBackupById(input.backupId, input);
|
||||
const backup = await findBackupById(input.backupId);
|
||||
|
||||
if (IS_CLOUD && backup.enabled) {
|
||||
await schedule({
|
||||
cronSchedule: backup.schedule,
|
||||
backupId: backup.backupId,
|
||||
type: "backup",
|
||||
});
|
||||
if (IS_CLOUD) {
|
||||
if (backup.enabled) {
|
||||
await updateJob(
|
||||
{
|
||||
cronSchedule: backup.schedule,
|
||||
backupId: backup.backupId,
|
||||
type: "backup",
|
||||
},
|
||||
ctx.session.id,
|
||||
);
|
||||
} else {
|
||||
await removeJob(
|
||||
{
|
||||
cronSchedule: backup.schedule,
|
||||
backupId: backup.backupId,
|
||||
type: "backup",
|
||||
},
|
||||
ctx.session.id,
|
||||
);
|
||||
}
|
||||
} else {
|
||||
if (backup.enabled) {
|
||||
removeScheduleBackup(input.backupId);
|
||||
@@ -88,16 +105,19 @@ export const backupRouter = createTRPCRouter({
|
||||
}),
|
||||
remove: protectedProcedure
|
||||
.input(apiRemoveBackup)
|
||||
.mutation(async ({ input }) => {
|
||||
.mutation(async ({ input, ctx }) => {
|
||||
try {
|
||||
const value = await removeBackupById(input.backupId);
|
||||
if (IS_CLOUD && value) {
|
||||
removeJob({
|
||||
backupId: input.backupId,
|
||||
cronSchedule: value.schedule,
|
||||
type: "backup",
|
||||
});
|
||||
} else {
|
||||
removeJob(
|
||||
{
|
||||
backupId: input.backupId,
|
||||
cronSchedule: value.schedule,
|
||||
type: "backup",
|
||||
},
|
||||
ctx.session.id,
|
||||
);
|
||||
} else if (!IS_CLOUD) {
|
||||
removeScheduleBackup(input.backupId);
|
||||
}
|
||||
return value;
|
||||
|
||||
@@ -256,7 +256,7 @@ export const composeRouter = createTRPCRouter({
|
||||
|
||||
if (IS_CLOUD && compose.serverId) {
|
||||
jobData.serverId = compose.serverId;
|
||||
await deploy(jobData);
|
||||
await deploy(jobData, ctx.session.id);
|
||||
return true;
|
||||
}
|
||||
await myQueue.add(
|
||||
@@ -288,7 +288,7 @@ export const composeRouter = createTRPCRouter({
|
||||
};
|
||||
if (IS_CLOUD && compose.serverId) {
|
||||
jobData.serverId = compose.serverId;
|
||||
await deploy(jobData);
|
||||
await deploy(jobData, ctx.session.id);
|
||||
return true;
|
||||
}
|
||||
await myQueue.add(
|
||||
|
||||
@@ -9,12 +9,13 @@ type QueueJob =
|
||||
cronSchedule: string;
|
||||
serverId: string;
|
||||
};
|
||||
export const schedule = async (job: QueueJob) => {
|
||||
export const schedule = async (job: QueueJob, authSession: string) => {
|
||||
try {
|
||||
const result = await fetch(`${process.env.JOBS_URL}/create-backup`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${authSession}`,
|
||||
},
|
||||
body: JSON.stringify(job),
|
||||
});
|
||||
@@ -27,12 +28,32 @@ export const schedule = async (job: QueueJob) => {
|
||||
}
|
||||
};
|
||||
|
||||
export const removeJob = async (job: QueueJob) => {
|
||||
export const removeJob = async (job: QueueJob, authSession: string) => {
|
||||
try {
|
||||
const result = await fetch(`${process.env.JOBS_URL}/remove-job`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${authSession}`,
|
||||
},
|
||||
body: JSON.stringify(job),
|
||||
});
|
||||
const data = await result.json();
|
||||
console.log(data);
|
||||
return data;
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
export const updateJob = async (job: QueueJob, authSession: string) => {
|
||||
try {
|
||||
const result = await fetch(`${process.env.JOBS_URL}/update-backup`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${authSession}`,
|
||||
},
|
||||
body: JSON.stringify(job),
|
||||
});
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
import type { DeploymentJob } from "../queues/deployments-queue";
|
||||
|
||||
export const deploy = async (jobData: DeploymentJob) => {
|
||||
export const deploy = async (jobData: DeploymentJob, sessionId: string) => {
|
||||
try {
|
||||
const result = await fetch(`${process.env.SERVER_URL}/deploy`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${sessionId}`,
|
||||
},
|
||||
body: JSON.stringify(jobData),
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user