import {Hono} from 'hono';
|
import {serve} from '@hono/node-server';
|
import {ISearchParams} from '../type';
|
import {getSearchProvider} from '../search-providers';
|
import {hostname, port} from "../config";
|
|
const app = new Hono();
|
|
app.get('/', (c) => {
|
return c.text('WakuWaku API:\n/search');
|
});
|
|
app.post('/search', async (c: any) => {
|
const params = await c.req.json<ISearchParams>();
|
const {stationId, keyword} = params;
|
|
if (!stationId || !keyword) {
|
return c.json({error: 'stationId and keyword are required'}, 400);
|
}
|
|
try {
|
const provider = getSearchProvider(stationId);
|
const result = await provider.search(params);
|
console.clear();
|
console.clear();
|
console.log(`Server running on http://${hostname}:${port}`);
|
console.log('last request:')
|
console.log({
|
...result,
|
dataList:result.dataList.length,
|
firstRow:result.dataList[0],
|
randomRow:result.dataList[Math.floor(Math.random()*result.dataList.length)],
|
lastRow:result.dataList[result.dataList.length-1],
|
})
|
// logJson(results).catch(console.error)
|
return c.json(result);
|
} catch (error: any) {
|
return c.json({error: error.message}, 500);
|
}
|
});
|
|
serve({
|
fetch: app.fetch,
|
port,
|
hostname
|
}, () => {
|
console.log(`Server running on http://${hostname}:${port}`);
|
});
|