File size: 4,784 Bytes
6b211e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b73cd07
6b211e3
b73cd07
 
6b211e3
 
b73cd07
6b211e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b73cd07
 
 
 
 
 
6b211e3
 
 
 
 
 
 
 
 
 
 
 
 
b73cd07
 
 
 
6b211e3
 
 
 
 
 
 
b73cd07
6b211e3
b73cd07
6b211e3
 
 
 
 
 
 
 
 
b73cd07
 
 
6b211e3
 
 
b73cd07
 
6b211e3
 
 
 
 
 
 
 
 
 
 
 
 
 
b73cd07
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6b211e3
 
 
 
 
 
b73cd07
6b211e3
 
b73cd07
 
6b211e3
b73cd07
 
 
6b211e3
b73cd07
 
 
6b211e3
 
b73cd07
 
 
 
 
 
 
 
 
 
 
 
 
 
6b211e3
b73cd07
 
 
 
6b211e3
 
b73cd07
6b211e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b73cd07
6b211e3
 
 
b73cd07
 
 
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import express from "express";
import path from "path";
import { createServer as createViteServer } from "vite";
import dotenv from "dotenv";

dotenv.config();

const app = express();
const PORT = 3000;

app.use(express.json({ limit: "50mb" }));
app.use(express.urlencoded({ limit: "50mb", extended: true }));

app.use((req, res, next) => {
  res.header("Access-Control-Allow-Origin", "*");
  res.header(
    "Access-Control-Allow-Headers",
    "Origin, X-Requested-With, Content-Type, Accept, Authorization"
  );
  res.header(
    "Access-Control-Allow-Methods",
    "GET, POST, PUT, DELETE, OPTIONS"
  );

  if (req.method === "OPTIONS") {
    return res.sendStatus(200);
  }

  next();
});

// Proxy Endpoint
app.get("/api/proxy", async (req, res) => {
  const targetUrl = req.query.url as string;

  if (!targetUrl) {
    return res.status(400).json({
      error: "الرابط مطلوب"
    });
  }

  try {
    const formattedUrl = targetUrl.startsWith("http")
      ? targetUrl
      : `https://${targetUrl}`;

    const response = await fetch(formattedUrl);

    if (!response.ok) {
      return res.status(response.status).json({
        error: `فشل جلب الموقع: ${response.statusText}`
      });
    }

    const contentType = response.headers.get("content-type") || "";

    if (
      contentType.includes("html") ||
      contentType.includes("css") ||
      contentType.includes("javascript") ||
      contentType.includes("text") ||
      contentType.includes("json")
    ) {
      const text = await response.text();
      res.setHeader("Content-Type", contentType);
      return res.send(text);
    }

    const buffer = await response.arrayBuffer();
    res.setHeader("Content-Type", contentType);
    return res.send(Buffer.from(buffer));
  } catch (err: any) {
    return res.status(500).json({
      error: err.message
    });
  }
});

// Groq AI Endpoint
app.post("/api/ai/chat", async (req, res) => {
  const { messages, customApiKey } = req.body;

  if (!messages || !Array.isArray(messages)) {
    return res.status(400).json({
      error: "المحادثة غير صالحة"
    });
  }

  const apiKeys = [
    customApiKey,
    process.env.GROQ_API_KEY,
    process.env.GROQ_API_KEY_2,
    process.env.GROQ_API_KEY_3
  ].filter(Boolean);

  if (!apiKeys.length) {
    return res.status(400).json({
      error: "لا يوجد مفتاح Groq"
    });
  }

  const systemPrompt = `
أنت مساعد SiteClone Pro v20.
متخصص في:
- Web cloning
- Workflows
- APIs
- Debugging
- Hosting
أجب دائمًا بالعربية.
`;

  const formattedMessages = [
    {
      role: "system",
      content: systemPrompt
    },
    ...messages.map((m: any) => ({
      role: m.role === "model" ? "assistant" : "user",
      content: m.content
    }))
  ];

  let lastError: any = null;

  for (const key of apiKeys) {
    try {
      const controller = new AbortController();
      const timeoutId = setTimeout(() => controller.abort(), 20000);

      const response = await fetch(
        "https://api.groq.com/openai/v1/chat/completions",
        {
          method: "POST",
          headers: {
            "Content-Type": "application/json",
            Authorization: `Bearer ${key}`
          },
          body: JSON.stringify({
            model: "llama-3.3-70b-versatile",
            messages: formattedMessages,
            temperature: 0.7
          }),
          signal: controller.signal
        }
      );

      clearTimeout(timeoutId);

      if (!response.ok) {
        const errText = await response.text();
        throw new Error(errText);
      }

      const data = await response.json();

      return res.json({
        text:
          data.choices?.[0]?.message?.content ||
          "لم يتم الحصول على رد.",
        activeProvider: "Groq",
        activeModel: "llama-3.3-70b-versatile"
      });
    } catch (err: any) {
      console.warn("Groq Key Failed:", err.message);
      lastError = err;
      continue;
    }
  }

  return res.status(500).json({
    error: `فشل جميع مفاتيح Groq: ${lastError?.message}`
  });
});

// Vite / Production Static Serving
async function initServer() {
  if (process.env.NODE_ENV !== "production") {
    const vite = await createViteServer({
      server: {
        middlewareMode: true
      },
      appType: "spa"
    });

    app.use(vite.middlewares);
  } else {
    const distPath = path.join(process.cwd(), "dist");

    app.use(express.static(distPath));

    app.get("*", (req, res) => {
      res.sendFile(path.join(distPath, "index.html"));
    });
  }

  app.listen(PORT, "0.0.0.0", () => {
    console.log(`SiteClone Pro running on port ${PORT}`);
  });
}

initServer().catch((err) => {
  console.error("Initialization error:", err);
});