-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
902 lines (766 loc) · 27.6 KB
/
app.js
File metadata and controls
902 lines (766 loc) · 27.6 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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
// app.js - FINAL VERSION (FIXED)
require('dotenv').config({
debug: false, quiet: true
});
const express = require('express');
const mongoose = require('mongoose');
const path = require('path');
const Manga = require('./models/Manga');
const Chapter = require('./models/Chapter');
// IMPORT RUTE API (PENTING)
const apiRoutes = require('./routes/api');
const app = express();
app.use((req, res, next) => {
const userAgent = req.headers['user-agent'] || '';
if (/meta-externalagent|facebookexternalhit|facebot/i.test(userAgent)) {
return res.status(403).send('403 Forbidden');
}
next();
});
const PORT = process.env.PORT || 3000;
const WEBSITE_URL = process.env.SITE_URL || `http://localhost:${PORT}`;
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
app.use(express.static('public'));
app.use((req, res, next) => {
res.locals.siteName = process.env.SITE_NAME || 'DoujinShi';
res.locals.siteUrl = process.env.SITE_URL || `${req.protocol}://${req.get('host')}`;
res.locals.currentUrl = req.path;
next();
});
// ==========================================
// SISTEM CACHE SEDERHANA (In-Memory)
// ==========================================
const cacheStore = new Map();
// Garbage Collection: Bersihkan cache expired setiap 5 menit
setInterval(() => {
const now = Date.now();
for (const [key, value] of cacheStore.entries()) {
if (now > value.expiry) cacheStore.delete(key);
}
}, 5 * 60 * 1000);
const simpleCache = (durationInSeconds) => {
return (req, res, next) => {
// Hanya cache method GET
if (req.method !== 'GET') return next();
// Key unik berdasarkan URL lengkap
const key = `__cache__${req.originalUrl || req.url}`;
const cachedBody = cacheStore.get(key);
// Cek apakah data ada di cache dan belum expired
if (cachedBody && Date.now() < cachedBody.expiry) {
return res.send(cachedBody.html);
}
// Intercept res.send untuk menyimpan output ke cache
const originalSend = res.send;
res.send = (body) => {
originalSend.call(res, body);
cacheStore.set(key, {
html: body,
expiry: Date.now() + (durationInSeconds * 1000)
});
};
next();
};
};
// ==========================================
// HELPER FUNCTION: Hitung Chapter
// ==========================================
async function attachChapterCounts(mangas) {
return await Promise.all(mangas.map(async (m) => {
const count = await Chapter.countDocuments({
manga_id: m._id
});
const mObj = m.toObject ? m.toObject(): m;
mObj.chapter_count = count;
return mObj;
}));
}
// ==========================================
// HELPER: Ambil 1 Chapter Terbaru (SUPER RINGAN)
// Menggunakan Aggregation (1 Query untuk banyak Manga)
// ==========================================
async function attachLatestChapter(mangas) {
if (!mangas || mangas.length === 0) return [];
// 1. Kumpulkan semua ID Manga dalam array
const mangaIds = mangas.map(m => m._id);
// 2. Lakukan 1 Query Aggregation untuk mencari chapter terbaru dari semua ID tersebut
const latestChapters = await mongoose.model('Chapter').aggregate([
{
$match: {
manga_id: { $in: mangaIds } // Cari chapter yang manga_id nya ada di list
}
},
{ $sort: { chapter_index: -1 } }, // Urutkan chapter dari besar ke kecil
{
$group: {
_id: "$manga_id", // Kelompokkan per Manga
latest: { $first: "$$ROOT" } // Ambil chapter pertama (teratas/terbaru) setelah disortir
}
},
{
$project: { // Hanya ambil field yang penting (Hemat Bandwidth)
_id: 1,
"latest.chapter_index": 1,
"latest.slug": 1,
"latest.createdAt": 1
}
}
]);
// 3. Buat Map (Kamus) untuk pencarian cepat di memori
const chapterMap = new Map();
latestChapters.forEach(item => {
chapterMap.set(item._id.toString(), item.latest);
});
// 4. Tempelkan data chapter ke object manga
return mangas.map(m => {
// Pastikan bentuknya object biasa (karena kita akan pakai .lean() di route)
const mObj = m.toObject ? m.toObject() : m;
// Ambil data dari Map, jika tidak ada set null
mObj.latestChapter = chapterMap.get(mObj._id.toString()) || null;
return mObj;
});
}
// ==========================================
// 2. MAIN ROUTES (DENGAN CACHE)
// ==========================================
// HOME PAGE - Cache 3 Menit (180 detik)
app.get('/', simpleCache(180), async (req, res) => {
try {
const limit = 24;
const page = parseInt(req.query.page) || 1;
const skip = (page - 1) * limit;
// 1. Recents (Update Terbaru)
let recents = await Manga.find()
.select('title slug thumb metadata tags updatedAt')
.sort({ updatedAt: -1 })
.skip(skip)
.limit(limit)
.lean(); // [OPTIMASI] Gunakan lean() agar ringan
recents = await attachLatestChapter(recents);
// 2. Trending (Sedang Hangat)
// Trending biasanya tidak butuh chapter di Home, jadi tidak perlu attachLatestChapter (biar makin cepat)
let trending = await Manga.find()
.select('title slug thumb metadata views')
.sort({ views: -1 })
.limit(10)
.lean(); // [OPTIMASI] Gunakan lean()
// 3. Manhwa
let manhwas = await Manga.find({ 'metadata.type': { $regex: 'manhwa', $options: 'i' } })
.select('title slug thumb metadata')
.sort({ updatedAt: -1 })
.limit(24)
.lean(); // [OPTIMASI] Gunakan lean()
manhwas = await attachLatestChapter(manhwas);
// 4. Doujinshi
let doujinshis = await Manga.find({ 'metadata.type': { $regex: 'doujinshi', $options: 'i' } })
.select('title slug thumb metadata')
.sort({ updatedAt: -1 })
.limit(24)
.lean(); // [OPTIMASI] Gunakan lean()
doujinshis = await attachLatestChapter(doujinshis);
res.render('landing', {
mangas: recents,
trending: trending,
manhwas: manhwas,
doujinshis: doujinshis,
title: `${res.locals.siteName} - Baca Manga & Doujinshi Bahasa Indonesia`,
desc: `${res.locals.siteName} Baca komik dewasa terbaru, manhwa 18+, manga, dan webtoon bahasa Indonesia gratis. Update harian dengan koleksi terlengkap di ${res.locals.siteName}.`
});
} catch (err) {
console.error(err);
res.status(500).send(err.message);
}
});
// DETAIL PAGE - Cache 3 Menit
app.get('/manga/:slug', simpleCache(180), async (req, res) => {
try {
// Update Views
const manga = await Manga.findOneAndUpdate(
{ slug: req.params.slug },
{ $inc: { views: 1 } },
{ new: true, timestamps: false }
);
// --- PERBAIKAN DI SINI ---
// Tambahkan object { title: '...', desc: '...' } agar layout_head tidak error
if (!manga) {
return res.status(404).render('404', {
title: '404 - Manga Tidak Ditemukan',
desc: 'Maaf, manga yang Anda cari tidak dapat ditemukan.'
});
}
// -------------------------
// Ambil Chapters
let chapters = await Chapter.find({
manga_id: manga._id
}).lean();
// Sorting Descending (Chapter Baru di Atas)
chapters.sort((b, a) => {
const numB = parseFloat(String(b.chapter_index).replace(/[^0-9.]/g, '')) || 0;
const numA = parseFloat(String(a.chapter_index).replace(/[^0-9.]/g, '')) || 0;
return numB - numA;
});
// Rekomendasi Manga Lain
const recommendations = await Manga.aggregate([
{ $match: { _id: { $ne: manga._id } } },
{ $sample: { size: 12 } },
{ $project: { title: 1, slug: 1, thumb: 1, metadata: 1 } }
]);
// SEO Data
const siteName = res.locals.siteName;
const type = manga.metadata && manga.metadata.type ? manga.metadata.type : 'Komik';
const seoDesc = `Baca ${type} ${manga.title} bahasa Indonesia lengkap di ${siteName}. ${manga.synopsis || ''}`;
res.render('detail', {
manga,
chapters,
recommendations,
title: `${manga.title} Bahasa Indonesia - ${res.locals.siteName}`,
desc: seoDesc,
ogType: 'article',
image: manga.thumb
});
} catch (err) {
console.error(err);
res.status(500).send(err.message);
}
});
// MANGA LIST (A-Z) - Cache 5 Menit (300 detik)
app.get('/manga-list', simpleCache(300), async (req, res) => {
try {
const limit = 24;
const page = parseInt(req.query.page) || 1;
const skip = (page - 1) * limit;
// --- FILTER QUERY LOGIC ---
let query = {};
const { q, status, type, orderby } = req.query;
const genreParam = req.query['genre[]'] || req.query.genre; // Handle array or string
// 1. Search
if (q) {
query.title = { $regex: q, $options: 'i' };
}
// 2. Filter Status
if (status && status !== 'all') {
query['metadata.status'] = { $regex: status, $options: 'i' };
}
// 3. Filter Type (Menangani data string atau object warisan)
if (type && type !== 'all') {
query.$or = [
{ 'metadata.type': { $regex: type, $options: 'i' } },
{ 'metadata.type.type': { $regex: type, $options: 'i' } } // Handle legacy object structure
];
}
// 4. Filter Genre
if (genreParam) {
const genres = Array.isArray(genreParam) ? genreParam : [genreParam];
if (genres.length > 0) {
// $all artinya manga harus punya SEMUA genre yang dipilih
query.tags = { $all: genres.map(g => new RegExp(g, 'i')) };
}
}
// --- SORTING LOGIC ---
let sort = { title: 1 }; // Default A-Z
if (orderby === 'titledesc') sort = { title: -1 };
if (orderby === 'update') sort = { updatedAt: -1 };
if (orderby === 'popular') sort = { views: -1 };
// --- FETCH DATA UTAMA ---
const totalManga = await Manga.countDocuments(query);
const totalPages = Math.ceil(totalManga / limit);
let mangas = await Manga.find(query)
.select('title slug thumb metadata tags views updatedAt') // Select field penting saja
.sort(sort)
.skip(skip)
.limit(limit);
mangas = await attachChapterCounts(mangas);
// --- [BARU] AMBIL DATA OPSI FILTER DARI DB ---
// Mengambil daftar unik dari database agar dropdown sesuai isi DB
const [dbGenres, dbStatuses, dbTypes] = await Promise.all([
Manga.distinct('tags'),
Manga.distinct('metadata.status'),
Manga.distinct('metadata.type')
]);
// Bersihkan data (hapus null/kosong dan urutkan)
const genreList = dbGenres.filter(g => g).sort();
const statusList = dbStatuses.filter(s => s).sort();
// Normalisasi Type (karena ada kemungkinan data lama berbentuk object)
const typeSet = new Set();
dbTypes.forEach(t => {
if (typeof t === 'string') typeSet.add(t);
else if (t && t.type) typeSet.add(t.type); // Jika tersimpan sebagai object
});
const typeList = Array.from(typeSet).sort();
res.render('manga_list', {
mangas,
currentPage: page,
totalPages: totalPages,
title: `Daftar Semua Manga - Halaman ${page}`,
desc: `Daftar lengkap Manga diurutkan dari A-Z.`,
// Kirim variable filter ke EJS
queryParams: req.query,
genreList,
statusList,
typeList
});
} catch (err) {
console.error(err);
res.status(500).send(err.message);
}
});
// READ CHAPTER - Cache 10 Menit (600 detik)
app.get('/read/:slug/:chapterSlug', simpleCache(600), async (req, res) => {
try {
const siteName = process.env.SITE_NAME || 'Doujinshi';
const manga = await Manga.findOne({
slug: req.params.slug
}).lean();
if (!manga) return res.status(404).send('Manga not found');
const chapter = await Chapter.findOne({
manga_id: manga._id, slug: req.params.chapterSlug
});
if (!chapter) return res.status(404).send('Chapter not found');
const [allChapters, nextChap, prevChap] = await Promise.all([
// 1. Ambil Semua Chapter untuk Sidebar/List
Chapter.find({
manga_id: manga._id
})
.select('title slug date chapter_index')
.sort({
chapter_index: -1
}),
// 2. LOGIKA NEXT (Chapter Selanjutnya / Angka Lebih Besar)
// Contoh: Sekarang Ch 10, Next adalah Ch 11 ($gt: 10, sort asc)
Chapter.findOne({
manga_id: manga._id,
chapter_index: {
$gt: chapter.chapter_index // UBAH DISINI: Gunakan $gt (Greater Than)
}
}).sort({
chapter_index: 1 // Sort Ascending (11, 12, 13...) ambil yang pertama
}),
// 3. LOGIKA PREV (Chapter Sebelumnya / Angka Lebih Kecil)
// Contoh: Sekarang Ch 10, Prev adalah Ch 9 ($lt: 10, sort desc)
Chapter.findOne({
manga_id: manga._id,
chapter_index: {
$lt: chapter.chapter_index // UBAH DISINI: Gunakan $lt (Less Than)
}
}).sort({
chapter_index: -1 // Sort Descending (9, 8, 7...) ambil yang pertama
})
]);
manga.chapters = allChapters;
res.render('read', {
manga,
chapter,
nextChap: nextChap,
prevChap: prevChap,
siteName,
title: `${manga.title} - Chapter ${chapter.title}`,
desc: `Baca manga ${manga.title} Chapter ${chapter.title} bahasa Indonesia terbaru di ${siteName}. Manga ${manga.title} bahasa Indonesia selalu update di ${siteName}. Jangan lupa membaca update manga lainnya ya. Daftar koleksi manga ${siteName} ada di menu Daftar Manga.`,
ogType: 'article',
image: manga.thumb
});
} catch (err) {
console.error("Error Read Chapter:", err);
res.status(500).send("Terjadi kesalahan pada server.");
}
});
// ==========================================
// 3. SEARCH & FILTER ROUTES (DENGAN CACHE)
// ==========================================
// SEARCH - Cache 2 Menit (120 detik)
app.get('/search', simpleCache(120), async (req, res) => {
try {
const keyword = req.query.q;
if (!keyword) return res.redirect('/');
const limit = 24;
const page = parseInt(req.query.page) || 1;
const skip = (page - 1) * limit;
const query = {
title: {
$regex: keyword,
$options: 'i'
}
};
const totalManga = await Manga.countDocuments(query);
const totalPages = Math.ceil(totalManga / limit);
let mangas = await Manga.find(query).limit(limit).skip(skip);
mangas = await attachChapterCounts(mangas);
res.render('archive', {
mangas,
pageTitle: `Hasil Pencarian: "${keyword}"`,
title: `Cari ${keyword}`,
desc: `Hasil pencarian ${keyword}`,
currentPage: page,
totalPages: totalPages,
paginationBaseUrl: `/search?q=${keyword}&`
});
} catch (err) {
res.status(500).send(err.message);
}
});
// GENRES - Cache 1 Jam (3600 detik)
app.get('/genres', simpleCache(3600), async (req, res) => {
try {
const genres = await Manga.aggregate([{
$unwind: "$tags"
},
{
$group: {
_id: "$tags", count: {
$sum: 1
}
}
},
{
$sort: {
_id: 1
}
}]);
res.render('genres', {
genres, title: 'Daftar Genre', desc: 'Daftar genre komik.'
});
} catch (err) {
res.status(500).send(err.message);
}
});
// FILTER GENRE - Cache 5 Menit
app.get('/genre/:tag', simpleCache(300), async (req, res) => {
try {
const rawTag = req.params.tag;
const limit = 24;
const page = parseInt(req.query.page) || 1;
const skip = (page - 1) * limit;
const parts = rawTag.split('-').map(part => part.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'));
const regexPattern = parts.join('[- ]');
const query = {
tags: {
$regex: new RegExp(regexPattern, 'i')
}
};
const totalManga = await Manga.countDocuments(query);
const totalPages = Math.ceil(totalManga / limit);
let mangas = await Manga.find(query).limit(limit).skip(skip);
mangas = await attachChapterCounts(mangas);
const displayTitle = rawTag.replace(/-/g, ' ').toUpperCase();
// --- LOGIKA CANONICAL ---
// Ambil URL dasar dari res.locals.siteUrl atau process.env
const baseUrl = res.locals.siteUrl || process.env.SITE_URL || `http://localhost:${process.env.PORT || 3000}`;
let canonicalUrl = `${baseUrl}/genre/${rawTag}`;
if (page > 1) canonicalUrl += `?page=${page}`;
res.render('archive', {
mangas,
pageTitle: `Genre: ${displayTitle}`,
title: `Genre ${displayTitle} ${page > 1 ? '- Page ' + page : ''}`, // Tambah info page di title tab
desc: `Daftar manga, manhwa, dan doujinshi dengan genre ${displayTitle}`,
currentPage: page,
totalPages: totalPages,
paginationBaseUrl: `/genre/${rawTag}?`,
canonicalUrl: canonicalUrl // <--- Kirim variable ini ke EJS
});
} catch (err) {
res.status(500).send(err.message);
}
});
// FILTER TYPE - Cache 5 Menit
app.get('/type/:type', simpleCache(300), async (req, res) => {
try {
const typeParam = req.params.type;
const limit = 24;
const page = parseInt(req.query.page) || 1;
const skip = (page - 1) * limit;
const sort = { updatedAt: -1 };
const query = {
'metadata.type': {
$regex: `^${typeParam}$`,
$options: 'i'
}
};
const totalManga = await Manga.countDocuments(query);
const totalPages = Math.ceil(totalManga / limit);
let mangas = await Manga.find(query)
.sort(sort)
.limit(limit)
.skip(skip);
mangas = await attachChapterCounts(mangas);
// --- LOGIKA CANONICAL ---
const baseUrl = res.locals.siteUrl || process.env.SITE_URL || `http://localhost:${process.env.PORT || 3000}`;
let canonicalUrl = `${baseUrl}/type/${typeParam}`;
if (page > 1) canonicalUrl += `?page=${page}`;
res.render('archive', {
mangas,
pageTitle: `${typeParam.toUpperCase()}`,
title: `Type ${typeParam} ${page > 1 ? '- Page ' + page : ''}`,
desc: `Daftar manga, manhwa, dan doujinshi dengan type ${typeParam}`,
currentPage: page,
totalPages: totalPages,
paginationBaseUrl: `/type/${typeParam}?`,
canonicalUrl: canonicalUrl // <--- Kirim variable ini ke EJS
});
} catch (err) {
res.status(500).send(err.message);
}
});
// FILTER STATUS - Cache 5 Menit
app.get('/status/:status', simpleCache(300), async (req, res) => {
try {
const statusParam = req.params.status;
const limit = 24;
const page = parseInt(req.query.page) || 1;
const skip = (page - 1) * limit;
const query = {
'metadata.status': {
$regex: `^${statusParam}$`,
$options: 'i'
}
};
const totalManga = await Manga.countDocuments(query);
const totalPages = Math.ceil(totalManga / limit);
let mangas = await Manga.find(query).limit(limit).skip(skip);
mangas = await attachChapterCounts(mangas);
// --- LOGIKA CANONICAL ---
const baseUrl = res.locals.siteUrl || process.env.SITE_URL || `http://localhost:${process.env.PORT || 3000}`;
let canonicalUrl = `${baseUrl}/status/${statusParam}`;
if (page > 1) canonicalUrl += `?page=${page}`;
res.render('archive', {
mangas,
pageTitle: `Status: ${statusParam.toUpperCase()}`,
title: `Status ${statusParam} ${page > 1 ? '- Page ' + page : ''}`,
desc: `Daftar manga, manhwa, dan doujinshi dengan status ${statusParam}`,
currentPage: page,
totalPages: totalPages,
paginationBaseUrl: `/status/${statusParam}?`,
canonicalUrl: canonicalUrl // <--- Kirim variable ini ke EJS
});
} catch (err) {
res.status(500).send(err.message);
}
});
// ==========================================
// 4. SEO ROUTES (NO CHAPTERS - LIGHTWEIGHT)
// ==========================================
const SITEMAP_LIMIT = 1000; // Batas URL per file sitemap
// Helper: Escape XML Characters
function escapeXml(unsafe) {
if (!unsafe) return "";
return String(unsafe).replace(/[<>&'"]/g, function (c) {
switch (c) {
case '<': return '<';
case '>': return '>';
case '&': return '&';
case '\'': return ''';
case '"': return '"';
}
});
}
// A. Route untuk File Stylesheet (XSL)
app.get('/main-sitemap.xsl', (req, res) => {
res.set('Content-Type', 'text/xsl');
res.sendFile(path.join(__dirname, 'main-sitemap.xsl'));
});
// B. ROBOTS.TXT
app.get('/robots.txt', (req, res) => {
const baseUrl = process.env.SITE_URL || `https://${req.get('host')}`;
res.type('text/plain');
res.send(
`User-agent: *
Allow: /
Disallow: /api/
Disallow: /search
Sitemap: ${baseUrl}/sitemap_index.xml`
);
});
// Redirect sitemap lama
app.get('/sitemap.xml', (req, res) => res.redirect(301, '/sitemap_index.xml'));
// C. SITEMAP INDEX (Hanya Index Manga & Halaman Statis)
app.get('/sitemap_index.xml', async (req, res) => {
try {
const baseUrl = process.env.SITE_URL || `${req.protocol}://${req.get('host')}`;
const lastMod = new Date().toISOString();
// Hitung halaman Manga saja (Chapter dihapus)
const totalManga = await Manga.countDocuments();
const totalMangaPages = Math.ceil(totalManga / SITEMAP_LIMIT) || 1;
let xml = `<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="/main-sitemap.xsl"?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<sitemap>
<loc>${baseUrl}/page-sitemap.xml</loc>
<lastmod>${lastMod}</lastmod>
</sitemap>
<sitemap>
<loc>${baseUrl}/genre-sitemap.xml</loc>
<lastmod>${lastMod}</lastmod>
</sitemap>`;
// Loop hanya untuk Manga
for (let i = 1; i <= totalMangaPages; i++) {
const suffix = i === 1 ? '' : `-${i}`;
xml += `<sitemap><loc>${baseUrl}/manga-sitemap${suffix}.xml</loc><lastmod>${lastMod}</lastmod></sitemap>`;
}
xml += `</sitemapindex>`;
res.header('Content-Type', 'application/xml');
res.send(xml);
} catch (error) {
console.error("Error Sitemap Index:", error);
res.status(500).end();
}
});
// D. PAGE SITEMAP (Statis)
app.get('/page-sitemap.xml', (req, res) => {
const baseUrl = process.env.SITE_URL || `${req.protocol}://${req.get('host')}`;
const now = new Date().toISOString();
const staticPages = [
{ url: '/', priority: '1.0' },
{ url: '/manga-list', priority: '0.9' },
{ url: '/genres', priority: '0.8' },
{ url: '/type/manhwa', priority: '0.8' },
{ url: '/type/manga', priority: '0.8' },
{ url: '/type/doujinshi', priority: '0.8' },
];
let xml = `<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="/main-sitemap.xsl"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">`;
staticPages.forEach(page => {
xml += `<url><loc>${baseUrl}${page.url}</loc><lastmod>${now}</lastmod><changefreq>daily</changefreq><priority>${page.priority}</priority></url>`;
});
xml += `</urlset>`;
res.header('Content-Type', 'application/xml');
res.send(xml);
});
// E. GENRE SITEMAP (FIX DUPLIKAT & LOWERCASE)
app.get('/genre-sitemap.xml', async (req, res) => {
try {
const baseUrl = process.env.SITE_URL || `${req.protocol}://${req.get('host')}`;
const now = new Date().toISOString();
const rawGenres = await Manga.distinct('tags');
const uniqueGenres = new Set();
rawGenres.forEach(tag => {
if (tag) {
const cleanSlug = tag.trim().replace(/\s+/g, '-').toLowerCase();
if (cleanSlug.length > 0) {
uniqueGenres.add(cleanSlug);
}
}
});
let xml = `<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="/main-sitemap.xsl"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">`;
for (const slug of uniqueGenres) {
const safeUrl = encodeURIComponent(slug);
xml += `<url><loc>${baseUrl}/genre/${safeUrl}</loc><lastmod>${now}</lastmod><priority>0.7</priority></url>`;
}
xml += `</urlset>`;
res.header('Content-Type', 'application/xml');
res.send(xml);
} catch (e) {
console.error(e);
res.status(500).end();
}
});
// F. MANGA SITEMAP (Cursor Stream + allowDiskUse)
app.get(/^\/manga-sitemap(-(\d+))?\.xml$/, async (req, res) => {
try {
const baseUrl = process.env.SITE_URL || `${req.protocol}://${req.get('host')}`;
const pageParam = req.params[1];
const page = pageParam ? parseInt(pageParam) : 1;
const skip = (page - 1) * SITEMAP_LIMIT;
const xmlHeader = `<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="/main-sitemap.xsl"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">`;
const xmlFooter = `</urlset>`;
res.header('Content-Type', 'application/xml');
res.write(xmlHeader);
const cursor = Manga.find()
.select('slug updatedAt thumb title')
.sort({ updatedAt: -1 })
.skip(skip)
.limit(SITEMAP_LIMIT)
.allowDiskUse(true)
.lean()
.cursor();
for await (const m of cursor) {
try {
let dateObj = m.updatedAt ? new Date(m.updatedAt) : new Date();
if (isNaN(dateObj.getTime())) dateObj = new Date();
let imageXml = '';
if (m.thumb && m.thumb.startsWith('http')) {
const cleanTitle = escapeXml(m.title || 'Manga');
const cleanThumb = escapeXml(m.thumb);
imageXml = `<image:image><image:loc>${cleanThumb}</image:loc><image:title>${cleanTitle}</image:title></image:image>`;
}
const entry = `
<url>
<loc>${baseUrl}/manga/${escapeXml(m.slug)}</loc>
<lastmod>${dateObj.toISOString()}</lastmod>
<changefreq>weekly</changefreq>
<priority>0.9</priority>
${imageXml}
</url>`;
res.write(entry);
} catch (err) {
console.error("Skipping bad manga:", m._id);
}
}
res.end(xmlFooter);
} catch (error) {
console.error("Error Sitemap Manga Fatal:", error);
if(!res.headersSent) res.status(500).end();
else res.end();
}
});
// STATIC PAGES - Cache 1 Jam
app.get('/privacy', simpleCache(3600), (req, res) => res.render('privacy', {
title: 'Privacy Policy',
desc: 'Kebijakan Privasi'
}));
app.get('/terms', simpleCache(3600), (req, res) => res.render('terms', {
title: 'Terms of Service',
desc: 'Syarat dan Ketentuan'
}));
app.get('/contact', simpleCache(3600), (req, res) => res.render('contact', {
title: 'Contact Us',
desc: 'Hubungi Kami'
}));
// PROFIL PAGE
app.get('/profile', (req, res) => {
res.render('profile',
{
title: `Profil Saya - ${res.locals.siteName}`,
desc: 'Lihat bookmark dan riwayat bacaan kamu.'
});
});
// LANDING PAGE APLIKASI - Cache 1 Jam
app.get('/app', simpleCache(3600), (req, res) => {
res.render('apk', {
title: `Download Aplikasi - ${res.locals.siteName}`,
desc: 'Download aplikasi Doujindesu versi terbaru untuk Android. Baca doujinshi lebih nyaman dan cepat.',
currentUrl: '/app'
});
});
app.use('/api', apiRoutes);
app.use((req, res) => res.status(404).render('404', {
title: '404 - Tidak Ditemukan',
desc: 'Halaman tidak ditemukan.'
}));
// ==========================================
// 4. SERVER STARTUP
// ==========================================
const DB_URI = process.env.DB_URI;
if (!DB_URI) {
console.error("FATAL ERROR: DB_URI is not defined in environment variables.");
process.exit(1);
}
const startServer = async () => {
try {
await mongoose.connect(DB_URI, {
serverSelectionTimeoutMS: 30000
});
console.log('Successfully connected to MongoDB...');
app.listen(PORT, () => {
console.log(`Server is running on port: ${PORT}`);
console.log(`Access at: ${WEBSITE_URL}`);
});
} catch (err) {
console.error('Failed to connect to MongoDB. Server will not start.', err);
process.exit(1);
}
};
startServer();