-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
94 lines (76 loc) · 2.39 KB
/
app.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import express from 'express';
import bodyParser from 'body-parser';
import dotenv from "dotenv";
import schedule from "node-schedule";
import fetch from "node-fetch";
import {initCronJob, initVNubanCronJob} from "./cronjob.js";
dotenv.config();
import {createContractController} from "./controller/createContractController.js";
import {
createContractForVAMSController,
subscribeToSNSTopicController,
createContractFromTopicController,
unsubscribeToSNSTopicController
} from "./controller/createContractForVAMSController.js";
import createVNuban from "./controller/createVNubanController.js";
import appConfig from './configs/appConfig.js';
const app = express();
const port = appConfig.PORT;
app.use(bodyParser.json());
app.use(
bodyParser.urlencoded({
extended: true,
})
);
app.get('/', (request, response) => {
response.json({ app: 'batch-jobs' });
});
app.get('/create-contracts', createContractController);
app.post('/v1/create-contract', createContractForVAMSController);
app.post('/v1/sns-topic/subscribe', subscribeToSNSTopicController);
app.post('/v1/sns-topic/unsubscribe', unsubscribeToSNSTopicController);
app.post('/v1/web-hook/create-contract-from-topic', bodyParser.text(), createContractFromTopicController);
app.post('/v1/vnuban/create', createVNuban);
app.get('/test-slack', async (req, res) => {
let resBody="";
try {
const res = await fetch(process.env.SLACK_WEBHOOK_URL, {
method: 'post',
body: JSON.stringify({text: "OK"})
});
resBody = await res.text();
} catch (e) {
console.log(e.message)
}
res.json({status: resBody});
});
app.get("/check-localhost", async (req, res) => {
try{
const r = await fetch("http://localhost:4020");
const resJSON = await r.json();
res.json(resJSON);
} catch(e) {
res.json({error: e.message});
}
});
const shutDown = async () => {
console.log("****** process stopped removed all scheduled jobs ******");
console.log(schedule.scheduledJobs);
try {
for (let item in schedule.scheduledJobs) {
await schedule.scheduledJobs[item].deleteFromSchedule();
}
await schedule.gracefulShutdown();
} catch(e) {
console.log(e.message)
} finally {
process.exit(0)
}
};
process.on('SIGTERM', shutDown);
process.on('SIGINT', shutDown);
app.listen(port, () => {
console.log(`App running on port ${port}.`);
initCronJob();
initVNubanCronJob();
});