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
|
import axios from 'axios';
import { notFound } from 'next/navigation';
import PageNotFound from '../../404';
export default async function handler(req, res) {
const { url = '', page = 1, limit = 30, all } = req.query;
let q = '*:*';
// ✅ kalau BUKAN sitemap
if (!all) {
const cleanUrl = url.trim();
if (!cleanUrl) {
return res.status(400).json({ error: 'Missing url param' });
}
q = `keywords_s:"${cleanUrl}"`;
}
const offset = (page - 1) * limit;
const params = [
`q.op=AND`,
`q=${q}`,
`indent=true`,
`rows=${limit}`,
`start=${offset}`,
];
try {
const result = await axios.post(
`${process.env.SOLR_HOST}/solr/searchkey/select`,
params.join('&'),
{
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
},
);
const solrResponse = result.data;
if (solrResponse.response.numFound === 0) {
return res.status(404).json({ error: 'Not Found' });
} else {
res.status(200).json(result.data);
}
} catch (error) {
console.error(error?.response?.data || error);
res.status(500).json({ error: 'Internal Server Error' });
}
}
|