Skip to content

Commit

Permalink
Merge pull request #11 from TeamSparker/feature/#10
Browse files Browse the repository at this point in the history
[FEAT] slack Webhook 연결
  • Loading branch information
xxeol2 authored Jan 11, 2022
2 parents 9aba967 + e9b4d09 commit e451243
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 1 deletion.
4 changes: 3 additions & 1 deletion functions/api/routes/auth/authSignUpPOST.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const responseMessage = require('../../../constants/responseMessage');
const db = require('../../../db/db');
const { userDB } = require('../../../db');
const jwtHandlers = require('../../../lib/jwtHandlers');
const slackAPI = require('../../../middlewares/slackAPI');

/**
* @회원가입
Expand Down Expand Up @@ -60,7 +61,8 @@ module.exports = async (req, res) => {
} catch (error) {
console.log(error);
functions.logger.error(`[SIGNUP ERROR] [${req.method.toUpperCase()}] ${req.originalUrl}`, `[CONTENT] socialId:${socialId} ${error}`);

const slackMessage = `[SIGNUP ERROR] [${req.method.toUpperCase()}] ${req.originalUrl} [CONTENT] socialId:${socialId} ${error} ${JSON.stringify(error)}`;
slackAPI.sendMessageToSlack(slackMessage, slackAPI.DEV_WEB_HOOK_ERROR_MONITORING);
res.status(statusCode.INTERNAL_SERVER_ERROR).send(util.fail(statusCode.INTERNAL_SERVER_ERROR, responseMessage.INTERNAL_SERVER_ERROR));
} finally {
client.release();
Expand Down
33 changes: 33 additions & 0 deletions functions/middlewares/slackAPI.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const functions = require('firebase-functions');
const axios = require('axios');

const dotenv = require('dotenv');

dotenv.config();

// 슬랙 Webhook에서 발급받은 endpoint를 .env 파일에서 끌어옴
// endpoint 자체는 깃허브에 올라가면 안 되기 때문!
const DEV_WEB_HOOK_ERROR_MONITORING = process.env.DEV_WEB_HOOK_ERROR_MONITORING;

const sendMessageToSlack = (message, apiEndPoint = DEV_WEB_HOOK_ERROR_MONITORING) => {
// 슬랙 Webhook을 이용해 슬랙에 메시지를 보내는 코드
try {
axios
.post(apiEndPoint, { text: message })
.then((response) => {})
.catch((e) => {
throw e;
});
} catch (e) {
console.error(e);
// 슬랙 Webhook 자체에서 에러가 났을 경우,
// Firebase 콘솔에 에러를 찍는 코드
functions.logger.error('[slackAPI 에러]', { error: e });
}
};

// 이 파일에서 정의한 변수 / 함수를 export 해서, 다른 곳에서 사용할 수 있게 해주는 코드
module.exports = {
sendMessageToSlack,
DEV_WEB_HOOK_ERROR_MONITORING,
};
13 changes: 13 additions & 0 deletions functions/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions functions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
},
"main": "index.js",
"dependencies": {
"axios": "^0.24.0",
"busboy": "^0.3.1",
"cookie-parser": "^1.4.6",
"cors": "^2.8.5",
Expand Down

0 comments on commit e451243

Please sign in to comment.