-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
132 lines (114 loc) · 3.01 KB
/
index.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Configure dotenv to read env
import dotenv from "dotenv";
dotenv.config({
path: ".env",
});
import express from "express";
import rateLimit from "express-rate-limit";
import bodyParser from "body-parser";
import morgan from "morgan";
import morganBody from "morgan-body";
import { sendDM } from "./lib/twitter.js";
import { updateIndex, updateGetroJobs, updateClouflareIndex } from "./lib/jobs.js";
import PScale from './service/pscale.js';
const PORT = process.env["PORT"];
const AUTH_TOKEN = process.env["AUTH_TOKEN"];
const app = express();
// Apply the rate limiting middleware to all requests
app.use(
rateLimit({
windowMs: 1 * 60 * 1000, // 1 minute
max: 100, // Limit each IP to 100 requests per `window`
standardHeaders: true, // Return rate limit info in the `RateLimit-*` headers
legacyHeaders: false, // Disable the `X-RateLimit-*` headers
})
);
/*
* For bots to check keepalive
*/
app.get("/keepalive", async (_req, res) => {
res.setHeader("Content-Type", "text/plain");
res.end("Success");
});
// Parse the body into well fromatted JSON
// to be consumed by middlewares
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(
morgan('combined', {
skip: (req, _res) => {
if (req.baseUrl === '/keepalive') {
return false;
}
return true;
},
})
);
morganBody(app, {
logResponseBody: true,
});
/*
* Endpoint '/instagrant' for the bot to send DMs
*/
app.post("/instagrant", async (req, res) => {
if (req.get("AUTH_TOKEN") === AUTH_TOKEN) {
console.log(req.body);
// Trigger a DM to the applier
const [status, msg] = await sendDM(req.body);
if (status) {
res.setHeader("Content-Type", "text/plain");
res.end(msg);
} else {
res.status(400).end(msg);
}
} else {
res.status(401).end("Access Denied");
}
});
/*
* This endpoint is used to handle earn opportunities
*/
app.post("/earn", async (req, res) => {
if (req.get("AUTH_TOKEN") === AUTH_TOKEN) {
// Trigger a DM to the applier
const [status, msg] = await updateIndex();
if (status) {
res.setHeader("Content-Type", "text/plain");
res.end(msg);
} else {
res.status(400).end(msg);
}
} else {
res.status(401).end("Access Denied");
}
});
/*
* This endpoint is used to update getro jobs
*/
app.post("/getro", async (req, res) => {
if (req.get("AUTH_TOKEN") === AUTH_TOKEN) {
// Trigger a DM to the applier
const [status, msg] = await updateGetroJobs();
if (status) {
res.setHeader("Content-Type", "text/plain");
res.end(msg);
} else {
res.status(400).end(msg);
}
} else {
res.status(401).end("Access Denied");
}
});
/*
* This endpoint is used index opportunities
*/
app.post("/index/cloudflare", async (_req, res) => {
await updateClouflareIndex();
res.setHeader("Content-Type", "text/plain");
res.end("Updated");
});
// Start listening
app.listen(PORT, async () => {
await PScale.init();
console.log(`App listening on port ${PORT}`);
});