-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
570 lines (504 loc) · 16.3 KB
/
server.js
File metadata and controls
570 lines (504 loc) · 16.3 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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
"use strict";
/**
* Bible Vision - Read-only JSON API
* ------------------------------------------------------------
* Goals:
* - Only read from world_english_bible/ (no writes, no POST)
* - Synchronous, dependency-free, JSON-only
* - Simple endpoints your logic.js can build on
*
* Directory expectation:
* PROJECT_ROOT/world_english_bible/*.txt (e.g., 002_GEN_01.txt)
*
* Start:
* node server.js --port 4101
* (env PORT also supported)
*
* Endpoints (all GET, all JSON):
* 1) /api/health
* 2) /api/books
* 3) /api/book/chapters?book=GEN
* 4) /api/book/meta?book=GEN
* 5) /api/chapter?book=GEN&chapter=1
* 6) /api/chapter/verses?book=GEN&chapter=1
* 7) /api/verse/random?seed=&book=GEN&chapter=1
* 8) /api/search?q=light&book=GEN&limit=25
* 9) /api/files
* 10) /api/stats
* 11) /api/range?book=GEN&from=1&to=3
* 12) /api/verse?book=GEN&chapter=1&verse=3
*/
const http = require("http");
const url = require("url");
const fs = require("fs");
const path = require("path");
/* =============================== config =============================== */
function isDir(p) {
try {
return fs.statSync(p).isDirectory();
} catch {
return false;
}
}
function findProjectRoot(startDir) {
// climb up looking for world_english_bible/ to match your project layout
let dir = startDir;
for (let i = 0; i < 10; i++) {
const hasWeb = isDir(path.join(dir, "world_english_bible"));
if (hasWeb) return dir;
const parent = path.dirname(dir);
if (parent === dir) break;
dir = parent;
}
// fallback: if not found, just use startDir
return startDir;
}
const APP_DIR = process.cwd();
const PROJECT_ROOT = findProjectRoot(APP_DIR);
const PUBLIC_DIR = path.join(APP_DIR, "public");
const BIBLE_DIR = path.join(PROJECT_ROOT, "world_english_bible");
const PORT = (() => {
const i = process.argv.indexOf("--port");
if (i >= 0 && process.argv[i + 1]) return Number(process.argv[i + 1]);
if (process.env.PORT) return Number(process.env.PORT);
return 4101;
})();
/* =============================== helpers ============================== */
const MIME = {
".html": "text/html; charset=utf-8",
".js": "application/javascript; charset=utf-8",
".css": "text/css; charset=utf-8",
".ico": "image/x-icon",
".png": "image/png",
".jpg": "image/jpeg",
".svg": "image/svg+xml",
".json": "application/json; charset=utf-8",
};
function sendJSON(res, code, obj) {
const body = JSON.stringify(obj, null, 2);
res.writeHead(code, { "Content-Type": MIME[".json"], "Cache-Control": "no-store" });
res.end(body);
}
function send404(res) {
res.writeHead(404, { "Content-Type": "text/plain; charset=utf-8" });
res.end("Not found");
}
function serveStatic(res, requestPath) {
const rel = requestPath === "/" ? "/index.html" : requestPath;
const safeRel = String(rel).replace(/^\/+/, "");
const abs = path.resolve(path.join(PUBLIC_DIR, safeRel));
const root = path.resolve(PUBLIC_DIR) + path.sep;
if (!abs.startsWith(root)) return send404(res);
try {
const st = fs.statSync(abs);
if (!st.isFile()) return send404(res);
const ext = path.extname(abs).toLowerCase();
res.writeHead(200, {
"Content-Type": MIME[ext] || "application/octet-stream",
"Cache-Control": "no-cache",
});
res.end(fs.readFileSync(abs));
} catch {
send404(res);
}
}
/* ============================ Bible utilities =========================== */
// File pattern: 015_PSA_144.txt or 015_2CH_07.txt
const FILE_RE = /^(\d{3})_((?:[1-3][A-Z]{2}|[A-Z]{3}))_(\d{2,3})\.txt$/;
function listChapterFiles() {
try {
return fs
.readdirSync(BIBLE_DIR)
.filter((f) => FILE_RE.test(f))
.sort();
} catch {
return [];
}
}
function parseChapterFile(filename) {
const m = FILE_RE.exec(filename);
if (!m) return null;
const book = m[2]; // e.g., GEN
const chapter = String(Number(m[3])); // "01" -> "1"
const full = path.join(BIBLE_DIR, filename);
let content = "";
try {
content = fs.readFileSync(full, "utf8");
} catch {
return null;
}
const verses = content.split(/\r?\n/).slice(2);
return { book, chapter: Number(chapter), verses, sourceFile: filename };
}
function pickRandom(arr) {
if (!arr || !arr.length) return { index: -1, item: null };
const index = Math.floor(Math.random() * arr.length);
return { index, item: arr[index] };
}
function indexBooks() {
// { GEN: { chapters: [1,2,...], files: ["002_GEN_01.txt", ...] }, ... }
const idx = {};
const files = listChapterFiles();
for (const f of files) {
const m = FILE_RE.exec(f);
if (!m) continue;
const book = m[2];
const chap = Number(m[3]);
if (!idx[book]) idx[book] = { chapters: [], files: [] };
idx[book].files.push(f);
idx[book].chapters.push(chap);
}
// normalize chapter arrays to sorted unique numbers
for (const b in idx) {
const uniq = Array.from(new Set(idx[b].chapters)).sort((a, b) => a - b);
idx[b].chapters = uniq;
}
return idx;
}
function countVersesInFile(filename) {
const parsed = parseChapterFile(filename);
return parsed ? parsed.verses.length : 0;
}
/* ================================ API ================================== */
/**
* 1) /api/health
* Summary about availability and counts.
*/
function apiHealth(res) {
const okBible = isDir(BIBLE_DIR);
const files = okBible ? listChapterFiles().length : 0;
const ok = okBible && files > 0;
sendJSON(res, ok ? 200 : 500, {
ok,
app: "bible-vision",
appDir: APP_DIR,
projectRoot: PROJECT_ROOT,
bibleDir: BIBLE_DIR,
chapterFiles: files,
});
}
/**
* 2) /api/books
* List available book codes with chapter counts.
*/
function apiBooks(res) {
const idx = indexBooks();
const items = Object.keys(idx)
.sort()
.map((code) => ({
book: code,
chapters: idx[code].chapters.length,
}));
sendJSON(res, 200, { ok: true, items });
}
/**
* 3) /api/book/chapters?book=GEN
* List chapter numbers for a book.
*/
function apiBookChapters(res, q) {
const book = q.book ? String(q.book).toUpperCase() : "";
const idx = indexBooks();
const entry = idx[book];
if (!entry) return sendJSON(res, 404, { ok: false, error: "book not found" });
sendJSON(res, 200, { ok: true, book, chapters: entry.chapters });
}
/**
* 4) /api/book/meta?book=GEN
* Per-book stats: chapters, total verses, first/last chapter.
*/
function apiBookMeta(res, q) {
const book = q.book ? String(q.book).toUpperCase() : "";
const idx = indexBooks();
const entry = idx[book];
if (!entry) return sendJSON(res, 404, { ok: false, error: "book not found" });
let totalVerses = 0;
for (const f of entry.files) totalVerses += countVersesInFile(f);
sendJSON(res, 200, {
ok: true,
book,
chapters: entry.chapters.length,
totalVerses,
});
}
/**
* 5) /api/chapter?book=GEN&chapter=1
* Full chapter payload with verses.
*/
function apiChapter(res, q) {
const book = q.book ? String(q.book).toUpperCase() : "";
const chap = Number(q.chapter);
if (!book || !Number.isInteger(chap) || chap <= 0) {
return sendJSON(res, 400, {
ok: false,
error: "book and positive integer chapter are required",
});
}
const chapStr = String(chap).padStart(2, "0");
const files = listChapterFiles().filter(
(f) => f.includes("_" + book + "_") && f.endsWith("_" + chapStr + ".txt")
);
if (!files.length) return sendJSON(res, 404, { ok: false, error: "chapter not found" });
const parsed = parseChapterFile(files[0]);
if (!parsed) return sendJSON(res, 500, { ok: false, error: "parse failure" });
sendJSON(res, 200, {
ok: true,
book: parsed.book,
chapter: parsed.chapter,
verses: parsed.verses,
sourceFile: parsed.sourceFile,
});
}
/**
* 6) /api/chapter/verses?book=GEN&chapter=1
* Verse metadata only (numbers and lengths).
*/
function apiChapterVerses(res, q) {
const book = q.book ? String(q.book).toUpperCase() : "";
const chap = Number(q.chapter);
if (!book || !Number.isInteger(chap) || chap <= 0) {
return sendJSON(res, 400, {
ok: false,
error: "book and positive integer chapter are required",
});
}
const chapStr = String(chap).padStart(2, "0");
const files = listChapterFiles().filter(
(f) => f.includes("_" + book + "_") && f.endsWith("_" + chapStr + ".txt")
);
if (!files.length) return sendJSON(res, 404, { ok: false, error: "chapter not found" });
const parsed = parseChapterFile(files[0]);
if (!parsed) return sendJSON(res, 500, { ok: false, error: "parse failure" });
const verses = parsed.verses.map((v) => ({
verse: v,
words: v.trim().split(/\s+/).length,
chars: v.length,
}));
sendJSON(res, 200, {
ok: true,
book: parsed.book,
chapter: parsed.chapter,
verseCount: parsed.verses.length,
verses,
sourceFile: parsed.sourceFile,
});
}
/**
* 7) /api/verse/random?seed=&book=GEN&chapter=1
* Random verse globally, or limited to a book or chapter.
*/
function apiVerseRandom(res, q) {
if (!isDir(BIBLE_DIR))
return sendJSON(res, 500, { ok: false, error: "world_english_bible not found" });
const filesAll = listChapterFiles();
if (!filesAll.length) return sendJSON(res, 500, { ok: false, error: "no chapter files found" });
const book = q.book ? String(q.book).toUpperCase() : null;
const chap = q.chapter ? String(Number(q.chapter)).padStart(2, "0") : null;
let candidates = filesAll;
if (book) candidates = candidates.filter((f) => f.includes("_" + book + "_"));
if (book && chap) candidates = candidates.filter((f) => f.endsWith("_" + chap + ".txt"));
if (!candidates.length) candidates = filesAll;
const { item: file } = pickRandom(candidates);
const parsed = file && parseChapterFile(file);
if (!parsed || !parsed.verses.length)
return sendJSON(res, 500, { ok: false, error: "parse failure", file });
const { index, item: verse } = pickRandom(parsed.verses);
sendJSON(res, 200, {
ok: true,
book: parsed.book,
chapter: parsed.chapter,
verse: index + 1,
text: verse,
sourceFile: parsed.sourceFile,
});
}
/**
* 8) /api/search?q=light&book=GEN&limit=25
* Very simple case-insensitive substring search over chapter files.
*/
function apiSearch(res, q) {
const term = (q.q || "").toString().trim();
if (!term || term.length < 2)
return sendJSON(res, 400, { ok: false, error: "q must be at least 2 characters" });
const book = q.book ? String(q.book).toUpperCase() : null;
const limit = Number(q.limit || 25);
let files = listChapterFiles();
if (book) files = files.filter((f) => f.includes("_" + book + "_"));
const needle = term.toLowerCase();
const hits = [];
for (const f of files) {
const parsed = parseChapterFile(f);
if (!parsed) continue;
for (const [idx, ver] of parsed.verses.entries()) {
if (ver.toLowerCase().indexOf(needle) !== -1) {
hits.push({
book: parsed.book,
chapter: parsed.chapter,
verse: idx + 1,
text: ver,
sourceFile: parsed.sourceFile,
});
if (Number.isInteger(limit) && limit > 0 && hits.length >= limit) break;
}
}
if (Number.isInteger(limit) && limit > 0 && hits.length >= limit) break;
}
sendJSON(res, 200, { ok: true, q: term, count: hits.length, items: hits });
}
/**
* 9) /api/files
* Raw file listing with sizes.
*/
function apiFiles(res) {
try {
const names = listChapterFiles();
const items = names.map((f) => {
const st = fs.statSync(path.join(BIBLE_DIR, f));
return { file: f, bytes: st.size };
});
sendJSON(res, 200, { ok: true, dir: BIBLE_DIR, items, count: items.length });
} catch {
sendJSON(res, 500, { ok: false, error: "failed to stat files" });
}
}
/**
* 10) /api/stats
* Global counts: books, chapters, verses.
*/
function apiStats(res) {
const idx = indexBooks();
const books = Object.keys(idx).length;
let chapters = 0;
let verses = 0;
for (const b in idx) {
chapters += idx[b].chapters.length;
for (const f of idx[b].files) verses += countVersesInFile(f);
}
sendJSON(res, 200, { ok: true, books, chapters, verses });
}
/**
* 11) /api/range?book=GEN&from=1&to=3
* Chapters range summary for a book.
*/
function apiRange(res, q) {
const book = q.book ? String(q.book).toUpperCase() : "";
const from = Number(q.from);
const to = Number(q.to);
if (!book || !Number.isInteger(from) || !Number.isInteger(to) || from <= 0 || to < from) {
return sendJSON(res, 400, {
ok: false,
error: "book, from, to must be positive integers (to >= from)",
});
}
const out = [];
for (let ch = from; ch <= to; ch++) {
const chapStr = String(ch).padStart(2, "0");
const files = listChapterFiles().filter(
(f) => f.includes("_" + book + "_") && f.endsWith("_" + chapStr + ".txt")
);
if (!files.length) continue;
const parsed = parseChapterFile(files[0]);
if (!parsed) continue;
out.push({
book: parsed.book,
chapter: parsed.chapter,
verseCount: parsed.verses.length,
preview: parsed.verses[0] ? parsed.verses[0].slice(0, 90) : "",
sourceFile: parsed.sourceFile,
});
}
if (!out.length) return sendJSON(res, 404, { ok: false, error: "no chapters in range" });
sendJSON(res, 200, { ok: true, book, from, to, chapters: out });
}
/**
* 12) /api/verse?book=GEN&chapter=1&verse=3
* Returns a single verse, or a helpful error.
*/
function apiVerse(res, q) {
const book = q.book ? String(q.book).toUpperCase() : "";
const chapter = Number(q.chapter);
const verse = Number(q.verse);
if (
!book ||
!Number.isInteger(chapter) ||
chapter <= 0 ||
!Number.isInteger(verse) ||
verse <= 0
) {
return sendJSON(res, 400, {
ok: false,
error: "book, chapter, verse are required (positive integers for chapter and verse)",
example: "/api/verse?book=GEN&chapter=1&verse=3",
});
}
const chapStr = String(chapter).padStart(2, "0");
const files = listChapterFiles().filter(
(f) => f.includes("_" + book + "_") && f.endsWith("_" + chapStr + ".txt")
);
if (!files.length) {
const idx = indexBooks();
if (!idx[book]) {
return sendJSON(res, 404, { ok: false, error: "book not found", book });
}
return sendJSON(res, 404, {
ok: false,
error: "chapter not found for book",
book,
requestedChapter: chapter,
availableChapters: idx[book].chapters,
});
}
const parsed = parseChapterFile(files[0]);
if (!parsed) return sendJSON(res, 500, { ok: false, error: "parse failure", file: files[0] });
const maxVerse = parsed.verses.length;
if (verse > maxVerse) {
return sendJSON(res, 404, {
ok: false,
error: "verse not found in chapter",
book: parsed.book,
chapter: parsed.chapter,
requestedVerse: verse,
maxVerse,
});
}
const text = parsed.verses[verse - 1];
return sendJSON(res, 200, {
ok: true,
book: parsed.book,
chapter: parsed.chapter,
verse,
text,
sourceFile: parsed.sourceFile,
});
}
/* ============================== HTTP router ============================= */
const server = http.createServer((req, res) => {
try {
const u = url.parse(req.url, true);
const p = u.pathname || "/";
// JSON API
if (p === "/api/health") return apiHealth(res);
if (p === "/api/books") return apiBooks(res);
if (p === "/api/book/chapters") return apiBookChapters(res, u.query);
if (p === "/api/book/meta") return apiBookMeta(res, u.query);
if (p === "/api/chapter") return apiChapter(res, u.query);
if (p === "/api/chapter/verses") return apiChapterVerses(res, u.query);
if (p === "/api/verse/random") return apiVerseRandom(res, u.query);
if (p === "/api/search") return apiSearch(res, u.query);
if (p === "/api/files") return apiFiles(res);
if (p === "/api/stats") return apiStats(res);
if (p === "/api/range") return apiRange(res, u.query);
if (p === "/api/verse") return apiVerse(res, u.query);
// Static for index.html, logic.js, style.css
return serveStatic(res, p);
} catch (e) {
sendJSON(res, 500, { ok: false, error: String((e && e.message) || e) });
}
});
/* ================================ start ================================= */
server.listen(PORT, () => {
console.log("Bible Vision server listening on http://localhost:" + PORT);
console.log("Project root: " + PROJECT_ROOT);
console.log("Bible dir: " + BIBLE_DIR);
const files = listChapterFiles();
console.log("Chapter files found: " + files.length);
});