files
simple-auto_browsing/index.js
2025-10-13 14:32:41 +09:00

63 lines
2.2 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;
// JSON 요청 본문을 파싱하기 위한 미들웨어
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// ### 1. 크론 스케줄러 설정 ###
// 5분마다 runBot 함수를 실행
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
`);
// 간단한 HTML 테이블 형태로 결과 표시
let html = `<h1>출석 계정 목록</h1>
<table border="1">
<tr><th>사이트</th><th>계정 ID</th><th>상태</th><th>다음 실행</th><th>사용여부</th></tr>`;
accounts.forEach(acc => {
html += `<tr>
<td>${acc.DOMAIN_NM}</td>
<td>${acc.DOMAIN_ACCNT_ID}</td>
<td>${acc.ATNDNC_STTS_CD}</td>
<td>${acc.ATNDNC_STRT_DTTM}</td>
<td>${acc.USE_YN}</td>
</tr>`;
});
html += '</table>';
res.send(html);
} catch (error) {
res.status(500).send("데이터 조회 중 오류 발생: " + error.message);
}
});
// TODO: 계정 정보 수정, 추가, 삭제 등의 API 엔드포인트를 여기에 추가할 수 있습니다.
// ### 3. 웹 서버 실행 ###
app.listen(PORT, () => {
console.log(`웹 서버가 http://localhost:${PORT} 에서 실행 중입니다. (외부 접속: http://localhost:8080)`);
console.log('자동 출석 봇이 스케줄 대기 중입니다...');
// 서버 시작 시 1회 즉시 실행 (테스트용)
runBot();
});