mirror of
https://github.com/Dokploy/dokploy
synced 2025-06-26 18:27:59 +00:00
refactor: add authorization
This commit is contained in:
@@ -3,22 +3,62 @@ import { Hono } from "hono";
|
||||
import "dotenv/config";
|
||||
import { zValidator } from "@hono/zod-validator";
|
||||
import { logger } from "./logger";
|
||||
import { cleanQueue, removeJob, scheduleJob } from "./queue";
|
||||
import { cleanQueue, getJobRepeatable, removeJob, scheduleJob } from "./queue";
|
||||
import { jobQueueSchema } from "./schema";
|
||||
import { firstWorker, secondWorker } from "./workers";
|
||||
import { validateBearerTokenAPI } from "@dokploy/server";
|
||||
|
||||
const app = new Hono();
|
||||
|
||||
cleanQueue();
|
||||
|
||||
app.post("/create-backup", zValidator("json", jobQueueSchema), (c) => {
|
||||
app.use(async (c, next) => {
|
||||
const authHeader = c.req.header("authorization");
|
||||
|
||||
if (!authHeader || !authHeader.startsWith("Bearer ")) {
|
||||
return c.json({ message: "Authorization header missing" }, 401);
|
||||
}
|
||||
|
||||
const result = await validateBearerTokenAPI(authHeader);
|
||||
|
||||
if (!result.user || !result.session) {
|
||||
return c.json({ message: "Invalid session" }, 403);
|
||||
}
|
||||
return next();
|
||||
});
|
||||
|
||||
app.post("/create-backup", zValidator("json", jobQueueSchema), async (c) => {
|
||||
const data = c.req.valid("json");
|
||||
scheduleJob(data);
|
||||
|
||||
logger.info("Backup created successfully", data);
|
||||
return c.json({ message: "Backup created successfully" });
|
||||
});
|
||||
|
||||
app.post("/update-backup", zValidator("json", jobQueueSchema), async (c) => {
|
||||
const data = c.req.valid("json");
|
||||
const job = await getJobRepeatable(data);
|
||||
if (job) {
|
||||
let result = false;
|
||||
if (data.type === "backup") {
|
||||
result = await removeJob({
|
||||
backupId: data.backupId,
|
||||
type: "backup",
|
||||
cronSchedule: job.pattern,
|
||||
});
|
||||
} else if (data.type === "server") {
|
||||
result = await removeJob({
|
||||
serverId: data.serverId,
|
||||
type: "server",
|
||||
cronSchedule: job.pattern,
|
||||
});
|
||||
}
|
||||
logger.info("Job removed", result);
|
||||
}
|
||||
scheduleJob(data);
|
||||
|
||||
return c.json({ message: "Backup updated successfully" });
|
||||
});
|
||||
|
||||
app.post("/remove-job", zValidator("json", jobQueueSchema), async (c) => {
|
||||
const data = c.req.valid("json");
|
||||
const result = await removeJob(data);
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Queue } from "bullmq";
|
||||
import { Queue, type RepeatableJob } from "bullmq";
|
||||
import { logger } from "./logger";
|
||||
import type { QueueJob } from "./schema";
|
||||
|
||||
@@ -52,4 +52,24 @@ export const removeJob = async (data: QueueJob) => {
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
|
||||
export const getJobRepeatable = async (
|
||||
data: QueueJob,
|
||||
): Promise<RepeatableJob | null> => {
|
||||
const repeatableJobs = await jobQueue.getRepeatableJobs();
|
||||
if (data.type === "backup") {
|
||||
const { backupId } = data;
|
||||
const job = repeatableJobs.find((j) => j.name === backupId);
|
||||
return job ? job : null;
|
||||
}
|
||||
if (data.type === "server") {
|
||||
const { serverId } = data;
|
||||
const job = repeatableJobs.find((j) => j.name === `${serverId}-cleanup`);
|
||||
return job ? job : null;
|
||||
}
|
||||
|
||||
return null;
|
||||
};
|
||||
|
||||
@@ -5,6 +5,7 @@ import { runJobs } from "./utils";
|
||||
export const firstWorker = new Worker(
|
||||
"backupQueue",
|
||||
async (job: Job<QueueJob>) => {
|
||||
console.log("Job received", job.data);
|
||||
await runJobs(job.data);
|
||||
},
|
||||
{
|
||||
@@ -17,6 +18,7 @@ export const firstWorker = new Worker(
|
||||
export const secondWorker = new Worker(
|
||||
"backupQueue",
|
||||
async (job: Job<QueueJob>) => {
|
||||
console.log(job.data);
|
||||
await runJobs(job.data);
|
||||
},
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user