files
simple-auto_browsing/index.js
2025-10-13 15:39:49 +09:00

62 lines
2.3 KiB
JavaScript

require('dotenv').config();
const express = require('express');
const cron = require('node-cron');
const db = require('./db');
const { runBot } = require('./bot');
const app = express();
const PORT = 3000;
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// ### 1. 크론 스케줄러 설정 ###
cron.schedule('*/5 * * * *', () => {
console.log(`[CRON] 스케줄된 작업을 실행합니다...`);
runBot();
});
// ### 2. 웹 서버 API (라우트) 설정 ###
app.get('/', async (req, res) => {
try {
const accounts = await db.query(`
SELECT dal.*, dl.DOMAIN_NM
FROM DOMAIN_ACCNT_LIST dal
JOIN DOMAIN_LIST dl ON dal.DOMAIN_SEQ_ID = dl.DOMAIN_SEQ_ID
ORDER BY dal.DOMAIN_SEQ_ID, dal.DOMAIN_ACCNT_ID
`);
let html = `<h1>출석 계정 목록</h1>
<table border="1" style="width:100%; border-collapse: collapse;">
<tr style="background-color:#f2f2f2;">
<th style="padding: 8px;">사이트</th>
<th style="padding: 8px;">계정 ID</th>
<th style="padding: 8px;">상태</th>
<th style="padding: 8px;">다음 실행</th>
<th style="padding: 8px;">사용여부</th>
</tr>`;
accounts.forEach(acc => {
html += `<tr>
<td style="padding: 8px;">${acc.DOMAIN_NM}</td>
<td style="padding: 8px;">${acc.DOMAIN_ACCNT_ID}</td>
<td style="padding: 8px;">${acc.ATNDNC_STTS_CD}</td>
<td style="padding: 8px;">${acc.ATNDNC_STRT_DTTM}</td>
<td style="padding: 8px;">${acc.USE_YN}</td>
</tr>`;
});
html += '</table>';
res.send(html);
} catch (error) {
res.status(500).send("데이터 조회 중 오류 발생: " + error.message);
}
});
// ### 3. 웹 서버 실행 ###
app.listen(PORT, () => {
console.log(`웹 서버가 http://localhost:${PORT} 에서 실행 중입니다. (외부 접속: http://localhost:8080)`);
console.log('자동 출석 봇이 스케줄 대기 중입니다...');
// 서버 시작 시 1회 즉시 실행 (테스트용)
runBot();
});