diff --git a/.github/workflows/deploy-server.yml b/.github/workflows/deploy-server.yml new file mode 100644 index 0000000..26eeead --- /dev/null +++ b/.github/workflows/deploy-server.yml @@ -0,0 +1,28 @@ +name: Deploy to EC2 + +on: + push: + branches: + - master + +jobs: + deploy: + runs-on: ubuntu-latest + + steps: + - name: Set up SSH key + uses: webfactory/ssh-agent@v0.5.4 + with: + ssh-private-key: ${{ secrets.EC2_KEY }} + + - name: Deploy to EC2 + env: + EC2_HOST: ${{ secrets.EC2_HOST }} + EC2_USER: ${{ secrets.EC2_USER }} + run: | + ssh -o StrictHostKeyChecking=no $EC2_USER@$EC2_HOST << 'EOF' + cd /home/ubuntu # 프로젝트 디렉토리로 이동 + git pull origin master # 최신 코드 가져오기 + npm install # 의존성 설치 + pm2 restart all # PM2로 애플리케이션 재실행 + EOF diff --git a/src/index.ts b/src/index.ts index d67731e..9720475 100644 --- a/src/index.ts +++ b/src/index.ts @@ -8,12 +8,17 @@ import summarizeRoutes from "./routes/summarizeRoutes"; dotenv.config(); const app = express(); + app.use(bodyParser.json()); app.use("/slack", slackRoutes); app.use("/api", summarizeRoutes); +app.get("/", (req, res) => { + res.send("Hello, World!"); +}); + const PORT = process.env.PORT || 3000; app.listen(PORT, () => { console.log(`서버가 ${PORT}번 포트에서 실행 중입니다.`); diff --git a/src/routes/slackRoutes.ts b/src/routes/slackRoutes.ts index 795e251..84a2ca6 100644 --- a/src/routes/slackRoutes.ts +++ b/src/routes/slackRoutes.ts @@ -8,22 +8,30 @@ const router = express.Router(); const slackToken = process.env.SLACK_BOT_TOKEN || ""; +const processedEvents = new Set(); // 간단한 예로 메모리에 저장 + if (!slackToken) { console.error("SLACK_BOT_TOKEN 환경 변수가 설정되지 않았습니다."); - process.exit(1); // 환경 변수가 설정되지 않았으면 앱을 종료합니다. } const slackClient = new WebClient(slackToken); router.post("/events", async (req, res) => { - const { type, event } = req.body; + const { type, event, event_id } = req.body; if (type === "url_verification") { // 슬랙 URL 검증 요청 처리 return res.json({ challenge: req.body.challenge }); } + if (processedEvents.has(event_id)) { + // 이미 처리된 이벤트에 대해 200 응답을 보내고 종료 + return res.status(200).send("Event already processed."); + } + if (event && event.type === "app_mention") { + processedEvents.add(event_id); + const { text, channel, ts } = event; const youtubeUrl = extractYoutubeUrl(text);