Add Gitea webhook auto-deploy: webhook receiver plus server.js proxy route

This commit is contained in:
2026-05-24 23:54:55 +08:00
parent 48b7f481cc
commit d4b217f60f
6 changed files with 378 additions and 10 deletions
+39 -1
View File
@@ -8,6 +8,7 @@ const PORT = 80;
const BACKEND = process.env.BACKEND_URL || 'http://publish-backend:8080';
const STATIC_DIR = '/app/dist';
const UPLOADS_DIR = '/app/uploads';
const WEBHOOK_HOST = process.env.WEBHOOK_HOST || 'host.docker.internal:5000';
const MIME_TYPES = {
'.html': 'text/html',
@@ -93,6 +94,40 @@ function proxyRequest(req, res) {
}
}
function proxyWebhook(req, res) {
// Forward /webhook requests to the Python webhook receiver on NAS host
const targetUrl = `http://${WEBHOOK_HOST}${req.url}`;
const parsedUrl = url.parse(req.url);
const options = {
hostname: url.parse(targetUrl).hostname,
port: url.parse(targetUrl).port || 80,
path: req.url,
method: req.method,
headers: {}
};
['content-type', 'authorization', 'x-gitea-secret', 'x-gitea-event', 'user-agent', 'host'].forEach(h => {
const key = h.toLowerCase();
if (req.headers[key]) options.headers[h] = req.headers[key];
});
const proxyReq = http.request(options, (proxyRes) => {
res.writeHead(proxyRes.statusCode, proxyRes.headers);
proxyRes.pipe(res);
});
proxyReq.on('error', (e) => {
res.writeHead(502);
res.end('Webhook receiver error: ' + e.message);
});
if (['POST', 'PUT', 'DELETE'].includes(req.method)) {
req.pipe(proxyReq);
} else {
proxyReq.end();
}
}
const server = http.createServer((req, res) => {
// CORS headers
res.setHeader('Access-Control-Allow-Origin', '*');
@@ -104,7 +139,10 @@ const server = http.createServer((req, res) => {
return;
}
if (req.url.startsWith('/api') || req.url.startsWith('/uploads')) {
if (req.url.startsWith('/webhook')) {
// Proxy to webhook receiver running on NAS host (host.docker.internal:5000)
proxyWebhook(req, res);
} else if (req.url.startsWith('/api') || req.url.startsWith('/uploads')) {
proxyRequest(req, res);
} else {
serveStatic(req, res);