-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
409 lines (369 loc) · 17 KB
/
server.js
File metadata and controls
409 lines (369 loc) · 17 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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
const http = require("http");
const url = require("url");
const { request } = require("http");
const https = require("https");
const fs = require("fs");
const path = require("path");
const dotenv = require('dotenv');
dotenv.config();
const UNSPLASH_ACCESS_KEY = process.env.UNSPLASH_ACCESS_KEY;
const PORT = process.env.PORT || 3001;
const AI_MODEL = process.env.AI_MODEL || 'llama3.2';
const OLLAMA_API_HOST = process.env.OLLAMA_API_HOST || 'localhost';
const OLLAMA_API_PORT = process.env.OLLAMA_API_PORT || 11434;
const OPENAI_API_HOST = process.env.OPENAI_API_HOST || 'https://api.openai.com';
const OPENAI_API_KEY = process.env.OPENAI_API_KEY;
const API_PROVIDER = process.env.API_PROVIDER || 'ollama';
const GOOGLE_AI_API_KEY = process.env.GOOGLE_AI_API_KEY;
const handleStaticFile = (res, fileName, contentType = "text/html", errorType = 'Error', errorMessage = 'Ocorreu um erro.') => {
if (fileName) {
const filePath = path.join(__dirname, fileName);
fs.readFile(filePath, (err, content) => {
if (err) {
res.writeHead(500, { "Content-Type": "text/plain" });
res.end(`Erro interno do servidor ao carregar ${fileName}`);
console.error(`Falha ao carregar ${fileName}: ${err}`);
return;
} else {
res.writeHead(200, { "Content-Type": contentType });
res.end(content);
}
});
} else {
const filePath = path.join(__dirname, 'error.html');
fs.readFile(filePath, (err, content) => {
if (err) {
res.writeHead(500, { "Content-Type": "text/plain" });
res.end(`Erro interno do servidor ao carregar error.html`);
console.error(`Falha ao carregar error.html: ${err}`);
return;
} else {
let errorContent = content.toString();
errorContent = errorContent.replace('<title id="error-title">Error</title>', `<title id="error-title">${errorType}</title>`);
errorContent = errorContent.replace('<h1 id="error-heading">Error!</h1>', `<h1 id="error-heading">${errorType}!</h1>`);
errorContent = errorContent.replace('<p >Ocorreu um erro.</p >', `<p>${errorMessage}</p>`);
res.writeHead(200, { "Content-Type": "text/html; charset=utf-8" });
res.end(errorContent);
}
});
}
};
const handleAIResponse = (res, siteContent) => {
if (!siteContent) {
console.error("Site content is null, cannot proceed with filtering.");
handleStaticFile(res, null, "text/html; charset=utf-8", "AI Response Error", "There was an error processing your request with the AI model. Please try again. If the issue persists, consider checking the AI model configuration.");
return;
}
res.writeHead(200, {
"Content-Type": "text/html; charset=utf-8",
});
res.end(siteContent);
};
const generateSite = (res, language, mensagem) => {
const message = mensagem === undefined ? 'Web developer looking for a job' : mensagem;
const targetLanguage = language === 'pt-br' ? 'pt-br' : 'en-us';
const imagePromptRequest = {
model: AI_MODEL,
prompt: `Generate 3-5 keywords in "${targetLanguage}" language for Unsplash image search related to "${message}". Output format: keyword1, keyword2, keyword3`,
stream: false,
};
console.log("Sending image prompt request to AI:", imagePromptRequest);
makeAIRequest(imagePromptRequest, (imagePrompt) => {
console.log("Received image prompt from AI:", imagePrompt);
searchUnsplashImages(imagePrompt, (imageUrls) => {
if (!imageUrls || imageUrls.length === 0) {
console.error("No images found for the given prompt.");
handleStaticFile(res, null, "text/html; charset=utf-8", "Image Search Error", "Unfortunately, we could not find any relevant images for your request. Please try a different search term or ensure your query is valid.");
return;
}
const sitePrompt = `Create HTML for a simple, well-structured, and informative landing page about "${message}" using "${targetLanguage}". Requirements:
1. Responsive design using flexbox or CSS grid
2. Clean and modern aesthetic
3. Simple navigation with Home(/index.html), About(/about.html), and Author(https://github.com/Unix-User) links at the top of the page
4. Hero section with a relevant Unsplash image and a clear, concise headline
5. Brief "About" section explaining the main concept or service
6. Key features or benefits section (3-4 points)
7. Simple and responsive and elegant call-to-action (CTA) button
8. Minimal footer with essential links at the bottom of the page
9. Use this image URL for the hero section: ${imageUrls[0] || 'https://placekitten.com/800/400'}
10. Use a color scheme that complements the hero image
11. Include the following meta tags in the <head>:
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta property="og:title" content="Ollama Landing Page Generator" />
<meta property="og:description" content="Landing page generated by AI" />
<meta property="og:image" content="https://olpg.udianix.com.br/ollama.jpeg" />
<meta property="og:url" content="https://olpg.udianix.com.br" />
<meta property="og:type" content="website" />
<meta name="twitter:card" content="summary_large_image" />
<meta name="twitter:title" content="Ollama Landing Page Generator" />
<meta name="twitter:description" content="Landing page generated by AI" />
<meta name="twitter:image" content="https://olpg.udianix.com.br/ollama.jpeg" />
12. Optimize for fast loading and readability
Output: Single HTML file with inline CSS and minimal JS (if needed). Include only <head> and <body> tags.
Any explanations or comments should be included as HTML comments within the code.
Focus on clarity, simplicity, and effective communication of the main message.
Ensure the structure follows this order: navigation, hero, about, features, CTA, footer.
DO NOT INCLUDE ANY TEXT OR CODE OUTSIDE OF THE HTML STRUCTURE.
`;
const siteRequest = {
model: AI_MODEL,
prompt: sitePrompt,
stream: false,
};
console.log("Sending site generation request to AI:", siteRequest);
makeAIRequest(siteRequest, (siteContent) => {
console.log("Received site content from AI:", siteContent);
handleAIResponse(res, siteContent);
});
});
});
};
http.createServer((req, res) => {
const parsedUrl = url.parse(req.url, true);
console.log(`Request received for: ${parsedUrl.pathname}`);
switch (parsedUrl.pathname) {
case "/":
case "/index.html":
handleStaticFile(res, "index.html");
break;
case "/index-en.html":
handleStaticFile(res, "index-en.html");
break;
case "/about.html":
handleStaticFile(res, "about.html");
break;
case "/about-en.html":
handleStaticFile(res, "about-en.html");
break;
case "/ollama.jpeg":
handleStaticFile(res, "ollama.jpeg", "image/jpeg");
break;
case "/generate":
const { mensagem, language } = parsedUrl.query;
generateSite(res, language, mensagem);
break;
default:
handleStaticFile(res, null, "text/html; charset=utf-8", "Não Encontrado", `Recurso '${parsedUrl.pathname}' não encontrado no servidor.`);
}
}).listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});
async function makeAIRequest(postData, callback) {
console.log(`API Provider selected: ${API_PROVIDER}`);
try {
if (API_PROVIDER === 'ollama') {
const options = {
hostname: OLLAMA_API_HOST,
port: OLLAMA_API_PORT,
path: "/api/generate",
method: "POST",
headers: {
"Content-Type": "application/json",
"Content-Length": Buffer.byteLength(JSON.stringify(postData)),
},
};
console.log("Making AI request to Ollama:", `http://${options.hostname}:${options.port}${options.path}`, "with data:", postData);
const req = request(options, (response) => {
let data = "";
response.on("data", (chunk) => {
data += chunk;
});
response.on("end", () => {
try {
const jsonResponse = JSON.parse(data);
if (jsonResponse && jsonResponse.response) {
let responseText = jsonResponse.response;
responseText = responseText.replace(/```html/g, '').replace(/```/g, '');
callback(responseText);
} else {
console.error("Unexpected Ollama response structure:", jsonResponse);
callback(null);
}
} catch (error) {
console.error("Error parsing Ollama response:", error);
callback(null);
}
});
});
req.on("error", (error) => {
console.error("Error making Ollama request:", error);
callback(null);
});
req.write(JSON.stringify(postData));
req.end();
} else if (API_PROVIDER === 'openai') {
const data = JSON.stringify({
model: AI_MODEL,
messages: [{ role: 'user', content: postData.prompt }],
});
const options = {
hostname: new URL(OPENAI_API_HOST).hostname,
path: new URL(OPENAI_API_HOST).pathname + '/chat/completions',
method: 'POST',
headers: {
'Authorization': `Bearer ${OPENAI_API_KEY}`,
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(data),
},
};
console.log("Sending request to OpenAI with data:", data);
const req = https.request(options, (response) => {
let responseData = '';
response.on('data', (chunk) => {
responseData += chunk;
});
response.on('end', () => {
console.log("Received response from OpenAI:", responseData);
try {
const jsonResponse = JSON.parse(responseData);
if (jsonResponse.error) {
console.error("OpenAI API error:", jsonResponse.error.message);
callback(null);
return;
}
if (jsonResponse.choices && jsonResponse.choices.length > 0) {
let siteContent = jsonResponse.choices[0].message.content;
if (siteContent) {
siteContent = siteContent.replace(/```html/g, '').replace(/```/g, '');
callback(siteContent);
} else {
console.error("OpenAI returned empty content.");
callback(null);
}
} else {
console.error("Unexpected response structure from OpenAI:", jsonResponse);
callback(null);
}
} catch (error) {
console.error("Error parsing OpenAI response:", error);
callback(null);
}
});
});
req.on('error', (error) => {
console.error("Error making OpenAI request:", error);
callback(null);
});
req.write(data);
req.end();
} else if (API_PROVIDER === 'gemini') {
const data = JSON.stringify({
contents: [{
parts: [{ text: postData.prompt }]
}]
});
const options = {
hostname: 'generativelanguage.googleapis.com',
path: `/v1beta/models/${AI_MODEL}:generateContent?key=${GOOGLE_AI_API_KEY}`,
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(data),
},
};
const fullUrl = `https://${options.hostname}${options.path}`;
console.log("Sending request to Gemini URL:", fullUrl);
console.log("Sending request to Gemini with data:", data);
const req = https.request(options, (response) => {
let responseData = '';
response.on('data', (chunk) => {
responseData += chunk;
});
response.on('end', () => {
if (responseData.trim().startsWith('<')) {
console.error("Received an HTML response instead of JSON. URL might be incorrect. Response:", responseData);
callback(null);
return;
}
console.log("Received response from Gemini:", responseData);
try {
const jsonResponse = JSON.parse(responseData);
if (jsonResponse.error) {
console.error("Gemini API error:", jsonResponse.error.message);
callback(null);
return;
}
if (!jsonResponse.candidates || jsonResponse.candidates.length === 0) {
console.error("No candidates returned from Gemini. The prompt might have been blocked.", jsonResponse);
callback(null);
return;
}
let siteContent = jsonResponse.candidates[0].content.parts[0].text;
if (siteContent) {
siteContent = siteContent.replace(/```html/g, '').replace(/```/g, '');
callback(siteContent);
} else {
console.error("Gemini returned empty content.");
callback(null);
}
} catch (error) {
console.error("Error parsing Gemini response:", error);
callback(null);
}
});
});
req.on('error', (error) => {
console.error("Error making Gemini request:", error);
callback(null);
});
req.write(data);
req.end();
} else {
console.error(`Unsupported API provider: ${API_PROVIDER}`);
callback(null);
}
} catch (error) {
console.error("Error in makeAIRequest:", error);
callback(null);
}
}
function searchUnsplashImages(query, callback) {
const url = `https://api.unsplash.com/search/photos?query=${encodeURIComponent(
query
)}&per_page=5`;
const options = {
headers: {
Authorization: `Client-ID ${UNSPLASH_ACCESS_KEY}`,
},
};
console.log("Fetching images from Unsplash with URL:", url);
https
.get(url, options, (response) => {
let data = "";
response.on("data", (chunk) => {
data += chunk;
});
response.on("end", () => {
try {
const result = JSON.parse(data);
console.log("Unsplash API response:", result);
if (result && result.results) {
const imageUrls = result.results.map((photo) => photo.urls.regular);
callback(imageUrls);
} else {
console.error("Unexpected Unsplash response structure:", result);
callback([]);
}
} catch (error) {
console.error("Error parsing Unsplash response:", error);
callback([]);
}
});
})
.on("error", (error) => {
console.error("Error fetching images from Unsplash:", error);
callback([]);
});
}
function isValidHTML(content) {
if (!content || typeof content !== 'string') {
return false;
}
const trimmedContent = content.trim();
return trimmedContent.startsWith('<!DOCTYPE html>') &&
trimmedContent.includes('<html') &&
trimmedContent.includes('<head>') &&
trimmedContent.includes('<body') &&
trimmedContent.includes('</html>');
}