feat(api): add dockerfile api

This commit is contained in:
Mauricio Siu
2024-09-30 00:27:14 -06:00
parent 06a772e344
commit 2872ef3ccb
9 changed files with 127 additions and 153 deletions

View File

@@ -1,9 +1,11 @@
{
"name": "my-app",
"name": "@dokploy/api",
"version": "0.0.1",
"type": "module",
"scripts": {
"dev": "PORT=4000 tsx watch src/index.ts",
"dev2": "PORT=4001 tsx watch src/index.ts",
"tsc": "tsc --project tsconfig.json"
"build": "tsc --project tsconfig.json",
"start": "node dist/index.js"
},
"dependencies": {
"react": "18.2.0",
@@ -13,13 +15,8 @@
"hono": "^4.5.8",
"dotenv": "^16.3.1",
"@upstash/qstash": "2.7.9",
"ioredis": "5.4.1",
"nats": "2.28.2",
"bullmq": "5.13.2",
"@nerimity/mimiqueue": "1.2.3",
"timers": "0.1.1",
"redis": "4.7.0",
"date-fns": "4.1.0"
"@nerimity/mimiqueue": "1.2.3"
},
"devDependencies": {
"typescript": "^5.4.2",

View File

@@ -4,7 +4,6 @@ import "dotenv/config";
import { createClient } from "redis";
import { Queue } from "@nerimity/mimiqueue";
import { deployApplication } from "@dokploy/builders";
// import { setTimeout } from "timers/promises";
const app = new Hono();
const redisClient = createClient({
@@ -47,7 +46,7 @@ const queue = new Queue({
},
redisClient,
});
const port = process.env.PORT;
const port = Number.parseInt(process.env.PORT || "3000");
(async () => {
await redisClient.connect();
await redisClient.flushAll();

View File

@@ -1,82 +1,82 @@
import { Hono } from "hono";
import { Client } from "@upstash/qstash";
import { serve } from "@hono/node-server";
import dotenv from "dotenv";
import Redis from "ioredis";
// import { Hono } from "hono";
// import { Client } from "@upstash/qstash";
// import { serve } from "@hono/node-server";
// import dotenv from "dotenv";
// import Redis from "ioredis";
dotenv.config();
// dotenv.config();
const redis = new Redis({
host: "localhost",
port: 7777,
password: "xlfvpQ0ma2BkkkPX",
});
// const redis = new Redis({
// host: "localhost",
// port: 7777,
// password: "xlfvpQ0ma2BkkkPX",
// });
// redis.set("test", "test");
// console.log(await redis.get("test"));
// // redis.set("test", "test");
// // console.log(await redis.get("test"));
// console.log(await redis.get("user-1-processing"));
const app = new Hono();
console.log("QStash Token:", process.env.PUBLIC_URL);
// // console.log(await redis.get("user-1-processing"));
// const app = new Hono();
// console.log("QStash Token:", process.env.PUBLIC_URL);
const qstash = new Client({
token: process.env.QSTASH_TOKEN as string,
});
// const qstash = new Client({
// token: process.env.QSTASH_TOKEN as string,
// });
const queue = qstash.queue({
queueName: "deployments",
});
// const queue = qstash.queue({
// queueName: "deployments",
// });
// Endpoint que publica un mensaje en QStash
app.post("/enqueue", async (c) => {
const { userId, deploymentId } = await c.req.json();
const response = await qstash.publishJSON({
url: `${process.env.PUBLIC_URL}/process`, // Endpoint para procesar la tarea
body: { userId, deploymentId }, // Datos del despliegue
});
// // Endpoint que publica un mensaje en QStash
// app.post("/enqueue", async (c) => {
// const { userId, deploymentId } = await c.req.json();
// const response = await qstash.publishJSON({
// url: `${process.env.PUBLIC_URL}/process`, // Endpoint para procesar la tarea
// body: { userId, deploymentId }, // Datos del despliegue
return c.json({ message: "Task enqueued", id: response.messageId });
});
// });
// Endpoint que recibe el mensaje procesado
app.post("/process", async (c) => {
const { userId, deploymentId } = await c.req.json();
// return c.json({ message: "Task enqueued", id: response.messageId });
// });
const isProcessing = await redis.get(`user-${userId}-processing`);
console.log(`isProcessing for user ${userId}:`, isProcessing);
// // Endpoint que recibe el mensaje procesado
// app.post("/process", async (c) => {
// const { userId, deploymentId } = await c.req.json();
if (isProcessing === "true") {
console.log(
`User ${userId} is already processing a deployment. Queuing the next one.`,
);
return c.json(
{
status: "User is already processing a deployment, waiting...",
},
{
status: 400,
},
);
}
redis.set(`user-${userId}-processing`, "true");
// const isProcessing = await redis.get(`user-${userId}-processing`);
// console.log(`isProcessing for user ${userId}:`, isProcessing);
try {
await new Promise((resolve) => setTimeout(resolve, 5000));
} catch (error) {
} finally {
await redis.del(`user-${userId}-processing`);
}
// if (isProcessing === "true") {
// console.log(
// `User ${userId} is already processing a deployment. Queuing the next one.`,
// );
// return c.json(
// {
// status: "User is already processing a deployment, waiting...",
// },
// {
// status: 400,
// },
// );
// }
// redis.set(`user-${userId}-processing`, "true");
return c.json({ status: "Processed", userId, deploymentId });
});
// try {
// await new Promise((resolve) => setTimeout(resolve, 5000));
// } catch (error) {
// } finally {
// await redis.del(`user-${userId}-processing`);
// }
// Inicia el servidor en el puerto 3000
const port = 3000;
console.log(`Server is running on port http://localhost:${port}`);
// return c.json({ status: "Processed", userId, deploymentId });
// });
serve({
fetch: app.fetch,
port,
});
// 18
// // Inicia el servidor en el puerto 3000
// const port = 3000;
// console.log(`Server is running on port http://localhost:${port}`);
// serve({
// fetch: app.fetch,
// port,
// });
// // 18

View File

@@ -1,6 +1,8 @@
import { LEMON_SQUEEZY_API_KEY, LEMON_SQUEEZY_STORE_ID } from ".";
// import { LEMON_SQUEEZY_API_KEY, LEMON_SQUEEZY_STORE_ID } from ".";
import type { LemonSqueezyLicenseResponse } from "./types";
const LEMON_SQUEEZY_API_KEY = process.env.LEMON_SQUEEZY_API_KEY;
const LEMON_SQUEEZY_STORE_ID = process.env.LEMON_SQUEEZY_STORE_ID;
export const validateLemonSqueezyLicense = async (
licenseKey: string,
): Promise<LemonSqueezyLicenseResponse> => {

View File

@@ -5,10 +5,9 @@
"moduleResolution": "Node",
"strict": true,
"skipLibCheck": true,
"types": ["node"],
"outDir": "dist",
"jsx": "react-jsx",
"jsxImportSource": "hono/jsx",
"traceResolution": true,
"diagnostics": true
}
"jsxImportSource": "hono/jsx"
},
"exclude": ["node_modules", "dist"]
}