Update server.js
Browse files
server.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
| 1 |
import express from "express";
|
| 2 |
import fetch from "node-fetch";
|
| 3 |
import dotenv from "dotenv";
|
|
|
|
|
|
|
| 4 |
dotenv.config();
|
| 5 |
|
| 6 |
const app = express();
|
|
@@ -14,37 +16,69 @@ if (!process.env.OPENAI_KEY) {
|
|
| 14 |
app.use(express.json());
|
| 15 |
app.use(express.static("public"));
|
| 16 |
|
| 17 |
-
/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
async function fetchEnglishVerse(book, ch, v) {
|
| 19 |
-
const
|
| 20 |
-
|
| 21 |
-
|
| 22 |
|
| 23 |
-
//
|
| 24 |
console.log("Vers lekérése ezzel az URL-lel:", url);
|
| 25 |
|
| 26 |
-
const r = await
|
| 27 |
if (!r.ok) {
|
| 28 |
-
// <<< NAPLÓZÁS
|
| 29 |
console.error("Bible API hiba státuszkód:", r.status, r.statusText);
|
| 30 |
throw new Error("bible-api hiba");
|
| 31 |
}
|
| 32 |
const j = await r.json();
|
| 33 |
-
|
|
|
|
|
|
|
| 34 |
}
|
| 35 |
|
| 36 |
-
/
|
| 37 |
app.post("/api/analyze", async (req, res) => {
|
| 38 |
const { book, chapter, verse } = req.body || {};
|
| 39 |
-
|
| 40 |
-
// <<< NAPLÓZÁS
|
| 41 |
console.log("Bemenő /api/analyze kérés:", { book, chapter, verse });
|
| 42 |
|
| 43 |
try {
|
| 44 |
const engVerse = await fetchEnglishVerse(book, chapter, verse);
|
| 45 |
-
const ref = book
|
| 46 |
|
| 47 |
-
// ... (az OpenAI rész változatlan)
|
| 48 |
const aiRes = await fetch("https://api.openai.com/v1/chat/completions", {
|
| 49 |
method: "POST",
|
| 50 |
headers: {
|
|
@@ -58,54 +92,43 @@ app.post("/api/analyze", async (req, res) => {
|
|
| 58 |
role: "system",
|
| 59 |
content: "Fordítsd magyarra a megadott Biblia‑verset, majd első szám első személyben, legfeljebb két rövid bekezdésben magyarázd el, hogyan kapcsolódik a mai mesterséges intelligencia dilemmáihoz."
|
| 60 |
},
|
| 61 |
-
{
|
| 62 |
-
role: "user",
|
| 63 |
-
content: "Verse (KJV): \"" + engVerse + "\" — " + ref
|
| 64 |
-
}
|
| 65 |
],
|
| 66 |
temperature: 0.9,
|
| 67 |
max_tokens: 300
|
| 68 |
})
|
| 69 |
});
|
|
|
|
| 70 |
if (!aiRes.ok) throw new Error("OpenAI API hiba");
|
| 71 |
const data = await aiRes.json();
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
} catch (err) {
|
| 76 |
console.error(err);
|
| 77 |
res.status(500).json({ error: err.message || "Ismeretlen hiba" });
|
| 78 |
}
|
| 79 |
});
|
| 80 |
|
| 81 |
-
/
|
| 82 |
app.get("/api/meta", async (req, res) => {
|
| 83 |
const book = req.query.book;
|
| 84 |
if (!book) return res.status(400).json({ error: "Missing book" });
|
| 85 |
|
| 86 |
try {
|
| 87 |
-
const
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
console.log("Meta lekérése ezzel az URL-lel:", url);
|
| 97 |
-
}
|
| 98 |
-
|
| 99 |
-
const r = await fetch(url);
|
| 100 |
-
if (!r.ok) break;
|
| 101 |
-
const d = await r.json();
|
| 102 |
-
const lastVerseInChapter = d.verses[d.verses.length - 1].verse;
|
| 103 |
-
chapters.push(lastVerseInChapter);
|
| 104 |
-
}
|
| 105 |
res.json({ chapters });
|
| 106 |
} catch (err) {
|
| 107 |
-
|
|
|
|
| 108 |
}
|
| 109 |
});
|
| 110 |
|
| 111 |
-
app.listen(PORT, () => console.log("🚀 Fut a", PORT, "porton"));
|
|
|
|
| 1 |
import express from "express";
|
| 2 |
import fetch from "node-fetch";
|
| 3 |
import dotenv from "dotenv";
|
| 4 |
+
import { setTimeout as delay } from "node:timers/promises";
|
| 5 |
+
|
| 6 |
dotenv.config();
|
| 7 |
|
| 8 |
const app = express();
|
|
|
|
| 16 |
app.use(express.json());
|
| 17 |
app.use(express.static("public"));
|
| 18 |
|
| 19 |
+
/* ---------- Simple token‑bucket rate‑limiter for Bible‑API (15 req / 30 s) ---------- */
|
| 20 |
+
let tokens = 15;
|
| 21 |
+
setInterval(() => {
|
| 22 |
+
tokens = 15;
|
| 23 |
+
}, 30_000); // reset every 30 s
|
| 24 |
+
|
| 25 |
+
async function limitedFetch(url, opts) {
|
| 26 |
+
// Wait until a token is available
|
| 27 |
+
while (tokens === 0) await delay(200);
|
| 28 |
+
tokens--;
|
| 29 |
+
|
| 30 |
+
const r = await fetch(url, opts);
|
| 31 |
+
|
| 32 |
+
if (r.status === 429) {
|
| 33 |
+
const retry = parseInt(r.headers.get("Retry-After") || "5", 10) * 1000;
|
| 34 |
+
console.warn("429 kaptunk, várok", retry, "ms‑t és újrapróbálom →", url);
|
| 35 |
+
await delay(retry);
|
| 36 |
+
return limitedFetch(url, opts); // single retry
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
return r;
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
/* ---------- In‑memory verse cache (5 perc) ---------- */
|
| 43 |
+
const verseCache = new Map(); // key => { text, exp }
|
| 44 |
+
|
| 45 |
+
function cacheGet(key) {
|
| 46 |
+
const v = verseCache.get(key);
|
| 47 |
+
if (v && v.exp > Date.now()) return v.text;
|
| 48 |
+
}
|
| 49 |
+
function cacheSet(key, text, ttl = 300_000) {
|
| 50 |
+
verseCache.set(key, { text, exp: Date.now() + ttl });
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
/* ---------- Fetch English KJV verse ---------- */
|
| 54 |
async function fetchEnglishVerse(book, ch, v) {
|
| 55 |
+
const ref = `${book} ${ch}:${v}`;
|
| 56 |
+
const cached = cacheGet(ref);
|
| 57 |
+
if (cached) return cached;
|
| 58 |
|
| 59 |
+
const url = "https://bible-api.com/" + encodeURIComponent(ref) + "?translation=kjv";
|
| 60 |
console.log("Vers lekérése ezzel az URL-lel:", url);
|
| 61 |
|
| 62 |
+
const r = await limitedFetch(url);
|
| 63 |
if (!r.ok) {
|
|
|
|
| 64 |
console.error("Bible API hiba státuszkód:", r.status, r.statusText);
|
| 65 |
throw new Error("bible-api hiba");
|
| 66 |
}
|
| 67 |
const j = await r.json();
|
| 68 |
+
const text = j.text.trim();
|
| 69 |
+
cacheSet(ref, text);
|
| 70 |
+
return text;
|
| 71 |
}
|
| 72 |
|
| 73 |
+
/* ---------- /api/analyze ---------- */
|
| 74 |
app.post("/api/analyze", async (req, res) => {
|
| 75 |
const { book, chapter, verse } = req.body || {};
|
|
|
|
|
|
|
| 76 |
console.log("Bemenő /api/analyze kérés:", { book, chapter, verse });
|
| 77 |
|
| 78 |
try {
|
| 79 |
const engVerse = await fetchEnglishVerse(book, chapter, verse);
|
| 80 |
+
const ref = `${book} ${chapter}:${verse}`;
|
| 81 |
|
|
|
|
| 82 |
const aiRes = await fetch("https://api.openai.com/v1/chat/completions", {
|
| 83 |
method: "POST",
|
| 84 |
headers: {
|
|
|
|
| 92 |
role: "system",
|
| 93 |
content: "Fordítsd magyarra a megadott Biblia‑verset, majd első szám első személyben, legfeljebb két rövid bekezdésben magyarázd el, hogyan kapcsolódik a mai mesterséges intelligencia dilemmáihoz."
|
| 94 |
},
|
| 95 |
+
{ role: "user", content: `Verse (KJV): "${engVerse}" — ${ref}` }
|
|
|
|
|
|
|
|
|
|
| 96 |
],
|
| 97 |
temperature: 0.9,
|
| 98 |
max_tokens: 300
|
| 99 |
})
|
| 100 |
});
|
| 101 |
+
|
| 102 |
if (!aiRes.ok) throw new Error("OpenAI API hiba");
|
| 103 |
const data = await aiRes.json();
|
| 104 |
+
res.json({ output: data.choices[0].message.content.trim() });
|
| 105 |
+
|
|
|
|
| 106 |
} catch (err) {
|
| 107 |
console.error(err);
|
| 108 |
res.status(500).json({ error: err.message || "Ismeretlen hiba" });
|
| 109 |
}
|
| 110 |
});
|
| 111 |
|
| 112 |
+
/* ---------- /api/meta – 1 fetch a teljes könyvre ---------- */
|
| 113 |
app.get("/api/meta", async (req, res) => {
|
| 114 |
const book = req.query.book;
|
| 115 |
if (!book) return res.status(400).json({ error: "Missing book" });
|
| 116 |
|
| 117 |
try {
|
| 118 |
+
const url = `https://bible-api.com/data/kjv/${book.toUpperCase()}`;
|
| 119 |
+
console.log("Meta lekérése ezzel az URL-lel:", url);
|
| 120 |
+
|
| 121 |
+
const r = await limitedFetch(url);
|
| 122 |
+
if (!r.ok) throw new Error("meta fetch error");
|
| 123 |
+
const j = await r.json();
|
| 124 |
+
|
| 125 |
+
// Expecting j.chapters = [{verses_count: n}, ...]
|
| 126 |
+
const chapters = (j.chapters || []).map(c => c.verses_count);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
res.json({ chapters });
|
| 128 |
} catch (err) {
|
| 129 |
+
console.error(err);
|
| 130 |
+
res.status(500).json({ error: err.message });
|
| 131 |
}
|
| 132 |
});
|
| 133 |
|
| 134 |
+
app.listen(PORT, () => console.log("🚀 Fut a", PORT, "porton"));
|