-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
78 lines (63 loc) · 1.94 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
require('dotenv').config();
const express = require('express'),
app = express(),
http = require('http'),
port = process.env.PORT || 1337,
morgan = require('morgan'),
bodyParser = require('body-parser'),
helmet = require('helmet'),
telegram = require('./telegram/service.js'),
github = require('./github/service'),
users = require('./users/service'),
config = require('./config'),
{ createTerminus, HealthCheckError } = require('@godaddy/terminus')
const telegramBot = telegram.newService(config.TELEGRAM_TOKEN)
/**
* Health Check Services
*/
const readinessProbe = ({ http }) => async () => {
const isServerListening = http.listening
if (!isServerListening) {
throw HealthCheckError
}
}
const terminusOptions = {
healthChecks: {
'/livez': () => {},
'/readyz': readinessProbe({ app })
},
timeout: 1000, // in miliseconds
beforeShutdown: () => {
console.log("server will be closed")
}
}
/*
* Express Middlewares
*/
app.use(helmet())
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(morgan('dev'));
// Get messages from Github
app.post('/github', (request, response) => {
const githubData = github.parseWebhookPayload(JSON.parse(request.body.payload));
telegram.sendPullRequestNotification(telegramBot,config.TELEGRAM_CHANNEL_ID,{
messageAction: github.getMessageBasedOnActionType(githubData.action),
user: users.getUserBasedOnGithubUsername(githubData.assigner),
title: githubData.title,
repository: githubData.repository,
PRUrl: githubData.PRUrl
})
response.status(200).json({});
});
// Just testing :P
app.get('/ping', (request, response) => {
response.json({
"bot": "ping juga"
});
})
const server = http.createServer(app)
createTerminus(server, terminusOptions)
server.listen(port, () => {
console.log('App running on port ' + port);
})