-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
116 lines (92 loc) · 3 KB
/
server.js
File metadata and controls
116 lines (92 loc) · 3 KB
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
const express = require('express');
const fs = require('fs');
const path = require('path');
require('dotenv').config();
const markdown = require('markdown-it')();
const crypto = require('crypto');
const exec = require('child_process').exec;
const bodyParser = require('body-parser');
const GITHUB_WEBHOOK_SECRET = process.env.GITHUB_WEBHOOK_SECRET || "";
const articlesRouter = require('./routes/chats');
const app = express();
app.use('/chats', articlesRouter);
const PORT = process.env.PORT || 3001;
app.use(bodyParser.json());
app.use(express.static(path.join(__dirname, 'public')));
// Serve canvases from the first directory
app.get('/load-canvases-1', (req, res) => {
const scriptFolderPath = path.join(__dirname, 'scripts-1');
readAndSendCanvasScripts(scriptFolderPath, res);
});
// Serve canvases from the second directory
app.get('/load-canvases-2', (req, res) => {
const scriptFolderPath = path.join(__dirname, 'scripts-2');
readAndSendCanvasScripts(scriptFolderPath, res);
});
app.post('/git-webhook', (req, res) => {
updateRepo(req, res);
});
function updateRepo(req, res) {
const payload = JSON.stringify(req.body);
const signature = req.header('X-Hub-Signature-256');
if (!payload || !signature) {
res.status(400).send('Invalid request');
return;
}
// Verify the payload's signature
const hmac = crypto.createHmac('sha256', GITHUB_WEBHOOK_SECRET);
const digest = 'sha256=' + hmac.update(payload).digest('hex');
if (signature !== digest) {
res.status(401).send('Unauthorized');
return;
}
// Check if the event is a push event to the main branch
const event = req.header('X-GitHub-Event');
const ref = req.body.ref;
if (event === 'push' && ref === 'refs/heads/main') {
// Update the repository and restart the server
exec('git pull && npm install && pm2 restart canvasser', (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return;
}
console.log(`stdout: ${stdout}`);
console.log(`stderr: ${stderr}`);
});
res.status(200).send('OK');
} else {
res.status(200).send('Not a push event to the main branch');
}
};
function readAndSendCanvasScripts(scriptFolderPath, res) {
fs.readdir(scriptFolderPath, (err, files) => {
if (err) {
console.error(err);
res.status(500).send('Error reading script files');
return;
}
const canvasData = files.map(file => {
const scriptContent = fs.readFileSync(path.join(scriptFolderPath, file), 'utf-8');
return {
scriptName: file,
scriptContent,
};
});
res.json(canvasData);
});
}
app.get('/articles/:filename', (req, res) => {
const filename = req.params.filename;
const filepath = `./articles/${filename}.md`;
fs.readFile(filepath, 'utf8', (err, content) => {
if (err) {
// Handle the error
} else {
const html = markdown.render(content);
res.send(html);
}
});
});
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});