added api webhook for testing
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,2 +1,3 @@
|
|||||||
/node_modules/
|
/node_modules/
|
||||||
/package-lock.json
|
/package-lock.json
|
||||||
|
/.env
|
||||||
@@ -7,10 +7,11 @@
|
|||||||
"start": "node server.js"
|
"start": "node server.js"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"body-parser": "^1.20.2",
|
||||||
|
"dotenv": "^16.4.5",
|
||||||
"express": "^4.17.1",
|
"express": "^4.17.1",
|
||||||
"three": "^0.164.1"
|
"three": "^0.164.1"
|
||||||
},
|
},
|
||||||
"devDependencies": {},
|
|
||||||
"keywords": [],
|
"keywords": [],
|
||||||
"author": "",
|
"author": "",
|
||||||
"license": "ISC"
|
"license": "ISC"
|
||||||
|
|||||||
37
server.js
37
server.js
@@ -1,12 +1,20 @@
|
|||||||
const express = require('express');
|
const express = require('express');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
|
const bodyParser = require('body-parser')
|
||||||
|
const crypto = require('crypto')
|
||||||
|
const { exec } = require('child_process')
|
||||||
|
require('dotenv').config();
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
const PORT = process.env.PORT || 3000;
|
const PORT = process.env.PORT || 3000;
|
||||||
|
const GITEA_SECRET = process.env.GITEA_SECRET;
|
||||||
|
|
||||||
// Serve static files from the 'public' directory
|
// Serve static files from the 'public' directory
|
||||||
app.use(express.static(path.join(__dirname, 'public')));
|
app.use(express.static(path.join(__dirname, 'public')));
|
||||||
|
|
||||||
|
// Middleware to parse JSON payloads
|
||||||
|
app.use(bodyParser.json())
|
||||||
|
|
||||||
// Custom middleware to handle URLs without .html for specific routes
|
// Custom middleware to handle URLs without .html for specific routes
|
||||||
app.use((req, res, next) => {
|
app.use((req, res, next) => {
|
||||||
// Extract the path without any query parameters
|
// Extract the path without any query parameters
|
||||||
@@ -49,6 +57,35 @@ app.get('/articles/:articleName', (req, res) => {
|
|||||||
res.sendFile(path.join(__dirname, 'public/articles', `${articleName}.html`));
|
res.sendFile(path.join(__dirname, 'public/articles', `${articleName}.html`));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Webhook handler
|
||||||
|
app.post('/api', (req, res) => {
|
||||||
|
const signature = req.headers['x-gitea-signature'];
|
||||||
|
const payload = JSON.stringify(req.body);
|
||||||
|
|
||||||
|
if (!signature || !payload) {
|
||||||
|
return res.status(400).send('Invalid payload or missing signature');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verify the secret
|
||||||
|
const hmac = crypto.createHmac('sha256', GITEA_SECRET);
|
||||||
|
const digest = `sha256=${hmac.update(payload).digest('hex')}`;
|
||||||
|
|
||||||
|
if (crypto.timingSafeEqual(Buffer.from(signature), Buffer.from(digest))) {
|
||||||
|
// Secret is valid, update the repository
|
||||||
|
exec('/home/gnome.sh', (err, stdout, stderr) => {
|
||||||
|
if (err) {
|
||||||
|
console.error(`Error updating repository: ${stderr}`);
|
||||||
|
return res.status(500).send('Server error');
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(`Repository updated: ${stdout}`);
|
||||||
|
res.status(200).send('Repository updated successfully');
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
res.status(401).send('Invalid secret');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
// Error handling
|
// Error handling
|
||||||
app.use((err, req, res, next) => {
|
app.use((err, req, res, next) => {
|
||||||
console.error(err.stack);
|
console.error(err.stack);
|
||||||
|
|||||||
Reference in New Issue
Block a user