-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
94 lines (78 loc) · 2.19 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/**
* This is the main Node.js server script for your project.
*/
const path = require("path");
const cmd = require('node-cmd');
const crypto = require('crypto');
const fastify = require("fastify")({
logger: false ,
});
const axios = require("axios");
// Webhook handler
const onWebhook = (req, reply) => {
let hmac = crypto.createHmac('sha1', process.env.SECRET);
let sig = `sha1=${hmac.update(JSON.stringify(req.body)).digest('hex')}`;
if (req.headers['x-github-event'] === 'push' && sig === req.headers['x-hub-signature']) {
cmd.run('chmod 777 ./git.sh');
cmd.get('./git.sh', (err, data) => {
if (data) {
console.log(data);
}
if (err) {
console.log(err);
}
});
cmd.run('refresh');
}
reply.sendStatus(200);
};
// GitHub webhook endpoint
fastify.post('/git', onWebhook);
// Enable CORS for all requests
fastify.register(require('@fastify/cors'), {
origin: "*", // Adjust this for production
});
// CORS bypass route for proxying requests
fastify.route({
method: ['GET', 'POST', 'PUT', 'DELETE'],
url: '/cors-bypass/*',
handler: async (request, reply) => {
try {
const targetUrl = request.params['*'];
if (!targetUrl) {
return reply.status(400).send({ error: 'Target URL is required' });
}
const authHeader = request.headers.authorization;
const response = await axios({
method: request.method,
url: targetUrl,
headers: {
Authorization: authHeader,
},
});
reply.status(response.status).send(response.data);
} catch (error) {
console.error('Error forwarding request:', error);
reply.status(error.response?.status || 500).send({
error: error.message,
...(error.response?.data && { details: error.response.data }),
});
}
},
});
// Setup static files
fastify.register(require("@fastify/static"), {
root: path.join(__dirname, ""),
prefix: "/",
});
// Run the server and log the address
fastify.listen(
{ port: process.env.PORT, host: "0.0.0.0" },
function (err, address) {
if (err) {
console.error(err);
process.exit(1);
}
console.log(`Your app is listening on ${address}`);
}
);